0

Currently I'm using RecyclerView from Android to show some data that I get from Firebase. I'm using MVC design pattern so I had to pass some data from the view (actViewDB), to the controller(Controller) and then to the Model class (DataModel). Unfortunately I always get the Java NullPointerException error like this

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ryzen.pajakcerdas, PID: 3138
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ryzen.pajakcerdas/com.example.ryzen.pajakcerdas.actViewDB}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ryzen.pajakcerdas.dataAdapter.setNotes(java.util.List)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        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)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ryzen.pajakcerdas.dataAdapter.setNotes(java.util.List)' on a null object reference
        at com.example.ryzen.pajakcerdas.actViewDB.showModelList(actViewDB.java:22)

Here is the code

actViewDB.java

public class actViewDB extends AppCompatActivity {

    private List<DataModel> modelList;
    private RecyclerView rc;
    private dataAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.recycle_viewdatabase);

        rc = findViewById(R.id.recycler_view);
        rc.setLayoutManager(new LinearLayoutManager(this));
        rc.setHasFixedSize(true);

        modelList = new ArrayList<>();
    }

    private void showModelList() {
        modelList.add(new DataModel("a", 2, 1, 3));
        adapter.setNotes(modelList);
        rc.setAdapter(adapter);
    }
}

Why is this happening? I'm sorry if this question is already asked, I already search for the answer multiple times from the last week but still can't fix this. The closest problem I found is this android: recycler view adapter but still doesn't fix my problem.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64

1 Answers1

0

You have to initialise the adapter before call its method

adapter = new DataAdapter();
Mohammad Sommakia
  • 1,773
  • 3
  • 15
  • 48
  • 1
    Thanks, it works. but then i had a new problems, the RecycleView are Null because i modified the method showModelList() to be called from Controller. should i initialise the RecycleView too? how to do it? – Achmad Farhan Mustaqim Jun 14 '19 at 12:15
  • no you have to get recyclerview reference from controller by calling activity.recyclerview after all this is new problem you have to ask new question and post your code so we can help you. feel free to up vote my answer. happy to help. – Mohammad Sommakia Jun 14 '19 at 12:21