I am trying to make an adapter that will show to listview real time database content.
I am using my own class and I want the line to show limited things from class.
I get FATAL EXCEPTION: main when I try to run.
---EDIT---
I noticed that what's happening is that the auth uid returns null while user is not null.
auth that returns null with adapter:
private void prepareRequests() {
auth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
user = firebaseAuth.getCurrentUser();
if (user == null) {
startActivity(new Intent(MainActivity.this, OnStart.class));
finish();
} else {
databaseRef = database.getReference("requests").child(user.getUid());
}
}
});
FirebaseListAdapter<Request> requestAdapter;
try {
requestAdapter = new FirebaseListAdapter<Request>(
this,
Request.class,
android.R.layout.two_line_list_item,
databaseRef) {
@Override
protected void populateView(View v, Request model, int position) {
((TextView) v.findViewById(R.id.requestName)).setText(model.getRequestName());
((TextView) v.findViewById(R.id.handled)).setText(model.getHandled());
}
};
} catch (NullPointerException e){
Request request = new Request("Make new request here!", "false", "no one", "None", 0, 0, "No Message");
databaseRef.child("Request").setValue(request);
requestAdapter = new FirebaseListAdapter<Request>(
this,
Request.class,
android.R.layout.two_line_list_item,
databaseRef
) {
@Override
protected void populateView(View v, Request model, int position) {
((TextView) v.findViewById(R.id.requestName)).setText(model.getRequestName());
((TextView) v.findViewById(R.id.handled)).setText(model.getHandled());
}
};
}
ListView listView = (ListView) findViewById(R.id.list_of_req);
listView.setAdapter(requestAdapter);
}
class:
public class Request {
private String requestname;
private String handled;
private String type;
private String handler;
private String message;
private double lat;
private double longt;
public Request() {
}
public Request(String requestname, String handled, String handler, String type, double lat, double longt, String message) {
this.requestname = requestname;
this.handled = handled;
this.lat = lat;
this.longt = longt;
this.type = type;
this.handler = handler;
this.message = message;
}
public String getRequestName(){
return requestname;
}
public void setRequestName(String requestname){
this.requestname = requestname;
}
public double getLat () {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLongt() {
return longt;
}
public void setLongt(double longt) {
this.longt = longt;
}
public String getHandled() {
return handled;
}
public void setHandled(String handled){
this.handled = handled;
}
public String getType(){
return type;
}
public void setType (String type) {
this.type = type;
}
public String getHandler() {
return handler;
}
public void setHandler(String handler){
this.handler = handler;
}
public String getMessage(){
return message;
}
public void setMessage(String message){
this.message = message;
}
}
request layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/requestName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textSize="18sp" />
<TextView
android:id="@+id/handled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp" />
</LinearLayout>
logcat:
--------- beginning of crash 07-12 18:30:15.773 2689-2689/com.newworldgrip.lostandfound.lostandfound E/AndroidRuntime: FATAL EXCEPTION: main Process: com.newworldgrip.lostandfound.lostandfound, PID: 2689 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.newworldgrip.lostandfound.lostandfound/com.newworldgrip.lostandfound.lostandfound.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6540) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference at com.firebase.ui.database.FirebaseArray.(FirebaseArray.java:43) at com.firebase.ui.database.FirebaseListAdapter.(FirebaseListAdapter.java:93) at com.newworldgrip.lostandfound.lostandfound.MainActivity$1.(MainActivity.java:0) at com.newworldgrip.lostandfound.lostandfound.MainActivity.onCreate(MainActivity.java:56) at android.app.Activity.performCreate(Activity.java:6980) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6540) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 07-12 18:30:15.773 2689-2689/com.newworldgrip.lostandfound.lostandfound E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.newworldgrip.lostandfound.lostandfound/com.newworldgrip.lostandfound.lostandfound.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6540) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference at com.firebase.ui.database.FirebaseArray.(FirebaseArray.java:43) at com.firebase.ui.database.FirebaseListAdapter.(FirebaseListAdapter.java:93) at com.newworldgrip.lostandfound.lostandfound.MainActivity$1.(MainActivity.java:0) at com.newworldgrip.lostandfound.lostandfound.MainActivity.onCreate(MainActivity.java:56) at android.app.Activity.performCreate(Activity.java:6980) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6540) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) any help would be appreciated.
I found the problem, Android is asynchronous which means it doesn't wait on auth state listener and jumps right into database analyzing.
All I had to do is to put the database and list adapter inside the listener.