1

On running the code below, I get this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference

I am getting the error on the line dataRef.child(dbs).addValueEventListener(new ValueEventListener(). I am not sure what is the problem.

public class UTFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { SharedPreferences prefs = getActivity().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String savedCollname= prefs.getString("collname", null); dataRef = FirebaseDatabase.getInstance().getReference().child(savedCollname); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UTFragment utFragment = new UTFragment(); UTFragment.MyAsync myTask = utFragment.new MyAsync(dbs,data); myTask.execute(); } class MyAsync extends AsyncTask<Void, Void, Void> { String dbs; public MyAsync(String dbs){ this.dbs = dbs; } @Override protected void onProgressUpdate(Void... values) { dataRef.child(dbs).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { //do something }} 
8
  • 1
    The error means that dataRef hasn't been initialized yet when you call child(...) on it. So most likely your fragment's onCreateView hasn't been called when the task updates its progress. Commented Jan 13, 2018 at 16:09
  • Don't get me wrong, but this a weird scenario. May I ask: Why are you creating a second Fragment inside that same Fragment? And why are you using an AsyncTask to load Firebase data? Commented Jan 13, 2018 at 16:13
  • To show progress bar I have used async task. There are lot more things going on I have just shown the relevant code. @RosárioPereiraFernandes Commented Jan 13, 2018 at 16:20
  • 1
    Understood... As Frank mentioned, the AsyncTask is calling child() in dataRef before it has been initialized. So I recommend initializing the DatabaseReference in your AsyncTask. Commented Jan 13, 2018 at 16:22
  • When I initialise DatabaseReference in AsyncTask it gives me the error : java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.support.v4.app.FragmentActivity.getSharedPreferences(java.lang.String, int)' on a null object reference @RosárioPereiraFernandes Commented Jan 13, 2018 at 16:29

2 Answers 2

1

The problem is when you instantiate the fragment for the second time. You don't need a second fragment to be able to use the AsyncTask. You can simply call for the AsyncTask:

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); MyAsync myTask = new MyAsync(dbs,data); myTask.execute(); } 
Sign up to request clarification or add additional context in comments.

Comments

1

The error says very clearly what the problem is. Your field dataRef isn't initialized yet by the time you are calling child() method on it, with other words has the value of null.

To solve this, onCreateView() method must be called before your task updates its progress.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.