I am also new to Java and Android Development, been making a simple game on my Wildfire S for a while and things have been going well except whenever I get a Null Pointer Exception I cannot seem to find what exactly is causing it.
All forums I have searched on, detail what a Null Pointer Exception is or how to pre-emptively overcome them but that is not what I need.
The problem I face here is that the part of code that Eclipse seems to be drawing my attention to seems completely fine and I haven’t been editing it at all. So I have possibly changed something by mistake somewhere. Not knowing which area has been edited I cannot seem to find out why the error is being thrown.
The log seems to tell me that the error is occuring at the onCreate method in GoalieMenu.java which hasn’t been edited in a very long time. It is simply an Activity which uses mainmenu.xml as its layout, which contains three buttons (start, howToPlay and Exit) each button has XMLs of their own they all seem fine…
I fear this is going to be a really dumb and obvious thing but I’m not greatly familiar with what I am looking at so it is taking me ages and I cannot find it!! grr
here is the log:
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): FATAL EXCEPTION: main
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): java.lang.RuntimeException: Unable
to start activity
ComponentInfo{com.luk.games.Goalie/com.luk.games.Goalie.GoalieMenu}:
java.lang.NullPointerException
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.os.Handler.dispatchMessage(Handler.java:99)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.os.Looper.loop(Looper.java:150)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread.main(ActivityThread.java:4277)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
java.lang.reflect.Method.invokeNative(Native Method)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
java.lang.reflect.Method.invoke(Method.java:507)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
dalvik.system.NativeStart.main(Native Method)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): Caused by:
java.lang.NullPointerException
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
com.luk.games.Goalie.GoalieMenu.onCreate(GoalieMenu.java:34)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
04-04 16:27:57.277: ERROR/AndroidRuntime(2831): ... 11 more
Here is the GoalieMenu.java:
package com.luk.games.Goalie;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class GoalieMenu extends Activity {
private Button startGameButton;
private Button howToPlayButton;
private Button exitButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.mainmenu);
this.startGameButton = (Button)this.findViewById(R.id.startGameButton);
this.startGameButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("com.luk.games.Goalie.GameActivity"));
}
});
this.howToPlayButton = (Button)this.findViewById(R.id.howToPlayButton);
this.howToPlayButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("com.luk.games.Goalie.HowToPlayActivity"));
}
});
this.exitButton = (Button)this.findViewById(R.id.exitButton);
this.exitButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
mainmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="@drawable/mainmenuscreen"
android:layout_height="match_parent" android:orientation="vertical">
<Button
android:id="@+id/startGameButton"
android:background="@layout/startgamebutton"
android:layout_height="50dp"
android:layout_width="180dp"
android:layout_gravity="center"
android:layout_marginTop="100dip"/>
<Button
android:id="@+id/howToPlayButton"
android:background="@layout/howtoplaybutton"
android:layout_height="50dp"
android:layout_width="180dp"
android:layout_gravity="center"/>
<Button
android:id="@+id/exitButton"
android:background="@layout/exitbutton"
android:layout_height="50dp"
android:layout_width="180dp"
android:layout_gravity="center"/>
</LinearLayout>
GoalieMenu.java:34, I think it is this this
howToPlayButton.setOnClickListenerhowToPlayButton might be null.
Debug and use
tryandcatchwhere you suspect.