(TableLayout) findViewById (null pointer exception

I am looking for an answer all day long, but when I try to get a table layout, I have zero. I found information that it could be a different table layout with the same name, but there is only one. My .xml file:

my xml

and my code is here layout6 = (TableLayout)findViewById(R.id.shdslResultsTableLayout); layout6.setVisibility(0); layout6 = (TableLayout)findViewById(R.id.shdslResultsTableLayout); layout6.setVisibility(0);

always ends with a null pointer. Please, help.

+4
source share
2 answers

If findViewById() returns NPE, try a few of them:

  • Clean the project through Project → Clean ... → check your project → OK
  • Make sure you have absolutely no spelling errors in the ID
  • Make sure the contentView displays the correct layout where TableLayout exists
  • Make sure you setContentView before findViewById

I assume that the third option that I have listed is most likely your problem.

Re-cleaning the project can also help after each of these steps.

+3
source

Please make sure you call the correct layout in the setContentView method and make sure you call setContentView BEFORE calling findViewById .

If you did, try to clean / rebuild.

Hope this helps you

+4
source

All Articles