0

I have a view all recipes activity that is trying to read from a firebase database.

However,

I keep getting null exception errors (see error codes below).

What I want to try is to read data and store it within a listview. This data is in the form of four strings: name, recipe description, ingredient list and method.

Would anyone be able to assist?

JAVA

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.lang.reflect.Method;
import java.util.ArrayList;

import static com.example.korey.R.id.ingredients;

public class viewAllRecipes extends AppCompatActivity {




    FirebaseDatabase mFirebaseDatabase;
    DatabaseReference mMessagesDatabaseReference;
    ArrayList<String> recipes = new ArrayList<>();
    ListView recipeListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_all_recipes);
        recipeListView = (ListView) findViewById(R.id.recipeListView);

        mFirebaseDatabase = FirebaseDatabase.getInstance();
        mMessagesDatabaseReference = mFirebaseDatabase.getReference("recipeDetails");

showData();
    }
    private void showData() {
        mMessagesDatabaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            getUpdates(dataSnapshot);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }

        });
    }

    public void getUpdates(DataSnapshot ds) {
        recipes.clear();

        for (DataSnapshot data : ds.getChildren()) {
            recipe r = new recipe();
            r.setName(data.getValue(recipe.class).getName());
            recipes.add(r.getName());


        }
        if (recipes.size()>0) {
            ArrayAdapter<String> adapter = new ArrayAdapter<>(viewAllRecipes.this,android.R.layout.simple_list_item_1,recipes);
            recipeListView.setAdapter(adapter);
        }
        else {
            Toast.makeText(viewAllRecipes.this, "No data", Toast.LENGTH_SHORT).show();
        }
    }
}

error code:

 FATAL EXCEPTION: main
                                                                                      Process: com.example.korey.puregymapplication, PID: 29093
                                                                                      com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.korey.puregymapplication.recipe
                                                                                          at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
                                                                                          at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                                                                                          at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                                                                                          at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                                          at com.example.korey.puregymapplication.viewAllRecipes.getUpdates(viewAllRecipes.java:78)
                                                                                          at com.example.korey.puregymapplication.viewAllRecipes$1.onChildAdded(viewAllRecipes.java:47)
                                                                                          at com.google.android.gms.internal.zzblz.zza(Unknown Source)
                                                                                          at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
                                                                                          at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
                                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                          at android.os.Looper.loop(Looper.java:158)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Can anybody help?

Cheers.

Korey
  • 5
  • 1
  • 4

3 Answers3

0
convert object of type java.lang.String to type com.example.korey.puregymapplication.recipe

this erro is when json data dont fit your model

check this code

r.setName(data.getValue(recipe.class).getName());

here is your problem , see your returned Json data

Elsunhoty
  • 1,609
  • 14
  • 27
0

Here's a cleaner solution:

    adapter = new FirebaseListAdapter<recipe>(viewAllRecipes.this, recipe.class,
            android.R.layout.simple_list_item_1,FirebaseDatabase.getInstance()
            .getReference().child("recipeDetails")) {
        @Override
        protected void populateView(View v, recipe model, int position) {
           TextView recipe =(TextView)v.findViewById(android.R.id.text1);
           tutorUser.setText(model.getName());
        }
    };

    recipeListView.setAdapter(adapter);
Chester Cobus
  • 701
  • 4
  • 12
0

Simple fix that probably took longer than it should.

mMessagesDatabaseReference = mFirebaseDatabase.getInstance().getReference();

^I required to add an instance of this database and a reference.

Fixed. No more crashing. ALL information within the firebase database is now displaying nicely.

I hope this can help other users who are trying to display information in a separate activity using a list view!

Korey
  • 5
  • 1
  • 4