android firebase database java.lang.String java.lang.Object.toString()' on a null object reference











up vote
0
down vote

favorite












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);
}
}
});









share|improve this question






















  • Can you please indicate at which particular line of code does this error occur?
    – Alex Mamo
    Nov 5 at 18:08










  • @AlexMamo it's in the line of the image String image = dataSnapshot.child("image").getValue().toString();
    – Waseem Ha
    Nov 5 at 18:09










  • @AlexMamo i'm new to stackOverflow , can i post the hole code here ?
    – Waseem Ha
    Nov 5 at 18:10










  • There is no need for the entire code. Please see my answer below.
    – Alex Mamo
    Nov 5 at 18:20















up vote
0
down vote

favorite












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);
}
}
});









share|improve this question






















  • Can you please indicate at which particular line of code does this error occur?
    – Alex Mamo
    Nov 5 at 18:08










  • @AlexMamo it's in the line of the image String image = dataSnapshot.child("image").getValue().toString();
    – Waseem Ha
    Nov 5 at 18:09










  • @AlexMamo i'm new to stackOverflow , can i post the hole code here ?
    – Waseem Ha
    Nov 5 at 18:10










  • There is no need for the entire code. Please see my answer below.
    – Alex Mamo
    Nov 5 at 18:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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);
}
}
});









share|improve this question













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);
}
}
});






java android firebase android-studio firebase-realtime-database






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 5 at 18:01









Waseem Ha

288




288












  • Can you please indicate at which particular line of code does this error occur?
    – Alex Mamo
    Nov 5 at 18:08










  • @AlexMamo it's in the line of the image String image = dataSnapshot.child("image").getValue().toString();
    – Waseem Ha
    Nov 5 at 18:09










  • @AlexMamo i'm new to stackOverflow , can i post the hole code here ?
    – Waseem Ha
    Nov 5 at 18:10










  • There is no need for the entire code. Please see my answer below.
    – Alex Mamo
    Nov 5 at 18:20


















  • Can you please indicate at which particular line of code does this error occur?
    – Alex Mamo
    Nov 5 at 18:08










  • @AlexMamo it's in the line of the image String image = dataSnapshot.child("image").getValue().toString();
    – Waseem Ha
    Nov 5 at 18:09










  • @AlexMamo i'm new to stackOverflow , can i post the hole code here ?
    – Waseem Ha
    Nov 5 at 18:10










  • There is no need for the entire code. Please see my answer below.
    – Alex Mamo
    Nov 5 at 18:20
















Can you please indicate at which particular line of code does this error occur?
– Alex Mamo
Nov 5 at 18:08




Can you please indicate at which particular line of code does this error occur?
– Alex Mamo
Nov 5 at 18:08












@AlexMamo it's in the line of the image String image = dataSnapshot.child("image").getValue().toString();
– Waseem Ha
Nov 5 at 18:09




@AlexMamo it's in the line of the image String image = dataSnapshot.child("image").getValue().toString();
– Waseem Ha
Nov 5 at 18:09












@AlexMamo i'm new to stackOverflow , can i post the hole code here ?
– Waseem Ha
Nov 5 at 18:10




@AlexMamo i'm new to stackOverflow , can i post the hole code here ?
– Waseem Ha
Nov 5 at 18:10












There is no need for the entire code. Please see my answer below.
– Alex Mamo
Nov 5 at 18:20




There is no need for the entire code. Please see my answer below.
– Alex Mamo
Nov 5 at 18:20












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Before trying to get the data from the dataSnapshot object, you should check if that data actually exist:



mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) { //Check for existens
String name = dataSnapshot.child("name").getValue(String.class);
String image = dataSnapshot.child("image").getValue(String.class);
String status = dataSnapshot.child("status").getValue(String.class);
String thumb_image = dataSnapshot.child("thumb_image").getValue(String.class);

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) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
});


Also is best to pass String.class to the getValue() method insted of casting it to String. You will not get any NullPointerException anymore.






