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.

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your post, but that's not it. I'm calling setContentView in onCreate.
Could you please share your whole oncreate and xml layout?
Clean project seems to have fixed whatever the problem was. Bloody hell.
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...
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 ; }
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);

Comments

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.

2 Comments

Thanks for your post, but that's not it. I'm calling setContentView in onCreate.
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.
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.

Comments

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

Comments

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);

Comments

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.)

1 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.