title: childEventListerer on null object reference, Firebase database
body: <p>After trying to use FirebaseRecyclerView and getting stuck on an issue which caused populateViewHolder() not to be called, I attempted an older method in which to retrieve values for a RecyclerView within the Firebase database.
  However I am getting this error:</p>
  
  <blockquote>
    <p>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chris.storyboard/com.example.chris.storyboard.StoryPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.DatabaseReference.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                                                                      at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6776)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.DatabaseReference.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
                                                                                      at com.example.chris.storyboard.FirebaseHelper.retrieve(FirebaseHelper.java:51)
                                                                                      at com.example.chris.storyboard.StoryPage.onCreate(StoryPage.java:98)
                                                                                      at android.app.Activity.performCreate(Activity.java:6955)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                                                                                      at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                      at android.os.Looper.loop(Looper.java:154) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) </p>
  </blockquote>
  
  <p>I am having trouble figuring out why it's seeing the object reference as 'null' as I've used a similar method in another class that is working fine.</p>
  
  <p>Here is the rest of my code:</p>
  
  <p>FirebaseHelper.class</p>
  
  <pre><code>public class FirebaseHelper{
  DatabaseReference db = FirebaseDatabase.getInstance().getReference(); 
  Boolean isSaved = null; 
  ArrayList stories = new ArrayList&lt;&gt;(); 
  FirebaseAuth firebaseAuth;
  
  public FirebaseHelper(DatabaseReference db) {
      this.db = db;
  }
  
  public Boolean Saved(Stories stories) {
      if (stories == null) {
          isSaved = false;
  
      } else {
          try {
              db.child("Stories").push().setValue(stories);
              isSaved = true;
  
          } catch (DatabaseException e) {
              e.printStackTrace();
              isSaved = false;
          }
      }
      return isSaved;
  }
  
  public ArrayList&lt;String&gt; retrieve() {
  
      db.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
              Log.d("Testing: ", "Child added!");
             // fetchData(dataSnapshot);
          }
  
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
              Log.d("Testing: ", "Child changed!");
            //  fetchData(dataSnapshot);
          }
  
  
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
  
          }
  
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  
      return stories;
  }
  
  
  private void fetchData(DataSnapshot datasnapshot) {
      stories.clear();
          String storyTitle = datasnapshot.child("storyTitle").getValue(String.class);
          stories.add(storyTitle);
  
      }
  
  
  }
  </code></pre>
  
  <p>StoriesAdapter.class</p>
  
  <pre><code>public class StoriesAdapter extends RecyclerView.Adapter&lt;StoriesHolder&gt; {
  
  Context c;
  ArrayList&lt;String&gt; stories;
  
  public StoriesAdapter(Context c, ArrayList&lt;String&gt; stories) {
      this.c = c;
      this.stories = stories;
  }
  
  @Override
  public StoriesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v= LayoutInflater.from(c).inflate(R.layout.customlistview,parent,false);
  
      return new StoriesHolder(v);
  }
  
  @Override
  public void onBindViewHolder(StoriesHolder holder, int position) {
      holder.titletxtview.setText(stories.get(position));
      holder.cattxtview.setText(stories.get(position));
      holder.createdbytxtview.setText(stories.get(position));
  
  }
  
  @Override
  public int getItemCount() {
      return stories.size();
  }
  </code></pre>
  
  <p>StoriesHolder.class</p>
  
  <pre><code>public class StoriesHolder extends RecyclerView.ViewHolder {
  
  TextView titletxtview;
  TextView cattxtview;
  TextView createdbytxtview;
  
  public StoriesHolder(View itemView) {
      super(itemView);
  
      titletxtview = (TextView) itemView.findViewById(R.id.titleTxtView);
      cattxtview = (TextView) itemView.findViewById(R.id.cattxtview);
      createdbytxtview = (TextView) itemView.findViewById(R.id.createdbytxtview);
  }
  </code></pre>
  
  <p>StoryPage.class</p>
  
  <pre><code>public class StoryPage extends AppCompatActivity {
  
  Button homePage;
  Button CreateStory;
  Button logOut;
  RecyclerView rv;
  DatabaseReference db;
  FirebaseHelper helper;
  StoriesAdapter adapter;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.storypage);
      this.setTitle("Browse stories");
  
      homePage = (Button) findViewById(R.id.homeBtn);
      CreateStory = (Button) findViewById(R.id.CreateStoryBtn);
      logOut = (Button) findViewById(R.id.logOutbtn);
      rv = (RecyclerView) findViewById(R.id.storyRecyclerView);
      rv.setLayoutManager(new LinearLayoutManager(this));
      helper = new FirebaseHelper(db);
  
      db = FirebaseDatabase.getInstance().getReference().child("Stories");
  
  
      homePage.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startActivity(new Intent(StoryPage.this, Home.class));
          }
      });
  
      CreateStory.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startActivity(new Intent(StoryPage.this, CreateStory.class));
          }
      });
  
      homePage.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
              FirebaseAuth.getInstance().signOut();
              startActivity(new Intent(StoryPage.this, MainActivity.class));
          }
      });
  
  
      Query query = FirebaseDatabase.getInstance()
              .getReference()
              .child("Stories")
              .limitToLast(10);
  
      FirebaseRecyclerOptions&lt;Stories&gt; options =
              new FirebaseRecyclerOptions.Builder&lt;Stories&gt;()
                      .setQuery(query, Stories.class)
                      .build();
  
      adapter = new StoriesAdapter(this, helper.retrieve());
      rv.setAdapter(adapter);
  
  }
  </code></pre>
  
  <p>the 'failed to invoke on null object reference' error is displaying the following lines</p>
  
  <pre><code>adapter = new StoriesAdapter(this, helper.retrieve());
  </code></pre>
  
  <p>and</p>
  
  <pre><code> db.addChildEventListener(new ChildEventListener() {
  </code></pre>
  
  <p>Starting to feel like displaying RecyclerView just doesn't agree with me!</p>
  
  <p>Any help is greatly appreciated! Cheers</p>