share|improve this answer





















  • So ? ... that dose not do anything , the data exists on the Storage !
    – Waseem Ha
    Nov 5 at 18:25










  • You said you get NullPointerException at this line of code String image = dataSnapshot.child("image").getValue().toString();. It doesn't matter if the data exist in the Storage, you should check if it exist in your dataSnapshot object. Is has nothing to do with what exist, or does not exist in the Firebase Storage. Have you even tried my solution above? Does it work?
    – Alex Mamo
    Nov 5 at 18:28












  • yes i've tried it , i'm storing an image in the firebase storage , getting it's URL and storing it on the firebase database , but when i store it it crashes
    – Waseem Ha
    Nov 5 at 18:35










  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question or as a comment and indicate the line at which line of code it occurs.
    – Alex Mamo
    Nov 5 at 18:37












  • Hi Johnny! Is there everything alright, have you solved the issue?
    – Alex Mamo
    Nov 6 at 7:42




















up vote
0
down vote













As you wrote NullPointerException rises in line



String name = dataSnapshot.child("name").getValue().toString();


The error message says you try to invoke .toString() on an null object.
You should save the result from String name = dataSnapshot.child("name").getValue() and check if it's not null before calling .toSring() method.






share|improve this answer










New contributor




JohnnyJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Error appers inString image = dataSnapshot.child("image").getValue().toString();
    – Waseem Ha
    Nov 5 at 18:54












  • ok, sorry for this ;) .... but checking the value before calling the method could help nevertheless
    – JohnnyJ
    Nov 5 at 18:57













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159790%2fandroid-firebase-database-java-lang-string-java-lang-object-tostring-on-a-nul%23new-answer', 'question_page');
}
);

Post as a guest
































2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Before trying to get the data from the dataSnapshot object, you should check if that data actually exist:



mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) { //Check for existens
String name = dataSnapshot.child("name").getValue(String.class);
String image = dataSnapshot.child("image").getValue(String.class);
String status = dataSnapshot.child("status").getValue(String.class);
String thumb_image = dataSnapshot.child("thumb_image").getValue(String.class);

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) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
});


Also is best to pass String.class to the getValue() method insted of casting it to String. You will not get any NullPointerException anymore.






share|improve this answer





















  • So ? ... that dose not do anything , the data exists on the Storage !
    – Waseem Ha
    Nov 5 at 18:25










  • You said you get NullPointerException at this line of code String image = dataSnapshot.child("image").getValue().toString();. It doesn't matter if the data exist in the Storage, you should check if it exist in your dataSnapshot object. Is has nothing to do with what exist, or does not exist in the Firebase Storage. Have you even tried my solution above? Does it work?
    – Alex Mamo
    Nov 5 at 18:28












  • yes i've tried it , i'm storing an image in the firebase storage , getting it's URL and storing it on the firebase database , but when i store it it crashes
    – Waseem Ha
    Nov 5 at 18:35










  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question or as a comment and indicate the line at which line of code it occurs.
    – Alex Mamo
    Nov 5 at 18:37












  • Hi Johnny! Is there everything alright, have you solved the issue?
    – Alex Mamo
    Nov 6 at 7:42

















up vote
0
down vote













Before trying to get the data from the dataSnapshot object, you should check if that data actually exist:



mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) { //Check for existens
String name = dataSnapshot.child("name").getValue(String.class);
String image = dataSnapshot.child("image").getValue(String.class);
String status = dataSnapshot.child("status").getValue(String.class);
String thumb_image = dataSnapshot.child("thumb_image").getValue(String.class);

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) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
});


Also is best to pass String.class to the getValue() method insted of casting it to String. You will not get any NullPointerException anymore.






share|improve this answer





















  • So ? ... that dose not do anything , the data exists on the Storage !
    – Waseem Ha
    Nov 5 at 18:25










  • You said you get NullPointerException at this line of code String image = dataSnapshot.child("image").getValue().toString();. It doesn't matter if the data exist in the Storage, you should check if it exist in your dataSnapshot object. Is has nothing to do with what exist, or does not exist in the Firebase Storage. Have you even tried my solution above? Does it work?
    – Alex Mamo
    Nov 5 at 18:28












  • yes i've tried it , i'm storing an image in the firebase storage , getting it's URL and storing it on the firebase database , but when i store it it crashes
    – Waseem Ha
    Nov 5 at 18:35










  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question or as a comment and indicate the line at which line of code it occurs.
    – Alex Mamo
    Nov 5 at 18:37












  • Hi Johnny! Is there everything alright, have you solved the issue?
    – Alex Mamo
    Nov 6 at 7:42















