nullpointerexception - getting null-pointer exception in android when adding buttons dynamically to LinearLayout -


i've created android application creating multi-dimensional buttons dynamically linearlayout, row count taken database, column count fixed 5.

the problem when row count 11 works fine....but when row count 12 i'm getting nullpointer exception @

rowlayout.addview(buttons[i][j],param); 

can please tell me why happening , solution this

public void createbuttons()     {           linearlayout layoutvertical = (linearlayout) findviewbyid(r.id.livlayout);         linearlayout.layoutparams param = new linearlayout.layoutparams(         layoutparams.match_parent,         layoutparams.wrap_content, 1.0f);         layoutvertical.setorientation(linearlayout.vertical);         param.setmargins(10, 10, 10, 10);         linearlayout rowlayout=null;          cursor cursor=db.getpubtables();          int rowcount =cursor.getcount(); // if rowcount 11 working other give null pointer exception eg..if 12         rowcount--;         system.out.println("counttttttttttttttt--->"+rowcount);         button[][] buttons = new button[rowcount][5];          int count=rowcount;          (int i=0; rowcount>0; i++)          {              if(count%5==1)             {                 rowlayout = new linearlayout(this);                 //rowlayout.setbackgroundcolor(color.black);                 rowlayout.setorientation(linearlayout.horizontal);                 //rowlayout.setweightsum(5);                 layoutvertical.addview(rowlayout,param);                 count=count-5;              }             for(int j=0;j<5&&rowcount>0;j++)             {                  cursor.movetonext();                 buttons[i][j]=new button(this);                 buttons[i][j].settext(""+cursor.getstring(0));                  system.out.println("seeeeeeeeeeeeeeeeeeeeeeeeee:"+cursor.getstring(0));                 buttons[i][j].setheight(55);                 buttons[i][j].setwidth(80);                 buttons[i][j].settypeface(null, typeface.bold);                 buttons[i][j].settextcolor(color.black);                 buttons[i][j].setbackgroundresource(r.drawable.dinein_btn_green);                  system.out.println("table status-->"+cursor.getstring(1));                 if(cursor.getstring(1).equals("reserved"))                 {                     buttons[i][j].setbackgroundresource(r.drawable.dinein_btn_red);                 }                 if(cursor.getstring(1).equals("availabe"))                 {                     buttons[i][j].setbackgroundresource(r.drawable.dinein_btn_green);                 }                 if(cursor.getstring(1).equals("occupied"))                 {                     buttons[i][j].setbackgroundresource(r.drawable.dinein_btn_red);                 }                  buttons[i][j].setid(integer.parseint(cursor.getstring(0)));                 rowlayout.addview(buttons[i][j],param); // if row count 12 here getting nullpointer exception, if row count 11 working                 rowcount--;             }         }         cursor.close();         db.close();     } 

logcat output

java.lang.runtimeexception: unable start activity componentinfo{com.kotouch/com.dinein.dinein_tables}: java.lang.nullpointerexception 

till rowcount 11, view visisble screen. add 1 more row cannot visible on screen, null value.

this how listview works internally. @ time has 11 rows (which can see on screen). while scrolling dynalically removes rows , add new rows.

hope helps