1

Hi everyone please help me, I've got NullPointerException when I try to retrieve data on Firebase and here is my database: FIREBASE DATABASE

And this is my Code:

package com.example.android.frep;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

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 com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class fHome extends AppCompatActivity {

    List<resepNusantara> listResepNusantara;
    ListView listViewResep;

    DatabaseReference dbResepNusantara;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_f_home);
        listViewResep = (ListView) findViewById(R.id.resepList);
        dbResepNusantara = FirebaseDatabase.getInstance().getReference("resepNusantara");
    }


    @Override
    protected void onStart() {
        super.onStart();

        dbResepNusantara.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                //clear the list
                //listResepNusantara.clear();


                //iterating all nodes
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                    //getting resep
                    // ERROR BEGIN HERE
                    resepNusantara resepN = postSnapshot.getValue(resepNusantara.class);
                    //adding resep to the list
                    listResepNusantara.add(resepN);
                }


                //creating the adapter for the list
                ResepList resepListAdapter = new ResepList(fHome.this, listResepNusantara);
                //attaching adapter to the listView
                listViewResep.setAdapter(resepListAdapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

}

So I've tried to create the model class (resepNusantara) to be the same data types as my database. But I still got this error. Why this happen ? Do I miss something ?

This is my LogCat:

10-24 22:51:53.422 15766-15766/? I/zygote: Not late-enabling -Xcheck:jni 
(already on)
10-24 22:51:53.441 15766-15766/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
10-24 22:51:53.671 15766-15766/com.example.android.frep D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
10-24 22:51:53.694 15766-15766/com.example.android.frep D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
10-24 22:51:53.733 15766-15766/com.example.android.frep I/FA: App measurement is starting up, version: 10084
10-24 22:51:53.733 15766-15766/com.example.android.frep I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
10-24 22:51:53.733 15766-15766/com.example.android.frep D/FA: Debug-level message logging enabled
10-24 22:51:53.734 15766-15766/com.example.android.frep D/FA: AppMeasurement singleton hash: 108831703
10-24 22:51:53.758 15766-15766/com.example.android.frep V/FA: Collection enabled
10-24 22:51:53.758 15766-15766/com.example.android.frep V/FA: App package, google app id: com.example.android.frep, 1:199887442989:android:188e4894cfe4b490
10-24 22:51:53.758 15766-15766/com.example.android.frep I/FA: To enable faster debug mode event logging run:
                                                                adb shell setprop debug.firebase.analytics.app com.example.android.frep
