Null Pointer Error in referencing RecyclerView in a fragment using Kotlin

1.4k views Asked by At

I was trying out kotlin and its awesome new functionalites in android when I got stuck at a problem. There is this cool thing in kotlin where you can directly use the id from the xml without using findViewById(). But when I apply it to a recycler view and try to make it work, I get a null pointer exception with it. I checked the xml and I do have a recycler view with the ID that i used. Can you guys help me figure out why I am getting a null pointer exception here?

Here is the code where I reference my recycler view and apply methods on it.

with(rv_my_device){
    setHasFixedSize(true)
    layoutManager = LinearLayoutManager(activity)
    listAdapter = MyDeviceAdapter(activity,titles,values)
    adapter = listAdapter
    }

The error I am getting is

FATAL EXCEPTION: main                                                                          
Process: com.example.sriram.mydevice, PID: 24771                                                                            
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sriram.mydevice/com.example.sriram.mydevice.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference                                                                               
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)                                                                  
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724     
at android.app.ActivityThread.-wrap12(ActivityThread.java
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)                                                                                 
at android.os.Handler.dispatchMessage(Handler.java:102)                                                                                 
at android.os.Looper.loop(Looper.java:154)                                                                                 
at android.app.ActivityThread.main(ActivityThread.java:6123)                                                                                  
at java.lang.reflect.Method.invoke(Native Method)                                                                                 

The kotlin code is present in an onCreateView method in a fragment.

And guys, please dont downvote saying that this is a duplicate question about null pointers. I know what are null pointers. I just cannot figure out where my code went wrong or if I misunderstood something in kotlin..

2

There are 2 answers

2
Yassine BELDI On BEST ANSWER

If you call your views inside onCreateView move it to onViewCreated, because view are not initialized when you call them in onCreateView

1
Kiskae On

rv_my_device is null. Beyond that we cannot help you since you havent included the code that initializes the variable, nor the location in which the code is executed. So it is impossible to replicate your problem.