无法在android studio中的child()中为参数'pathString'传递null [重复]

Posted

技术标签:

【中文标题】无法在android studio中的child()中为参数\'pathString\'传递null [重复]【英文标题】:Can't pass null for argument 'pathString' in child() in android studio [duplicate]无法在android studio中的child()中为参数'pathString'传递null [重复] 【发布时间】:2020-01-04 22:00:15 【问题描述】:

当我点击标记时,我在地图中添加标记应该是新活动,但这段代码有一个我无法解决的问题

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 ComponentInfocom.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】:

这是因为它是 @NonNull 带注释的参数。为了避免它,您可以简单地添加:-

   String.valueOf(childName);

在你的情况下:-

   lokasi.child(String.valueOf(locasid))

因此,即使它为 null,它也会使用 "null" 而不是 null

【讨论】:

该代码到底放在哪里?? 我已添加到我的答案中 现在我收到新错误尝试在空对象引用上调用虚拟方法'java.lang.String com.example.myapplication.Lokasi.LocationDB.getImage()' 为新的崩溃创建一个新问题!

以上是关于无法在android studio中的child()中为参数'pathString'传递null [重复]的主要内容,如果未能解决你的问题,请参考以下文章

无法按照 Google“入门”页面中的说明在 Android Studio 中添加地图; Android Studio 报错

无法解析 Android Studio 中的依赖项

无法在android studio中的android模拟器中访问互联网

Android Studio 中的新活动无法启动

本机代码中的断点无法在 Android Studio 上命中 [重复]

无法使用 setsmallIcon 在 android studio 中更改图标