10-24 22:51:53.790 15766-15766/com.example.android.frep V/FA: Registered activity lifecycle callback
10-24 22:51:53.791 15766-15766/com.example.android.frep I/FirebaseInitProvider: FirebaseApp initialization successful
10-24 22:51:53.792 15766-15766/com.example.android.frep I/InstantRun: starting instant run server: is main process
10-24 22:51:53.800 15766-15808/com.example.android.frep V/FA: Using measurement service
10-24 22:51:53.804 15766-15808/com.example.android.frep V/FA: Connecting to remote service
10-24 22:51:53.846 15766-15766/com.example.android.frep V/FA: onActivityCreated
10-24 22:51:53.991 15766-15766/com.example.android.frep I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
10-24 22:51:53.991 15766-15766/com.example.android.frep I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 6
10-24 22:51:53.993 15766-15766/com.example.android.frep W/zygote: Skipping duplicate class check due to unrecognized classloader
10-24 22:51:54.010 15766-15766/com.example.android.frep W/zygote: Skipping duplicate class check due to unrecognized classloader
10-24 22:51:54.045 15766-15808/com.example.android.frep V/FA: Using measurement service
10-24 22:51:54.045 15766-15808/com.example.android.frep V/FA: Connection attempt already in progress
10-24 22:51:54.049 15766-15808/com.example.android.frep V/FA: Activity resumed, time: 2406504
10-24 22:51:54.071 15766-15816/com.example.android.frep D/OpenGLRenderer: HWUI GL Pipeline
10-24 22:51:54.075 15766-15819/com.example.android.frep D/NetworkSecurityConfig: No Network Security Config specified, using platform default
10-24 22:51:54.139 15766-15816/com.example.android.frep I/OpenGLRenderer: Initialized EGL, version 1.4
10-24 22:51:54.139 15766-15816/com.example.android.frep D/OpenGLRenderer: Swap behavior 1
10-24 22:51:54.139 15766-15816/com.example.android.frep W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
10-24 22:51:54.139 15766-15816/com.example.android.frep D/OpenGLRenderer: Swap behavior 0
10-24 22:51:54.158 15766-15816/com.example.android.frep D/EGL_emulation: eglCreateContext: 0x9bd053c0: maj 3 min 1 rcv 4
10-24 22:51:54.190 15766-15816/com.example.android.frep D/EGL_emulation: eglMakeCurrent: 0x9bd053c0: ver 3 1 (tinfo 0x9bd03470)
10-24 22:51:54.190 15766-15816/com.example.android.frep E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
10-24 22:51:54.191 15766-15816/com.example.android.frep E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
10-24 22:51:54.191 15766-15816/com.example.android.frep E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824


                                                                          [ 10-24 22:51:54.234 15766:15816 D/         ]
                                                                          SurfaceInterface::setAsyncMode: set async mode 1
10-24 22:51:54.263 15766-15816/com.example.android.frep D/EGL_emulation: eglMakeCurrent: 0x9bd053c0: ver 3 1 (tinfo 0x9bd03470)
10-24 22:51:54.370 15766-15808/com.example.android.frep D/FA: Connected to remote service
10-24 22:51:54.370 15766-15808/com.example.android.frep V/FA: Processing queued up service tasks: 2
10-24 22:51:55.831 15766-15766/com.example.android.frep D/AndroidRuntime: Shutting down VM


                                                                          --------- beginning of crash
10-24 22:51:55.832 15766-15766/com.example.android.frep E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.android.frep, PID: 15766
                                                                          java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
                                                                              at com.example.android.frep.fHome$1.onDataChange(fHome.java:60)
                                                                              at com.google.android.gms.internal.zzbmz.zza(Unknown Source:13)
                                                                              at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source:2)
                                                                              at com.google.android.gms.internal.zzboc$1.run(Unknown Source:63)
                                                                              at android.os.Handler.handleCallback(Handler.java:789)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                              at android.os.Looper.loop(Looper.java:164)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

And this is my model class (only properties and constructor):

public class resepNusantara {

    private String keteranganResep;
    private String asalDaerah;
    private String bahan;
    private String cara;
    private Long rating;
    private boolean favourite;
    private String nama;

    public resepNusantara() {
        //required
    }

    public resepNusantara(String asalDaerah, String bahan, String cara, boolean favourite, String keteranganResep, String nama, Long rating) {
        this.setNama(nama);
        this.keteranganResep = keteranganResep;
        this.asalDaerah = asalDaerah;
        this.bahan = bahan;
        this.cara = cara;
        this.rating = rating;
        this.favourite = favourite;
    }
Birendra Singh
  • 842
  • 11
  • 19
Daniel CH
  • 27
  • 8

2 Answers2

0

You have not initialized the List.

You may want to initialize it during the declaration as:

List<resepNusantara> listResepNusantara = new ArrayList<>();

And then uncomment the line to clear the list in the onDataChange() method.

listResepNusantara.clear();
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

You are getting this crash because you never initialized listResepNusantara. Replace the line

    List<resepNusantara> listResepNusantara;

by

    List<resepNusantara> listResepNusantara =  new ArrayList<resepNusantara>();

and you are good to go. BTW have a look on What is a NullPointerException, and how do I fix it.

Birendra Singh
  • 842
  • 11
  • 19