Android studio is forced to shut down 'java.lang.Stringjava.lang.Object.toString()' on a null object reference

Asked 2 years ago, Updated 2 years ago, 104 views

When you press a button on a map map, an error occurs. I used the thread I don't know which part is wrong. I'm just copying what someone else did Please help me because I am not good at interpreting logcat.

06-20 16:55:53.125 15979-15979/com.ossw.taxiapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ossw.taxiapp, PID: 15979
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at com.ossw.taxiapp.CustomerMapActivity$6.onDataChange(CustomerMapActivity.java:289)
        at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@19.3.0:179)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.3.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.3.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.3.0: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:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

//This is an error section

    private void getDriverInfo() {
        mDriverInfo.setVisibility(View.VISIBLE);
        DatabaseReference mCustomerDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child(driverFoundID);
        mCustomerDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists() && dataSnapshot.getChildrenCount() > 0) {
                    if (dataSnapshot.child("name") != null) {
                        d_name.setText(dataSnapshot.child("name").getValue().toString());
                    }
                    if (dataSnapshot.child("phone") != null) {
                        d_phone.setText(dataSnapshot.child("phone").getValue().toString());
                    }
                    if (dataSnapshot.child("car") != null) {
                        d_car.setText(dataSnapshot.child("car").getValue().toString());
                    }
                    if (dataSnapshot.child("carNum") != null) {
                        d_callNum.setText(dataSnapshot.child("callNum").getValue().toString());
                    }
                    if (dataSnapshot.child("profileImageUrl").getValue() != null) {
                        Glide.with(getApplication()).load(dataSnapshot.child("profileImageUrl").getValue().toString()).into(d_profileimg);
                    }

                    int ratingSum = 0;
                    float ratingsTotal = 0;
                    float ratingsAvg = 0;
                    for (DataSnapshot child : dataSnapshot.child("rating").getChildren()) {
                        ratingSum = ratingSum + Integer.valueOf(child.getValue().toString());
                        ratingsTotal++;
                    }
                    if (ratingsTotal != 0) {
                        ratingsAvg = ratingSum / ratingsTotal;
                        mRatingBar.setRating(ratingsAvg);
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

android android-studio java error nullpointerexception

2022-09-20 21:51

1 Answers

if (dataSnapshot.child("carNum") != null) {
    d_callNum.setText(dataSnapshot.child("callNum").getValue().toString());
}

I think it's because of the typo in this part.

If you look at Logcat, you can see that it called the toString() method in the Null Object Reference, which seems to have happened here.

It's late, but I hope it helps.


2022-09-20 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.