本文介绍了获取'试图调用虚方法'java.lang.String null对象引用java.lang.String.trim()'即使引用不为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在应用程序中使用了 FirebaseUI ,而 FirebaseRecyclerAdapter 更具体。 使用给定的方法从我的 FirebaseDatabase 引用中获取数据 以下是我的代码: 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 $ b $ 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); //在$ b $下面的错误字符串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,发生了一些错误,请重试!,Snackbar.LENGTH_SHORT); snackbar.show(); onBackPressed(); $ b @Override public void onCancelled(DatabaseError databaseError){ }); } }; aList.setAdapter(mRecyclerViewAdapter); $ b $ p $ b 以下是数据库结构的样子: -app -requestID -uniqueKey1 -key:value -key:value - cLat:value -uniqueKey2 -key:value -key:value -cLat:value 正如你在上面的数据库结构中看到的,引用有2 键,所有的数据都被获取并显示在 RecyclerView 但只要从引用中删除了一个键,应用程序就会崩溃,出现以下错误: java.lang.NullPointerException:试图调用虚拟方法'java.lang.String java.lang.String.trim()'在上面指定的行的空对象引用上。 引用有一个键仍然和我提取的所有相同的字段也在这一个关键。然后为什么我得到这个错误? 解决方案如果这样会导致错误: Map< String,String> map =(Map< String,String>)dataSnapshot.getValue(); String cLatS = map.get(cLat)。trim(); 原因很可能是快照没有小孩 cLat 你应该检查 .hasChild(cLat) - 参见 DataSnapshot ...或者阻止尝试 .trim(null): if(map.get(cLat)!= null){ String cLatS = map.get(cLat)。trim(); currentLtAU = Double.parseDouble(cLatS); } 对于这类应用程序,您可能需要查看Firebase GeoFire 。 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: valueAs 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? 解决方案 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. 这篇关于获取'试图调用虚方法'java.lang.String null对象引用java.lang.String.trim()'即使引用不为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-27 10:06