Getting 'Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference' even when the reference is not null

ⅰ亾dé卋堺 提交于 2019-12-25 08:28:51

问题


I'm using FirebaseUI in my app and FirebaseRecyclerAdapter to be more specific.

I'm fetching data from my FirebaseDatabase reference using the method given here.

Here's my code:

private void attachRecyclerViewAdapter() {
        Query lastFifty = mDatabase.child(rID).limitToFirst(50);
        mRecyclerViewAdapter = new FirebaseRecyclerAdapter<AModelClass, AModelClass.ViewHolder>(
                AModelClass.class, R.layout.a_player_layout, APlayersModelClass.ViewHolder.class, lastFifty) {

            @Override
            protected void populateViewHolder(AModelClass.ViewHolder viewHolder, AsModelClass model, int position) {

                String key = this.getRef(position).getKey();
                aReference.child(requestID).child(key).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.getValue() != null) {

                            Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
                            pA = map.get("pName");
                            uA = map.get("pUrl"); 
                            // error on line below
                            String cLatS = map.get("cLat").trim();
                            currentLtAU = Double.parseDouble(cLatS);
                            String cLngS = map.get("cLng").trim();
                            currentLnAU = Double.parseDouble(cLngS);

                            viewHolder.setPName(pA);
                            viewHolder.setPUrl(uA);
                            viewHolder.setCurrentLatAU(String.valueOf(currentLtAU));
                            viewHolder.setCurrentLngAU(String.valueOf(currentLnAU));

                        } else {
                            Snackbar snackbar = Snackbar
                                    .make(coordinatorLayout, "Some error occurred. Please retry!", Snackbar.LENGTH_SHORT);
                            snackbar.show();
                            onBackPressed();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            }
        };

        aList.setAdapter(mRecyclerViewAdapter);
    }

Here's how database structure looks like:

-app
  -requestID
    -uniqueKey1
      -key: value
      -key: value
      -cLat: value
    -uniqueKey2
      -key: value
      -key: value
      -cLat: value

As you can see in the database structure above, reference has 2 keys and all the data is getting fetched and shown in the RecyclerView but as soon as one key is removed from the reference, the app crashes giving this error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference on the line specified above.

The reference has one key still and all the same fields which I'm fetching are in this one key too. Then why am I getting this error?


回答1:


if this causes the error:

Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();
String cLatS = map.get("cLat").trim();

the reason is most likely, that the snapshot has no child cLat.

you should check with .hasChild("cLat") - see DataSnapshot

... or prevent the attempted .trim(null) alike:

if(map.get("cLat") != null) {
    String cLatS = map.get("cLat").trim();
    currentLtAU = Double.parseDouble(cLatS);
}

while for this kind of application, you might want to check out Firebase GeoFire.



来源:https://stackoverflow.com/questions/41921120/getting-attempt-to-invoke-virtual-method-java-lang-string-java-lang-string-tri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!