82198

Getting 'Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()'

Question:

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 <a href="https://stackoverflow.com/a/36236657/6144372" rel="nofollow">here</a>.

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 <strong>why am I</strong> getting this error?

Answer1:

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 <a href="https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/DataSnapshot.html#getValue--" rel="nofollow">DataSnapshot</a>

... 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 <a href="https://github.com/firebase/geofire" rel="nofollow">GeoFire</a>.

Recommend

  • wldeploy on all files in deployment folder
  • How to get a COUNT of NON-NULL members in a set with some conditions?
  • Invalid permission from Lambda to MongoDB in EC2
  • Convert log4j 1.x to log4j 2.x custom layout appender
  • How to avoid the throttle limit for a particular call of LinkedIn API in iPhone?
  • Fetching all Bill Payment Check from Quickbooks database
  • Merging data from multiple xml files without duplicates using XSLT
  • How to test API Gateway methods with custom authorizer and empty $context.authorizer.* variables?
  • Need to sense a keystroke in VBscript to end a loop
  • PostgreSql - unique rows, independent of order
  • Cursor inside Trigger not working
  • Uploading to Amazon S3 via ng-file-upload; Error 400
  • Quickbooks WebConnector How I can add invoice with Item Location/Site
  • org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no decla
  • Amazon S3 Signature Does Not Match - AWS SDK Java
  • Complex query on Firebase for Android
  • Changing a sql query using join instead of a subselect
  • what is the difference between getPeeledObjectId() and getObjectId() of Ref Object?
  • AWS S3 “404 Not Found”
  • webRequest API: How to get the requestId of a new request?
  • insert new inner object into existing object mongodb using php
  • Azure Stack Table Storage: Value for HTTP headers not in correct format
  • Clear elements from ConcurrentHashMap while iterating
  • window.requestAnimationFrame silently fails to invoke callback, but generates a valid requestID, whe
  • Set columns of JTable in specified order after creation
  • Custom Map XmlAdapter
  • Firebase , classmaper : no setter/field found for 'UserTwo' on class 'myclass'
  • How do I calculate a grouped z score in R using dplyr?
  • Send Raw Email (with attachment) to Multiple Recipients
  • firebase, how to update data at a key
  • ilmerge with a PFX file
  • Why value captured by reference in lambda is broken? [duplicate]
  • How to set my toolbar fixed while scrolling android
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Why joiner is not used after Sequence generator or Update statergy
  • apache spark aggregate function using min value
  • Recursive/Hierarchical Query Using Postgres
  • Sorting a 2D array using the second column C++
  • UserPrincipal.Current returns apppool on IIS