-2

Hello and sorry for my english skills =)

I'm coding a little calculation programm in Android Studio and have a problem.

I've made dynamically creating TableRow's in TableLayout like this:

    TableLayout table2 = (TableLayout) findViewById(R.id.table2);
    TableRow.LayoutParams rowParams = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);

    TableRow row = new TableRow(this);
    row.setLayoutParams(rowParams);

    row.addView(floor2);
    row.addView(ps2table);
    row.addView(gsatable);
    row.addView(vitable);
    table2.addView(row);

Where floor2, ps2table, gsatable and vitable - are TextView's which calculating after a button is pressed.

After the Table has created i try to use one of the numbers (the last row of the TableLayout and the child at 2 position):

    int gsn_count = table2.getChildCount();
    TableRow gsnRow = (TableRow) table2.getChildAt(gsn_count);
    TextView tGsn = (TextView) gsnRow.getChildAt(2);

My problem is, when i try to take a value of one or another number (double) from any TableRow from this TableLayout, i got this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TableRow.getChildAt(int)' on a null object reference

It is look for me as i have not a number in place which i refer to, but it is there. I'll add a picture with comments and all my code in links below.

Picture: http://i.imgur.com/huI4kba.png

Code for a method in which i've got an error: http://pastebin.com/nDPnbHNR

Thank you!

vaibhav
  • 766
  • 8
  • 13
  • Try to debug your code to specify your question the next time. I guess you could find the reason for this Exception pretty easy by yourself. ;) – daemmie Nov 25 '15 at 13:39

1 Answers1

1

Just try:

int gsn_count = table2.getChildCount();
TableRow gsnRow = (TableRow) table2.getChildAt(gsn_count -1);

getChildAt() gets an index (starting with 0) and getChildCount() returns the child count.

So if you have got 2 items, the max index is 1 and the count is 2.

daemmie
  • 6,091
  • 3
  • 25
  • 44