7

Why is

TextView test = (TextView) findViewById(R.id.testTextView);
test.getText();

generating a null pointer exception? The id is correct, testTextView is correctly declared in my XML layout file.

Cristian
  • 198,401
  • 62
  • 356
  • 264
Ken
  • 30,811
  • 34
  • 116
  • 155

7 Answers7

27

The only reason for findViewById to return null if you are passing a valid id is that you are either setting the wrong content view (with setContentView) or not setting a content view at all.

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Thanks for your post, but that's not it. I'm calling `setContentView` in `onCreate`. – Ken Jan 31 '11 at 04:48
  • Could you please share your whole oncreate and xml layout? – Cristian Jan 31 '11 at 04:49
  • 1
    Clean project seems to have fixed whatever the problem was. Bloody hell. – Ken Jan 31 '11 at 04:59
  • i am facing the same problem,but in my case i am not using setContentView because its a fragment,so i am using view = inflater.inflate(R.layout.test, container, false); but it is giving me errors,not in the onCreateMethod but in a seperate method... – Ankit Srivastava Sep 19 '13 at 13:50
  • Thank you very much. I would hug you if I could, I was getting desperate, did not pay attention, I was passing the wrong layout to setContent. while (1){ thank you ; } – Baroudi Safwen Jun 04 '16 at 23:41
3

I think you might have written setContentView(..) after defining the TextView. Reverse these, and it should work.

Change:

TextView test = (TextView) findViewById(R.id.testTextView);
.
.
setContetView(..)

To:

setContetView(..)
.
.
TextView test = (TextView) findViewById(R.id.testTextView);
AVI
  • 5,516
  • 5
  • 29
  • 38
2

Are you sure the TextView is set on the right XML? For example if you're creating a Dialog that loads a custom XML, to get an element from that xml you have to mention it in dialog.findViewById(R.id.testTextView);

Like Falmarri said, the view has to be inflated. I understand you solved it by creating a new project, but still thought to mention it for future users.

eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
2

You probably haven't called setContentView. You can only use findViewById to get elements of views that have already been inflated.

You could also use a layoutinflater to inflate the view, but that's probably not what you want.

Falmarri
  • 47,727
  • 41
  • 151
  • 191
  • Thanks for your post, but that's not it. I'm calling `setContentView` in `onCreate`. – Ken Jan 31 '11 at 04:48
  • Thank you. But it's not working. I want everybody to stop trying to explain and let's instead produce a working example that we can use. I can't test an explanation. I have no idea WHAT to call setcontentview on. Which parameter? I give you -1 because there is nothing to use. Please stop commenting and start solving the problem. – Niklas Rosencrantz Nov 21 '17 at 06:23
1

It can also be that you defined the activity in two files. For example layout and layout-v21 and some information like id is missing on one of them. So check all the activity's layouts

sivi
  • 10,654
  • 2
  • 52
  • 51
0

In my case, the layout was not finished inflating. Solved by adding a small delay before trying to access the TextView.

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
    @Override
    public void run() {
        TextView test = (TextView) findViewById(R.id.testTextView);
        test.getText();
    }
}, 100);
0

I struggled with this for a while and what I realized was, that, if you have more than one layout file version like:

"activity_one.xml" in "layout" folder and one in "layout - small" folder

Which I used for multiple phone layout support, the problem was that one of the TextViews was in both, with the exact same ID and everything, however the difference was that one was higher up in the hierarchy of views in the layout.

When I changed them to be in the same spot it worked.

(I know this is already answered, but maybe this helps someone out there. Very rare though.)

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 05 '22 at 23:04