Please help here (fore job interview)
I'm trying to upload an image , when it's starts uploding , it crashes and removes the two basic childrens (image,thumb image)
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.pc.newchatj, PID: 9350 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at com.example.pc.newchatj.SettingsActivity$1.onDataChange(SettingsActivity.java:103) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
And in my Firebase database it removes the two childrens (image , thumb_image)
anyone please help
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
String image = dataSnapshot.child("image").getValue().toString();
String status = dataSnapshot.child("status").getValue().toString();
String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();
mName.setText(name);
mStatus.setText(status);
if (!image.equals("default")) {
Picasso.get().load(image).placeholder(R.drawable.default_avatar).into(mDisplayImage);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
I've added StorageReference for both (image , thumb image)
filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()){
UploadTask uploadTask = thumb_filepath.putBytes(thumb_byte);
uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> thumb_task) {
if (thumb_task.isSuccessful()){
//getting regular image download url
filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
download_urll = uri.toString();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(SettingsActivity.this, "Regular fail", Toast.LENGTH_SHORT).show();
}
});
//getting thumbnail download url
thumb_filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri thumb_uri) {
thumb_downloadUrl = thumb_uri.toString();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(SettingsActivity.this, "thumb failed", Toast.LENGTH_SHORT).show();
}
});
Map update_hashMap = new HashMap();
update_hashMap.put("image" , download_urll);
update_hashMap.put("thumb_image" , thumb_downloadUrl);
mUserDatabase.updateChildren(update_hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
avi_settings.smoothToHide();
avi_settings.setVisibility(View.INVISIBLE);
Toast.makeText(SettingsActivity.this, "Successfully uploaded", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
Toast.makeText(SettingsActivity.this, "working", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(SettingsActivity.this, "Error uploading file", Toast.LENGTH_SHORT).show();
avi_settings.smoothToHide();
avi_settings.setVisibility(View.INVISIBLE);
}
}
});