无法在Android Studio中的child()中为参数'pathString'传递null

时间:2019-09-01 15:18:31

标签: java android firebase-realtime-database nullpointerexception

当我单击标记时,应该在地图上添加标记,这应该是新的活动,但是此代码存在我无法解决的问题

public class CoffeeShopDetail extends AppCompatActivity {

    TextView DetailText;
    TextView DetailTipe;
    TextView DetailAsal;
    ImageView DetailImage;
    String LokasiId = "";
    FirebaseDatabase database;
    DatabaseReference lokasi;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_loc);

        database = FirebaseDatabase.getInstance();
        lokasi = database.getReference().child("Lokasi");

        DetailText = (TextView) findViewById(R.id.detail_shop);
        DetailImage = (ImageView) findViewById(R.id.detail_image1);
        DetailTipe = (TextView) findViewById(R.id.descCS);
        DetailAsal = (TextView) findViewById(R.id.tandatanya);

        if (getIntent() != null)
            LokasiId = getIntent().getStringExtra("LokasiId");
        if ("LokasiId".equals(true)) {

            return;

        }
        getDetailLokasi(LokasiId);
    }

    //-----1-----
    private void getDetailLokasi(String LokasiId) {

        lokasi.child(LokasiId).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                final LocationDB LocationDB = dataSnapshot.getValue(LocationDB.class);
                Picasso.get().load(LocationDB.getImage()).into(DetailImage);
                DetailText.setText(LocationDB.getNama_tempat());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

}

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 23166
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.CoffeeShopDetail}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3260)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7319)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
     Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
        at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database@@18.0.1:101)
        at com.example.myapplication.CoffeeShopDetail.getDetailLokasi(CoffeeShopDetail.java:71)
        at com.example.myapplication.CoffeeShopDetail.onCreate(CoffeeShopDetail.java:62)
        at android.app.Activity.performCreate(Activity.java:7783)
        at android.app.Activity.performCreate(Activity.java:7772)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3235)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3396) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7319) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)

1 个答案:

答案 0 :(得分:0)

这是因为它是一个带有@NonNull注释的参数。为了避免这种情况,您只需添加:-

   String.valueOf(childName);

在您的情况下:-

   lokasi.child(String.valueOf(locasid))

因此,即使它为null,它也会以"null"而不是null的形式出现