up vote
0
down vote










up vote
0
down vote









Before trying to get the data from the dataSnapshot object, you should check if that data actually exist:



mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) { //Check for existens
String name = dataSnapshot.child("name").getValue(String.class);
String image = dataSnapshot.child("image").getValue(String.class);
String status = dataSnapshot.child("status").getValue(String.class);
String thumb_image = dataSnapshot.child("thumb_image").getValue(String.class);

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) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
});


Also is best to pass String.class to the getValue() method insted of casting it to String. You will not get any NullPointerException anymore.






share|improve this answer












Before trying to get the data from the dataSnapshot object, you should check if that data actually exist:



mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) { //Check for existens
String name = dataSnapshot.child("name").getValue(String.class);
String image = dataSnapshot.child("image").getValue(String.class);
String status = dataSnapshot.child("status").getValue(String.class);
String thumb_image = dataSnapshot.child("thumb_image").getValue(String.class);

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) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
});


Also is best to pass String.class to the getValue() method insted of casting it to String. You will not get any NullPointerException anymore.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 5 at 18:20









Alex Mamo

35.4k62354




35.4k62354












  • So ? ... that dose not do anything , the data exists on the Storage !
    – Waseem Ha
    Nov 5 at 18:25










  • You said you get NullPointerException at this line of code String image = dataSnapshot.child("image").getValue().toString();. It doesn't matter if the data exist in the Storage, you should check if it exist in your dataSnapshot object. Is has nothing to do with what exist, or does not exist in the Firebase Storage. Have you even tried my solution above? Does it work?
    – Alex Mamo
    Nov 5 at 18:28












  • yes i've tried it , i'm storing an image in the firebase storage , getting it's URL and storing it on the firebase database , but when i store it it crashes
    – Waseem Ha
    Nov 5 at 18:35










  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question or as a comment and indicate the line at which line of code it occurs.
    – Alex Mamo
    Nov 5 at 18:37












  • Hi Johnny! Is there everything alright, have you solved the issue?
    – Alex Mamo
    Nov 6 at 7:42




















  • So ? ... that dose not do anything , the data exists on the Storage !
    – Waseem Ha
    Nov 5 at 18:25










  • You said you get NullPointerException at this line of code String image = dataSnapshot.child("image").getValue().toString();. It doesn't matter if the data exist in the Storage, you should check if it exist in your dataSnapshot object. Is has nothing to do with what exist, or does not exist in the Firebase Storage. Have you even tried my solution above? Does it work?
    – Alex Mamo
    Nov 5 at 18:28












  • yes i've tried it , i'm storing an image in the firebase storage , getting it's URL and storing it on the firebase database , but when i store it it crashes
    – Waseem Ha
    Nov 5 at 18:35










  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question or as a comment and indicate the line at which line of code it occurs.
    – Alex Mamo
    Nov 5 at 18:37












  • Hi Johnny! Is there everything alright, have you solved the issue?
    – Alex Mamo
    Nov 6 at 7:42


















So ? ... that dose not do anything , the data exists on the Storage !
– Waseem Ha
Nov 5 at 18:25




So ? ... that dose not do anything , the data exists on the Storage !
– Waseem Ha
Nov 5 at 18:25












You said you get NullPointerException at this line of code String image = dataSnapshot.child("image").getValue().toString();. It doesn't matter if the data exist in the Storage, you should check if it exist in your dataSnapshot object. Is has nothing to do with what exist, or does not exist in the Firebase Storage. Have you even tried my solution above? Does it work?
– Alex Mamo
Nov 5 at 18:28






You said you get NullPointerException at this line of code String image = dataSnapshot.child("image").getValue().toString();. It doesn't matter if the data exist in the Storage, you should check if it exist in your dataSnapshot object. Is has nothing to do with what exist, or does not exist in the Firebase Storage. Have you even tried my solution above? Does it work?
– Alex Mamo
Nov 5 at 18:28














