NullPointerException on findViewById() in android

10,337

Solution 1

The setContentView method should be called with appropriate layout before calling findViewById. It is usually called in onCreate(Bundle savedInstance) method.

Solution 2

You have to call it from your Activity's onCreate method, as the resources will not have been made available before that point.

So expanding MByD's answer, in your onCreate method, first call setContentView(), then findViewById().

Solution 3

First , you should call the setContentView(int layout) ,in order to set the Content of your Activity , and then you can get your Views ( findViewById(int id) ) ;

So your Activity will be like this :

public class UserInteraction extends Activity {
EditText etFrom;
int from;
EditText etTill;
int till;

public void onCreate(Bundle savedInstance{
   super.onCreate(saveInstance);
   this.setContentView(R.layout.main);

   etFrom = (EditText)findViewById(R.id.et_from);
   etTill = (EditText)findViewById(R.id.et_till);
} 

}

Share:
10,337
pointour
Author by

pointour

Updated on June 27, 2022

Comments

  • pointour
    pointour almost 2 years

    In the following code i get a NullPointerException on lines 9/10 with findViewById().
    In my main class I just instantiated an object from this class, to use .getFrom()

    public class UserInteraction extends Activity {
    EditText etFrom;
    int from;
    EditText etTill;
    int till;
    
    public UserInteraction(){
        etFrom = (EditText)findViewById(R.id.et_from);
        etTill = (EditText)findViewById(R.id.et_till);
    }
    
    public int getFrom() {
        String s = etFrom.getText().toString();
        int i = Integer.parseInt(s);
        return i;
    }
    
    public int getTill() {
        String s = etTill.getText().toString();
        int i = Integer.parseInt(s);
        return i;
    }
    

    Is it that the contentView is set in my main class ..? What could be the cause ?

  • Femi
    Femi almost 13 years
    Its important to note: there is honestly very little (to be precise, there is NOTHING) user-interface related that you can safely do in the constructor of an Activity. In general, you should use the onCreate method for everything you would normally use the constructor for.
  • MByD
    MByD almost 13 years
    @Femi - That's an important point. Thanks for pointing that out.
  • pointour
    pointour almost 13 years
    Thanks, I think i got it. Was a huge migration of code to fix it haha.
  • adamp
    adamp almost 13 years
    The reason for this is that Activity#findViewById isn't magical, it's a simple recursive search down the view hierarchy attached to an activity's current window. If you haven't attached any views by using setContentView or similar yet, findViewById will return null because there were no matching views present to find.
  • Niklas Rosencrantz
    Niklas Rosencrantz over 6 years
    Called with which argument? I see many answers just a variable of code and of course I can't assume that works. -1
  • Niklas Rosencrantz
    Niklas Rosencrantz over 6 years
    Call setContentView with which argument? I can't use comments. Where is working code? Been trying for days to achieve the trivial.