title: notifyDataSetChanged() causes NullPointerException when changing listview data which is retrieved from firebase
body: <p>I am trying to set a <code>ListView</code> inside my <code>Fragment</code>. I am getting data from Firebase which i set to my <code>ListView</code>. In my Firebase database I have a parent node <code>patients</code> which has child nodes like <code>patient1</code>, <code>patient2</code> etc. and every item in my <code>ListView</code> represents a single patient. My <code>ListView</code> will always have a maximum of 2 items as I am setting my list with the top two patients from Firebase database. Whenever a new patient is added or deleted I want my <code>listView</code> to get updated with new patient data. But when a new patient is added my app gets crashed.</p>
  
  <p><strong>Logcat exception :-</strong></p>
  
  <pre><code>01-10 14:20:20.966 17863-17863/com.abc.tempapp/AndroidRuntime: FATAL EXCEPTION: main
  01-10 14:20:20.966 17863-17863/com.abc.tempapp E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
  </code></pre>
  
  <p><strong>Code :-</strong></p>
  
  <pre><code> @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
      {
         ...
         setListView();
         ...
      }
  
   public void setListView()
      {
          Firebase.setAndroidContext(context);
          firebaseData = new Firebase("https://xyz.firebaseio.com");
          firebaseData.child("patients").orderByChild("appointment-id").limitToFirst(2).addListenerForSingleValueEvent(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  patientList.clear();
                  for (DataSnapshot tempSnapshot : dataSnapshot.getChildren()) {
                          int tempId = Integer.parseInt(tempSnapshot.child("appointment-id").getValue().toString());
                      String tempContact = tempSnapshot.child("number").getValue().toString();
                      String tempName = tempSnapshot.child("name").getValue().toString();
                      long tempTime = Long.parseLong(tempSnapshot.child("arrival-time").getValue().toString());
                      Patient patient = new Patient(tempId, tempContact, tempName, epochToDate(tempTime));
                      patientList.add(patient);
                  }
                  if (myAdapter == null) {
                      myAdapter = new MyAdapter(context, patientList);
                      listView.setAdapter(myAdapter);
                      firebaseData.child("patients").addChildEventListener(new ChildEventListener() {
                          @Override
                          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                              dataChanged();
  
                          }
  
                          @Override
                          public void onChildRemoved(DataSnapshot dataSnapshot) {
                              dataChanged();
  
                          }
  
                          @Override
                          public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
  
                          @Override
                          public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
  
                          @Override
                          public void onCancelled(FirebaseError firebaseError) {}
                      });
                  }
              }
  
              @Override
              public void onCancelled(FirebaseError firebaseError) {}
          });
      }
  
   synchronized public void  dataChanged()
      {
          Firebase.setAndroidContext(context);
          firebaseData = new Firebase("https://xyz.firebaseio.com");
          firebaseData.child("patients").orderByChild("appointment-id").limitToFirst(2).addListenerForSingleValueEvent(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  patientList.clear();
                      for (DataSnapshot tempSnapshot : dataSnapshot.getChildren()) {
                              int tempId = Integer.parseInt(tempSnapshot.child("appointment-id").getValue().toString());
                              String tempContact = tempSnapshot.child("number").getValue().toString();
                              String tempName = tempSnapshot.child("name").getValue().toString();
                              long tempTime = Long.parseLong(tempSnapshot.child("arrival-time").getValue().toString());
                              Patient patient = new Patient(tempId, tempContact, tempName, epochToDate(tempTime));
                              patientList.add(patient);
                      }
                  myAdapter.notifyDataSetChanged();
                  }
              @Override
              public void onCancelled(FirebaseError firebaseError) {}
          });
  
      }
  </code></pre>
  
  <p>I don't know what I am missing here. It might be a very fundamental thing that I have got wrong, hope someone corrects me.</p>