java - Null Pointer Exception in Adapter for recyclerview -


i've been trying implement recyclerview activity keep getting nullpointerexception. first time making recyclerview if i'm doing wrong tell me. i'll show code tell where error at

my main xml layout (called activity_main.xml)

<android.support.v4.widget.drawerlayout                 xmlns:android="http://schemas.android.com/apk/res/android"                 xmlns:card_view="http://schemas.android.com/apk/res-auto"                 xmlns:app="http://schemas.android.com/apk/res-auto"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:id="@+id/drawer"                 android:fitssystemwindows="true">     <linearlayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical">          <include             android:id="@+id/tool_bar"             layout="@layout/tool_bar">         </include>          <android.support.v7.widget.recyclerview             android:id="@+id/main_recycler_view"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:paddingright="8dp"             android:paddingleft="8dp"             android:paddingtop="8dp"/>      </linearlayout>      <android.support.design.widget.navigationview         android:id="@+id/navigation_view"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_gravity="start"         app:headerlayout="@layout/drawer_header"         app:menu="@menu/drawer"/>  </android.support.v4.widget.drawerlayout> 

my row xml recyclerview (called main_recycler_view_row.xml)

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"               android:layout_width="match_parent"               android:layout_height="match_parent">      <android.support.v7.widget.cardview         android:id="@+id/main_card"         android:layout_width="match_parent"         android:layout_height="wrap_content">          <linearlayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="vertical">              <textview                 android:id="@+id/period_title"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:textsize="24sp"                 android:textcolor="@android:color/black"                 android:paddingtop="24dp"                 android:paddingleft="16dp"                 android:paddingbottom="4dp"/>              <textview                 android:id="@+id/period_subtitle"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:textsize="14sp"                 android:paddingleft="16dp"                 android:paddingbottom="16dp"/>          </linearlayout>      </android.support.v7.widget.cardview>  </linearlayout> 

my main activity

