'Fatal error in onDataChange java.lang.String java.lang.Object.toString()' on a null object reference
This is my code which should check the order state in the firebase database and display a message for the user. In this line,
String shippingstate = snapshot.child("state").getValue().toString();
I got on a null object reference fatal error and the app was force closed. I verified all the child values in the database and their match but still get the error. Can you help me solve it, please? thanks
private void checkorderstate()
{
DatabaseReference orderref = FirebaseDatabase.getInstance().getReference().child("orders");
orderref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists())
{
**String shippingstate = snapshot.child("state").getValue().toString();**
String username = snapshot.child("name").getValue().toString();
if (shippingstate.equals("shipped"))
{
txttotalamount.setText("Dear "+ username+"\n order is shipped successfully");
recyclerView.setVisibility(View.GONE);
txtmsg1.setVisibility(View.VISIBLE);
txtmsg1.setText("Dear "+username+"\n Your order is completed and shipped successfully");
nextproccessbtn.setVisibility(View.GONE);
Toast.makeText(CartActivity.this, "You can put new order after your current order get shipped", Toast.LENGTH_LONG).show();
}
else if (shippingstate.equals("not shipped"))
{
txttotalamount.setText("Dear "+ username+"Order State: not shipped");
recyclerView.setVisibility(View.GONE);
txtmsg1.setText("Dear "+username+"\n your order under proccess, please wait the order shipped massage. thank you");
nextproccessbtn.setVisibility(View.GONE);
Toast.makeText(CartActivity.this, "You can put new order after your current order get shipped", Toast.LENGTH_LONG).show();
}
}
}
Debugger Statement:
E/AndroidRuntime: FATAL EXCEPTION: main Process: net.lazared.myamaze, PID: 3304 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at net.lazared.myamaze.CartActivity$3.onDataChange(CartActivity.java:189) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75) at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63) at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
