If you are new to android programming you will frequently encounter with NullPointerException when running your app. NullPointerException will make your running app crash. You will be warned with a dialog app force close.
What is NullPointerException?. From its name we know that it is one of java exception. This exception is raised by Java when you refer to a variable that is null. It means that your variable is not yet initialized, so when you call a method to that variable it does not know where that method is, because that variable itself points to nothing.
Below is an example of NullPointerException in android programming :
That piece of code above will raise a NullPointerException at line 14, because the variable button is not yet initialized and the method call to button.setOnClickListener does not point to any object at all.
Try to debug at that line by putting a breakpoint at line 14, tutorial to debuggin is here. Point your mouse arrow on button variable, you will see that button variable is still null, then click resume the app will crash.
You can fix that code by initializing button like in the example below :
Thank you for visiting our website. Just comment below if you have any question to ask.
What is NullPointerException?. From its name we know that it is one of java exception. This exception is raised by Java when you refer to a variable that is null. It means that your variable is not yet initialized, so when you call a method to that variable it does not know where that method is, because that variable itself points to nothing.
Below is an example of NullPointerException in android programming :
package com.example.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } });
That piece of code above will raise a NullPointerException at line 14, because the variable button is not yet initialized and the method call to button.setOnClickListener does not point to any object at all.
Try to debug at that line by putting a breakpoint at line 14, tutorial to debuggin is here. Point your mouse arrow on button variable, you will see that button variable is still null, then click resume the app will crash.
You can fix that code by initializing button like in the example below :
package com.example.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.btnLogin); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } }In the code above button variable has been initialized by findViewById(R.id.btnLogin); and it is safe now to call button.setOnClickListener, no NullPointerException will be raised.
Thank you for visiting our website. Just comment below if you have any question to ask.
No comments:
Post a Comment