package maxbleggi.afstudentplanner;  import android.content.intent; import android.content.res.configuration; import android.support.design.widget.navigationview; import android.support.v4.view.gravitycompat; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.app.appcompatactivity; import android.support.v7.app.actionbar; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.listview; import android.widget.toast;  import com.parse.getcallback; import com.parse.parse; import com.parse.parseexception; import com.parse.parseobject; import com.parse.parsequery; import com.parse.parseuser;  import java.util.arraylist; import java.util.list;  public class mainactivity extends appcompatactivity {     // "macros" user's class periods     private final int period1 = 0;     private final int period2 = 1;     private final int period3 = 2;     private final int period4 = 3;     private final int period5 = 4;     private final int period6 = 5;     private final int period7 = 6;      // toolbar     private toolbar toolbar;      // nav drawer     private drawerlayout navdrawer;      // user's data     private string[] userclasses = new string[10];      // list fill classroom class     private list<classroomclass> classrooms;      /*     * on creation of activity      */     @override     protected void oncreate(bundle savedinstancestate)     {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          // initialise , set toolbar actionbar         toolbar = (toolbar) findviewbyid(r.id.tool_bar);         setsupportactionbar(toolbar);          // initialize drawer layout         navdrawer = (drawerlayout) findviewbyid(r.id.drawer);         navigationview navview = (navigationview) findviewbyid(r.id.navigation_view);          // initialize recycler view elements         recyclerview mainrecyclerview = (recyclerview)findviewbyid(r.id.main_recycler_view);          // initialize layout manager recycler view         recyclerview.layoutmanager mainlayoutmanager = new linearlayoutmanager(this);          // adapter recycler view         recyclerview.adapter mainadapter= new mainrecycleradapter(classrooms);;          // initialize nav bars         initnavbars();          // initialize nav drawer         initnavdrawer();          // initialize data classes         initclassdata();          // add layout manager recycler view         mainrecyclerview.setlayoutmanager(mainlayoutmanager);          // add adapter recycler view         mainrecyclerview.setadapter(mainadapter);      }      /*     * initializes class data each class     */     public void initclassdata()     {         // check if user not cached         parseuser currentuser = parseuser.getcurrentuser();         if (currentuser == null)         {             // prompt user register screen             intent intent = new intent(mainactivity.this, registeractivity.class);             intent.addflags(intent.flag_activity_new_task);             intent.addflags(intent.flag_activity_clear_task);             startactivity(intent);         }          // query database user's classes         final parsequery<parseobject> query = parsequery.getquery("studentclasses");         query.whereequalto("student_id", parseuser.getcurrentuser());          query.getfirstinbackground(new getcallback<parseobject>()         {             @override             public void done(parseobject parseobject, parseexception e)             {                 if (parseobject == null)                 {                     // retrieved object                     userclasses[period1]= parseobject.getstring("first_period");                     userclasses[period2]= parseobject.getstring("second_period");                     userclasses[period3]= parseobject.getstring("third_period");                     userclasses[period4]= parseobject.getstring("fourth_period");                     userclasses[period5]= parseobject.getstring("fifth_period");                     userclasses[period6]= parseobject.getstring("sixth_period");                     userclasses[period7]= parseobject.getstring("seventh_period");                 }                 else                 {                     // failed lookup. here                 }             }         });          // fill fields class periods         classrooms = new arraylist<>();          // titles of classes add list         string[] classtitles = {"first period", "second period", "third period", "fourth period",                 "fifth period", "sixth period", "seventh period"};          // iterate on every period add list         (int = 0; <= period7; i++)         {             // if period registered "nothing" don't add list             //if (!userclasses[i].equals("nothing"))             //{                 classrooms.add(new classroomclass(userclasses[i], classtitles[i], + 1));             //}         }     }      public void initnavdrawer()     {         navdrawer = (drawerlayout) findviewbyid(r.id.drawer);          final navigationview navview = (navigationview) findviewbyid(r.id.navigation_view);          navview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener()         {             @override             public boolean onnavigationitemselected(menuitem menuitem)             {                 menuitem.setchecked(true);                 navdrawer.closedrawers();                 return true;             }         });     }      private void initnavbars()     {        if (toolbar != null)        {             getsupportactionbar().setdisplayhomeasupenabled(true);             toolbar.setnavigationicon(r.drawable.ic_menu_white);             toolbar.setnavigationonclicklistener(new view.onclicklistener()             {                 @override                 public void onclick(view v)                 {                     navdrawer.opendrawer(gravitycompat.start);                 }             });        }     } } 

my class data fields

package maxbleggi.afstudentplanner;  /*  * define characteristics classroom cards have in common */ public class classroomclass {      string classname;     string classperiod;     int classnumber;      public classroomclass(string classname, string classperiod, int classnumber)     {         this.classname = classname;         this.classperiod = classperiod;         this.classnumber = classnumber;     }      public string getclassname()     {         return classname;     }      public string getclassperiod()     {         return classperiod;     }      public int getclassnumber()     {         return classnumber;     } } 

finally adapter

package maxbleggi.afstudentplanner;  import android.support.v7.widget.cardview; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview;  import java.util.list;  public class mainrecycleradapter extends recyclerview.adapter<mainrecycleradapter.viewholder> {     private list<classroomclass> classrooms;      public static class viewholder extends recyclerview.viewholder     {         // hold data items         public view view;          viewholder(view itemview)         {             super(itemview);             view = itemview;         }     }      public mainrecycleradapter(list<classroomclass> classrooms)     {         this.classrooms = classrooms;     }      @override     public viewholder oncreateviewholder(viewgroup viewgroup, int i)     {         // create new view         view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.main_recycler_view_row, viewgroup, false);          // set view's parameters         viewholder pvh = new viewholder(v);         return pvh;     }      @override     public void onbindviewholder(viewholder viewholder, int position)     {         // elements view         textview title = (textview) viewholder.view.findviewbyid(r.id.period_title);         textview subtitle = (textview) viewholder.view.findviewbyid(r.id.period_subtitle);          // retrieve data data field , put in view         title.settext(classrooms.get(position).getclassname());         subtitle.settext(classrooms.get(position).classperiod);      }      @override     public int getitemcount()     {         return classrooms.size();     }       @override     public void onattachedtorecyclerview(recyclerview recyclerview)     {         super.onattachedtorecyclerview(recyclerview);     } } 

i java.lang.nullpointerexception in mainrecycleradapter @ item count here

    @override     public int getitemcount()     {         return classrooms.size();     } 

any or comments appreciated , sorry code. wanted make sure thorough.

you call

initclassdata(); 

after call of constructor:

recyclerview.adapter mainadapter= new mainrecycleradapter(classrooms);; 

this reason of nullpoimter. object classrooms still empty when invoke size() on nullpoint.

just replace order of lines.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -