I am a beginner. Stuck on this NullPointerException. This is a simple program of buttons. As soon as i run as android application, the project opens and immediately gets closed by displaying unfortunately my app has stopped.
04-15 17:40:05.656: D/AndroidRuntime(599): Shutting down VM
04-15 17:40:05.656: W/dalvikvm(599): threadid=1: thread exiting with uncaught exception (group=0xb2adcd58)
04-15 17:40:05.666: E/AndroidRuntime(599): FATAL EXCEPTION: main
04-15 17:40:05.666: E/AndroidRuntime(599): Process: com.example.buttona, PID: 599
04-15 17:40:05.666: E/AndroidRuntime(599): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttona/com.example.buttona.Buttonwa}: java.lang.NullPointerException
04-15 17:40:05.666: E/AndroidRuntime(599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
04-15 17:40:05.666: E/AndroidRuntime(599): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
04-15 17:40:05.666: E/AndroidRuntime(599): at android.app.ActivityThread.access$800(ActivityThread.java:138)
04-15 17:40:05.666: E/AndroidRuntime(599): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
04-15 17:40:05.666: E/AndroidRuntime(599): at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 17:40:05.666: E/AndroidRuntime(599): at android.os.Looper.loop(Looper.java:136)
04-15 17:40:05.666: E/AndroidRuntime(599): at android.app.ActivityThread.main(ActivityThread.java:5042)
04-15 17:40:05.666: E/AndroidRuntime(599): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 17:40:05.666: E/AndroidRuntime(599): at java.lang.reflect.Method.invoke(Method.java:515)
04-15 17:40:05.666: E/AndroidRuntime(599): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
04-15 17:40:05.666: E/AndroidRuntime(599): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-15 17:40:05.666: E/AndroidRuntime(599): at dalvik.system.NativeStart.main(Native Method)
04-15 17:40:05.666: E/AndroidRuntime(599): Caused by: java.lang.NullPointerException
04-15 17:40:05.666: E/AndroidRuntime(599): at com.example.buttona.Buttonwa.onCreate(Buttonwa.java:29)
04-15 17:40:05.666: E/AndroidRuntime(599): at android.app.Activity.performCreate(Activity.java:5231)
04-15 17:40:05.666: E/AndroidRuntime(599): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 17:40:05.666: E/AndroidRuntime(599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
04-15 17:40:05.666: E/AndroidRuntime(599): ... 11 more
04-15 17:40:11.466: I/Process(599): Sending signal. PID: 599 SIG: 9
04-15 17:57:32.017: D/AndroidRuntime(662): Shutting down VM
04-15 17:57:32.017: W/dalvikvm(662): threadid=1: thread exiting with uncaught exception (group=0xb2adcd58)
04-15 17:57:32.027: E/AndroidRuntime(662): FATAL EXCEPTION: main
04-15 17:57:32.027: E/AndroidRuntime(662): Process: com.example.buttona, PID: 662
04-15 17:57:32.027: E/AndroidRuntime(662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttona/com.example.buttona.Buttonwa}: java.lang.NullPointerException
04-15 17:57:32.027: E/AndroidRuntime(662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
04-15 17:57:32.027: E/AndroidRuntime(662): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
04-15 17:57:32.027: E/AndroidRuntime(662): at android.app.ActivityThread.access$800(ActivityThread.java:138)
04-15 17:57:32.027: E/AndroidRuntime(662): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
04-15 17:57:32.027: E/AndroidRuntime(662): at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 17:57:32.027: E/AndroidRuntime(662): at android.os.Looper.loop(Looper.java:136)
04-15 17:57:32.027: E/AndroidRuntime(662): at android.app.ActivityThread.main(ActivityThread.java:5042)
04-15 17:57:32.027: E/AndroidRuntime(662): at java.lang.reflect.Method.invokeNative(Native Method)
04-15 17:57:32.027: E/AndroidRuntime(662): at java.lang.reflect.Method.invoke(Method.java:515)
04-15 17:57:32.027: E/AndroidRuntime(662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
04-15 17:57:32.027: E/AndroidRuntime(662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-15 17:57:32.027: E/AndroidRuntime(662): at dalvik.system.NativeStart.main(Native Method)
04-15 17:57:32.027: E/AndroidRuntime(662): Caused by: java.lang.NullPointerException
04-15 17:57:32.027: E/AndroidRuntime(662): at com.example.buttona.Buttonwa.onCreate(Buttonwa.java:29)
04-15 17:57:32.027: E/AndroidRuntime(662): at android.app.Activity.performCreate(Activity.java:5231)
04-15 17:57:32.027: E/AndroidRuntime(662): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 17:57:32.027: E/AndroidRuntime(662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
04-15 17:57:32.027: E/AndroidRuntime(662): ... 11 more
04-15 17:57:34.957: I/Process(662): Sending signal. PID: 662 SIG: 9
This Is The Java File.
public class Buttonwa extends Activity {
Button name, surname;
TextView display;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buttonwa);
name = (Button) findViewById(R.id.name_button);
surname = (Button) findViewById(R.id.surname_button);
display = (TextView) findViewById(R.id.display);
name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
display.setText("Your Name Is Shashank Yadav");
}
});
surname.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
display.setText("Your Name Is Yadav Shashank");
}
});
this is the xml file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.buttona.Buttonwa$PlaceholderFragment" >
<TextView
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
/>
<Button
android:id="@+id/name_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/Name"
/>
<Button
android:id="@+id/surname_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/name_button"
android:text="@string/Surname"
/>
</RelativeLayout>