title: Reading data from a firebase database snapshot
body: <p>So, I'm trying to take a snapshot of my Database and add the items with its child's in array-strings.</p>
  
  <p><img src="https://i.stack.imgur.com/j5jCo.png" alt="My database looks like this"></p>
  
  <p>My main activity looks like this</p>
  
  <pre class="lang-java prettyprint-override"><code>    private Button mbutton;
      private FirebaseDatabase mFirebaseDatabase;
      private DatabaseReference myRef;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_advanced_search);
          mbutton = findViewById(R.id.button_search);
          mFirebaseDatabase = FirebaseDatabase.getInstance();
          myRef = mFirebaseDatabase.getReference();
          mbutton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  goListener();
              }
          });
      }
      private void goListener() {
          myRef.addListenerForSingleValueEvent(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot snapshot) {
                  dataQuery(snapshot);
              }
              @Override
              public void onCancelled(@NonNull DatabaseError databaseError) {
              }
          });
      }
      private void dataQuery(DataSnapshot snapshot) {
          for (int y = 0; y !=2; y++) {
              for (DataSnapshot ds : snapshot.getChildren()) {
                  AdvancedQuery findInfo = new AdvancedQuery();
                  findInfo.setName(ds.child(String.valueOf(y)).getValue(AdvancedQuery.class).getName());
                  findInfo.setIngredients(ds.child(String.valueOf(y)).getValue(AdvancedQuery.class).getIngredients());
              }
          }
      }
  }
  </code></pre>
  
  <p>And my set and get java file looks like this</p>
  
  <pre class="lang-java prettyprint-override"><code>public class AdvancedQuery {
      public String name, ingredients;
      public AdvancedQuery(){ }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public String getIngredients() {
          return ingredients;
      }
      public void setIngredients(String ingredients) {
          this.ingredients = ingredients;
      }
  }
  </code></pre>
  
  <p>However, when my program gets to the dataQuery method and tries get and set the name and ingredients, it crashes. Can someone explain what I've done wrong and if there are any better ways to do this with database snapshot?</p>
  
  <blockquote>
    <p>Process: in.tvac.akshayejh.firebasesearch, PID: 24798
        java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String in.tvac.akshayejh.firebasesearch.AdvancedQuery.getName()' on a null object reference
            at in.tvac.akshayejh.firebasesearch.AdvancedSearch.dataQuery(AdvancedSearch.java:140)
            at in.tvac.akshayejh.firebasesearch.AdvancedSearch.access$500(AdvancedSearch.java:20)
            at in.tvac.akshayejh.firebasesearch.AdvancedSearch$2.onDataChange(AdvancedSearch.java:115)
            at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@16.0.4:183)
            at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
            at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
            at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
            at android.os.Handler.handleCallback(Handler.java:873)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:6669)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)</p>
  </blockquote>