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..