-1

i've created an android application for creating multi-dimensional buttons dynamically to LinearLayout, the row count is taken from the database, where as the column count is fixed as 5.

The problem is when i get the row count as 11 everything works fine....but when i get the row count as 12 i'm getting nullpointer exception at

rowLayout.addView(buttons[i][j],param);

can anyone please tell me why this is happening and a solution for 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 is 11 its working other than that it give null pointer exception eg..if 12
        rowcount--;
        System.out.println("COUNTTTTTTTTTTTTTTT--->"+rowcount);
        Button[][] buttons = new Button[rowcount][5]; 
        int count=rowcount;

        for (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 is 12 here getting nullpointer exception, if row count is 11 its working perfectly
                rowcount--;
            }
        }
        cursor.close();
        db.close();
    }

LOGCAT OUTPUT

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kotouch/com.dinein.Dinein_Tables}: java.lang.NullPointerException
0

1 Answer 1

0

Till your rowcount 11, the view is visisble for the screen. As you add one more row which cannot be visible on the screen, so you get null value.

This is how even listView works internally. At a time it has 11 rows (which can see on the screen). While scrolling it dynalically removes rows and add new rows.

Hope this helps

4
  • can you please tell me some solution for this
    – user2040500
    Aug 23, 2013 at 5:44
  • You must be having a callback function (maybe getView()) while scrolling your view. Add rows dynamically there and return that view. also keep removing the views not in use.
    – Sushil
    Aug 23, 2013 at 5:50
  • By the way why dont you feed data to you rowlayout from an adapter. That should be the correct way to do it.
    – Sushil
    Aug 23, 2013 at 5:53
  • I just tried googling and found this link which may be useful for you: framentos.com/en/android-tutorial/2012/07/16/…
    – Sushil
    Aug 23, 2013 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.