Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?

This code, with the block at the top commented, runs successfully:

public class MainActivity extends AppCompatActivity {

    /*
    EditText username = (EditText)findViewById(R.id.editText_Username);
    EditText password = (EditText)findViewById(R.id.editText_Password);
    TextView inputdata = (TextView)findViewById(R.id.textView_InputData);
    TextView welcome = (TextView)findViewById(R.id.textView_Welcome);
    Button login=(Button)findViewById(R.id.button_Login);
    Button anotherLogin=(Button)findViewById(R.id.button_Login_Another);
    */

    public void doLoginOnClick(View v)
    {
        String s1=username.getText().toString();
        inputdata.setText(s1);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab); 
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    } 
 
}

I am trying to capture id of component using

findViewById(R.id.***)

as you can see, I put this code in comment at the very beginning of the code.

EditText username = (EditText)findViewById(R.id.editText_Username);
EditText password = (EditText)findViewById(R.id.editText_Password);
TextView inputdata = (TextView)findViewById(R.id.textView_InputData);
TextView welcome = (TextView)findViewById(R.id.textView_Welcome);
Button login=(Button)findViewById(R.id.button_Login);
Button anotherLogin=(Button)findViewById(R.id.button_Login_Another);

If I remove the comment above and run it (both in emulator and real device), the program crashes immediately, I am surprised what's wrong here?

even I have tried to initialize the same thing using constructor.

But if I put it inside onCreate() there's no crash? Why?

I was trying to fetch info of username and password and display it to the textview in textView_Inputdata using

EditText username = (EditText)findViewById(R.id.editText_Username);
EditText password = (EditText)findViewById(R.id.editText_Password);
TextView inputdata = (TextView)findViewById(R.id.textView_InputData);
inputdata.setText(username.getText.toString()+" "+password.getText.toString());

Is there better or easier way to do that?

like image 525
000 Avatar asked Apr 06 '16 08:04

000


People also ask

How to fix Null pointer exception in Android Studio?

How to fix the NullPointerException? To avoid NullPointerException we have to initialize the Textview component with the help of findviewbyid( ) method as shown below. The findViewbyId( ) takes the “id” value of the component as the parameter. This method helps locate the component present in the app.

What is FindViewById method in Android?

FindViewById(Int32)Finds a view that was identified by the android:id XML attribute that was processed in #onCreate .

What is Viewview FindViewById?

view. findViewById() is used to find a view inside a specific other view. For example to find a view inside your ListView row layout.


2 Answers

Instance member variables are initialized when the instance itself is initialized. It's too early for findViewById()

Before onCreate() your activity does not yet have a Window that findViewById() needs internally. Before setContentView() (that you should be calling in onCreate()) there are no views to be found either.

Therefore init your view references in onCreate() and after setContentView().

like image 104
laalto Avatar answered Sep 20 '22 20:09

laalto


Add this code

 EditText username =  findViewById(R.id.editText_Username);
 EditText password = findViewById(R.id.editText_Password);
 TextView inputdata = findViewById(R.id.textView_InputData);
 TextView welcome = findViewById(R.id.textView_Welcome);
 Button login = findViewById(R.id.button_Login);
 Button anotherLogin = findViewById(R.id.button_Login_Another);

in

onCreate(); 

method after

setContentView(R.layout.activity_main);
like image 31
Devanshu Dwivedi Avatar answered Sep 19 '22 20:09

Devanshu Dwivedi