1

I am a newbie to the android world. I am trying to develop an app for my school project and stumble upon this issue. please someone help me. My fragment code is bellow. Where I want to fill up a form with Image and upload to PHP, Mysql server for registration. But the app is crashing.

package com.dgdev.mtmicds;


import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.dgdev.mtmicds.DbAccess.Remote.APIClient;
import com.dgdev.mtmicds.DbAccess.Remote.ApiInterface;
import com.dgdev.mtmicds.DbAccess.Remote.UserRegistrationModel;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.app.Activity.RESULT_OK;
import static android.provider.MediaStore.Images.Media.*;
import static android.support.v4.content.PermissionChecker.checkSelfPermission;


/**
 * A simple {@link Fragment} subclass.
 */
public class ProfileFragment extends Fragment {
    View view;
    AppCompatImageView imageView;
    AppCompatEditText etFullname, etEmail, etDob, etMobile, etPsw, etRePsw, etAddr;
    AppCompatButton btnRegister, btnCancel;
    private static final int IMAGE_REQUEST = 7777;
    Bitmap bitmap;

    public ProfileFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_profile, container, false);

        imageView = (AppCompatImageView) view.findViewById(R.id.ProfileDP);
        etFullname = (AppCompatEditText) view.findViewById(R.id.tvfullanme);
        etEmail = (AppCompatEditText) view.findViewById(R.id.tvemail);
        etDob = (AppCompatEditText) view.findViewById(R.id.tvdob);
        etPsw = (AppCompatEditText) view.findViewById(R.id.tvpsw);
        etRePsw = (AppCompatEditText) view.findViewById(R.id.tvpsw_re);
        etAddr = (AppCompatEditText) view.findViewById(R.id.tvaddr);
        etMobile = (AppCompatEditText) view.findViewById(R.id.tvmobile);

        btnRegister = (AppCompatButton) view.findViewById(R.id.btnRegister);
        btnCancel = (AppCompatButton) view.findViewById(R.id.btnCancel);


        /*-----------------------------------------------------------------------------*/
        /* this onClickListener will be responsible for getting image URI from gallery */
        /*-----------------------------------------------------------------------------*/
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectImageFromGallery();
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                uploadData();
            }
        });
        return view;
    }

    private void uploadData() {
        String fullname = etFullname.getText().toString();
        String dob = convertTOMysqlDate(etDob.getText().toString());
        String mobile = etMobile.getText().toString();
        String addr = etAddr.getText().toString();
        String psw = etPsw.getText().toString();
        String prof_pic = imageToString();
        Toast.makeText(view.getContext(), dob, Toast.LENGTH_LONG).show();
        ApiInterface apiInterface = APIClient.GetClient().create(ApiInterface.class);
        Call<UserRegistrationModel> call = apiInterface.RegisterUser(fullname, dob, mobile, addr, psw, prof_pic);
        call.enqueue(new Callback<UserRegistrationModel>() {
            @Override
            public void onResponse(Call<UserRegistrationModel> call, Response<UserRegistrationModel> response) {
                UserRegistrationModel res = response.body();
                Toast.makeText(view.getContext(), res.getStatus(), Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<UserRegistrationModel> call, Throwable t) {
                Toast.makeText(view.getContext(), "You are not able to talk to server!", Toast.LENGTH_LONG).show();
            }
        });
    }

    private String convertTOMysqlDate(String s) {
        String $MysqlDateString;
        String[] DateParts = s.split("/");
        $MysqlDateString = DateParts[2] + "-" + DateParts[1] + "-" + DateParts[0];
        return $MysqlDateString;
    }

    private void selectImageFromGallery() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, IMAGE_REQUEST);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
            Uri path = data.getData();
            try {
                bitmap = getBitmap(getActivity().getApplicationContext().getContentResolver(), path);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    private String imageToString() {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, outputStream);
        byte[] ImageBytes = outputStream.toByteArray();
        return Base64.encodeToString(ImageBytes, Base64.DEFAULT);
    }
}

And I am getting bellow message in logcat

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.dgdev.mtmicds.DbAccess.Remote.UserRegistrationModel.getStatus()' on a null object reference
        at com.dgdev.mtmicds.ProfileFragment$3.onResponse(ProfileFragment.java:105)

Please Help me...I am a newbie...

2 Answers2

0

Try this

@Override
        public void onResponse(Call<UserRegistrationModel> call, Response<UserRegistrationModel> response) {
            if(response.isSuccessful()) {
                UserRegistrationModel res = response.body();
                if(res!=null) {
                    Toast.makeText(view.getContext(), res.getStatus(), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(view.getContext(), "Didn't get response", Toast.LENGTH_LONG).show();
                }
            }
        }
Brijesh Joshi
  • 1,584
  • 1
  • 5
  • 22
-1

Your com.dgdev.mtmicds.DbAccess.Remote.UserRegistrationModel is null. Check whether it is being alloted any value by getting in debug mode. Run the code step by step in debug mode and check how it is getting initialized.

Hint: Check this line of your code in debugger:

UserRegistrationModel res = response.body();
waterbyte
  • 193
  • 1
  • 15