Judy T Raj Judy T Raj - 15 days ago 7
Android Question

Android/Firebase: onDataChange() for query returns null pointer exception

I'm trying to retreive an object from my firebase databse by its id. But my app crashes with a null pointer exception. Please help me out. I'm new to Firebase.
Also I'd like to know how I can find out if the search is unsuccessful.
Here's my database contents

Here's my code for data retreival. The Query class belongs to com.google.firebase and not to com.firebase.client.

@Override
public void onClick(View view) {
key = scanContent;
Query q = dbRef.child("idno").equalTo(key);
if (key != null) {
q.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
Item i = dataSnapshot.getValue(Item.class);
id.setText(scanContent);
nam.setText(i.getName());
brand.setText(i.getBrand());
cost.setText(i.getCost());
sell.setText("Add to list of sold items?");
}

@Override
public void onCancelled(DatabaseError databaseError) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(databaseError.getMessage())
.setTitle("Error!")
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
});

} else {
search.setError("Enter a valid value");
}
}


Here are the contents of my logcat.

09-24 13:32:26.332 9228-9228/com.strokx.user.stockmanager W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40c03a68)
09-24 13:32:26.352 9228-9228/com.strokx.user.stockmanager E/UncaughtException: java.lang.NullPointerException
at com.strokx.user.stockmanager.SearchFragment$2$1.onDataChange(SearchFragment.java:106)
at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzaie.zza(Unknown Source)
at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
09-24 13:32:26.572 9228-9228/com.strokx.user.stockmanager D/FirebaseCrashApiImpl: throwable java.lang.NullPointerException
09-24 13:32:26.672 9228-9228/com.strokx.user.stockmanager E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.strokx.user.stockmanager.SearchFragment$2$1.onDataChange(SearchFragment.java:106)
at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzaie.zza(Unknown Source)
at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)

Answer

Fixed it. My query was wrong. Although fixing the query still wouldn't make the listview display the retrieved result. I used the firebase ui list adapter to fix that. I've posted the correct code below, if it helps anyone. Thanks everyone.

 dbRef= FirebaseDatabase.getInstance().getReferenceFromUrl("https://stockmanager-142503.firebaseio.com/Items");


        imgbtn.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View view) {
                                          key = scanContent;
                                          Query q = dbRef.orderByChild("idno").equalTo(key);
                                          FirebaseListAdapter<Item> adapter =new FirebaseListAdapter<Item>(getActivity(),Item.class,R.layout.rowitem,q) {
                                              @Override
                                              protected void populateView(View v, Item item, int position) {
                                                  TextView text1=(TextView)v.findViewById(R.id.text1);
                                                  TextView text2=(TextView)v.findViewById(R.id.text2);
                                                  TextView text3=(TextView)v.findViewById(R.id.text3);
                                                  TextView text4=(TextView)v.findViewById(R.id.text4);
                                                  TextView text5=(TextView)v.findViewById(R.id.text5);
                                                  text1.setText(item.getIdno());
                                                  text2.setText(item.getName());
                                                  text3.setText(item.getBrand());
                                                  text4.setText(item.getCost());
                                                  text5.setText(item.getDate());
                                                  solditem=item;
                                              }
                                          };
                                          sell.setText("Add this result to Sold Items List?");
                                      sList.setAdapter(adapter);
                                      }
                                  });
Comments