1

I have this fragment UserProfileFragment.java, Here I have a form with some fields also with one user profile Image-view where the user uploads its image, initially everything works fine but when the user wants to update the details after fetching its data from the server also fetching and setting the thumbnail of image on image-view, Here when the user updates the details without changing the current profile image of Image-view the app crashes giving the error as follows:

FATAL EXCEPTION: main
    Process: com.***.******, PID: 16313
    java.lang.NullPointerException
        at java.io.File.<init>(File.java:283)
        at com.***.*****.fragment.UserProfileFragment.Updatemyprofile(UserProfileFragment.java:576)
        at com.***.*****.fragment.UserProfileFragment.onClick(UserProfileFragment.java:343)

When the user changes the profile image the app won't crash while updating.

My update user method as follows:

private void Updatemyprofile() {
//pass it like this
        File file = new File(picturePath);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

// MultipartBody.Part is used to send also the actual file name
        MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);

// add another part within the multipart request
        RequestBody userid = RequestBody.create(MediaType.parse("multipart/form-data"), Constant.Userid);
        RequestBody role = RequestBody.create(MediaType.parse("multipart/form-data"), Constant.Role);
        RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"), edtfullName.getText().toString().trim());
        RequestBody pincode = RequestBody.create(MediaType.parse("multipart/form-data"), edtpincode.getText().toString().trim());
        RequestBody address = RequestBody.create(MediaType.parse("multipart/form-data"), edtaddress.getText().toString().trim());
        RequestBody gender = RequestBody.create(MediaType.parse("multipart/form-data"), Gender);
        RequestBody dob = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(epoch));

        apiInterface = RetrofitApiUtils.getAPIService();
        apiInterface.Updatemyprofile(Constant.AccessToken, userid, role, name, pincode, address, gender, dob, body ).enqueue(new Callback<UpdateUserProf>() {
            @Override
            public void onResponse(@NonNull Call<UpdateUserProf> call, @NonNull Response<UpdateUserProf> response) {
                try {
                    if (response.isSuccessful()) {
                        if (response.body() != null && response.body().getStatus().equals(true)) {
                            Toast.makeText(getActivity(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
                            UpdateUserProfData updateUserProfData = response.body().getData();
                            Log.d("Results ==>>...1", new GsonBuilder().setPrettyPrinting().create().toJson(updateUserProfData));
                            myprofile(Constant.Userid, Constant.Role);
                        } else {
                            Toast.makeText(getActivity(), response.body().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        CallingToastMethod.showToast(getActivity());
                    }
                } catch (Exception e) {
                    try {
                        throw new IOException(e.toString());
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<UpdateUserProf> call, Throwable t) {
                Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

Also I have tried this method for image validation:

  if (imgprofile.getDrawable() == null){
                        Toast.makeText(getActivity(), "Please select Image", Toast.LENGTH_SHORT).show();
                    }

It works when the user enters its details for the first time but not working for updating it.

My interface class method as follows:

// Update My Profile
    @Multipart
    @POST("updatemyprofile.php")
        Call<UpdateUserProf> Updatemyprofile(@Header("Authorization") String authkey,
                                             @Part("id") RequestBody id,
                                             @Part("role") RequestBody role,
                                             @Part("name") RequestBody name,
                                             @Part("pincode") RequestBody pincode,
                                             @Part("address") RequestBody address,
                                             @Part("gender") RequestBody gender,
                                             @Part("dob") RequestBody dob,
                                             @Part MultipartBody.Part image);

Please tell me how to do this I have been stuck on this for days and don't know how to do this... Thank you! Be safe..

  • 1
    Please read [what-is-a-nullpointerexception-and-how-do-i-fix-it](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). The problem is with your `File` object as per the error you attached. –  May 28 '20 at 17:05
  • Brother can you explain me this in brief related to my code... or what else can I do to prevent the crash? – Rajvir Bedi May 28 '20 at 17:11
  • Do you know how to use debugger in Android studio? If you do please try put the break points on your `File` object and try checking if the instance is returning `null`. If you don't please read [this](https://developer.android.com/studio/debug). `NullPointerExceptions` are very common in java –  May 28 '20 at 17:12
  • ok let me try the debugging whether its returning null or not? – Rajvir Bedi May 28 '20 at 17:24
  • 1
    That did returning null. All I have to do is to add an If condition to check the null pointer exception. Thank you for helping it.. – Rajvir Bedi May 28 '20 at 18:19

1 Answers1

0

May be your picturePath not found

Try inside if condition:

if(picturePath != null){
File file = new File(picturePath);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

// MultipartBody.Part is used to send also the actual file name
        MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
}```
Dinesh
  • 1,410
  • 2
  • 16
  • 29