yes i've tried it , i'm storing an image in the firebase storage , getting it's URL and storing it on the firebase database , but when i store it it crashes
– Waseem Ha
Nov 5 at 18:35




yes i've tried it , i'm storing an image in the firebase storage , getting it's URL and storing it on the firebase database , but when i store it it crashes
– Waseem Ha
Nov 5 at 18:35












If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question or as a comment and indicate the line at which line of code it occurs.
– Alex Mamo
Nov 5 at 18:37






If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question or as a comment and indicate the line at which line of code it occurs.
– Alex Mamo
Nov 5 at 18:37














Hi Johnny! Is there everything alright, have you solved the issue?
– Alex Mamo
Nov 6 at 7:42






Hi Johnny! Is there everything alright, have you solved the issue?
– Alex Mamo
Nov 6 at 7:42














up vote
0
down vote













As you wrote NullPointerException rises in line



String name = dataSnapshot.child("name").getValue().toString();


The error message says you try to invoke .toString() on an null object.
You should save the result from String name = dataSnapshot.child("name").getValue() and check if it's not null before calling .toSring() method.






share|improve this answer










New contributor




JohnnyJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Error appers inString image = dataSnapshot.child("image").getValue().toString();
    – Waseem Ha
    Nov 5 at 18:54












  • ok, sorry for this ;) .... but checking the value before calling the method could help nevertheless
    – JohnnyJ
    Nov 5 at 18:57

















up vote
0
down vote













As you wrote NullPointerException rises in line



String name = dataSnapshot.child("name").getValue().toString();


The error message says you try to invoke .toString() on an null object.
You should save the result from String name = dataSnapshot.child("name").getValue() and check if it's not null before calling .toSring() method.






share|improve this answer










New contributor




JohnnyJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Error appers inString image = dataSnapshot.child("image").getValue().toString();
    – Waseem Ha
    Nov 5 at 18:54












  • ok, sorry for this ;) .... but checking the value before calling the method could help nevertheless
    – JohnnyJ
    Nov 5 at 18:57















up vote
0
down vote










up vote
0
down vote









As you wrote NullPointerException rises in line



String name = dataSnapshot.child("name").getValue().toString();


The error message says you try to invoke .toString() on an null object.
You should save the result from String name = dataSnapshot.child("name").getValue() and check if it's not null before calling .toSring() method.






share|improve this answer










New contributor




JohnnyJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









As you wrote NullPointerException rises in line



String name = dataSnapshot.child("name").getValue().toString();


The error message says you try to invoke .toString() on an null object.
You should save the result from String name = dataSnapshot.child("name").getValue() and check if it's not null before calling .toSring() method.







share|improve this answer










New contributor




JohnnyJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer








edited Nov 6 at 5:46









Manhar

5612




5612






New contributor




JohnnyJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered Nov 5 at 18:52









JohnnyJ

1




1




New contributor




JohnnyJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





JohnnyJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






JohnnyJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Error appers inString image = dataSnapshot.child("image").getValue().toString();
    – Waseem Ha
    Nov 5 at 18:54












  • ok, sorry for this ;) .... but checking the value before calling the method could help nevertheless
    – JohnnyJ
    Nov 5 at 18:57




















  • Error appers inString image = dataSnapshot.child("image").getValue().toString();
    – Waseem Ha
    Nov 5 at 18:54












  • ok, sorry for this ;) .... but checking the value before calling the method could help nevertheless
    – JohnnyJ
    Nov 5 at 18:57


















Error appers inString image = dataSnapshot.child("image").getValue().toString();
– Waseem Ha
Nov 5 at 18:54






Error appers inString image = dataSnapshot.child("image").getValue().toString();
– Waseem Ha
Nov 5 at 18:54














ok, sorry for this ;) .... but checking the value before calling the method could help nevertheless
– JohnnyJ
Nov 5 at 18:57






ok, sorry for this ;) .... but checking the value before calling the method could help nevertheless
– JohnnyJ
Nov 5 at 18:57




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159790%2fandroid-firebase-database-java-lang-string-java-lang-object-tostring-on-a-nul%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings