1
02-18 21:26:32.055: E/AndroidRuntime(1529): FATAL EXCEPTION: main
02-18 21:26:32.055: E/AndroidRuntime(1529): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yellomecha.pianobirds/com.yellomecha.pianobirds.Main}: java.lang.NullPointerException
02-18 21:26:32.055: E/AndroidRuntime(1529):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at android.os.Looper.loop(Looper.java:137)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at java.lang.reflect.Method.invoke(Method.java:525)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at dalvik.system.NativeStart.main(Native Method)
02-18 21:26:32.055: E/AndroidRuntime(1529): Caused by: java.lang.NullPointerException
02-18 21:26:32.055: E/AndroidRuntime(1529):     at com.yellomecha.pianobirds.Wire.<init>(Wire.java:20)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at com.yellomecha.pianobirds.Game.<init>(Game.java:24)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at com.yellomecha.pianobirds.Main.onCreate(Main.java:26)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at android.app.Activity.performCreate(Activity.java:5133)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-18 21:26:32.055: E/AndroidRuntime(1529):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-18 21:26:32.055: E/AndroidRuntime(1529):     ... 11 more

start activity ComponentInfo{com.yellomecha.pianobirds/com.yellomecha.pianobirds.Main}: java.lang.NullPointerException

I get this error when running the following on Eclipse why am I getting this error, I have no idea. I looked around on this site and it seems that a lot of people get this error for an assortment of reasons.

public class Main extends Activity 
{

private static Game game;
public static Handler updateHandler = new Handler()
{
    public void handleMessage(Message msg)
    {
        game.update();
        game.invalidate();
        super.handleMessage(msg);
    }
};

/** On Create **/
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    game = new Game(this);
    //LinearLayout layout = (LinearLayout) findViewById(R.layout.activity_main);;
    //layout.setOrientation(LinearLayout.HORIZONTAL);

    setContentView(game);
    Thread mainThread = new Thread(new UpdateThread());
    mainThread.start();
}

public class UpdateThread implements Runnable
{
    @Override
    public void run()
    {
        while(true)
            Main.updateHandler.sendEmptyMessage(0);
    }

}

}

The game class

public class Game extends View
{
private Bitmap birdImage;
private Paint paint;
private Wire top,bottom;

public Game(Context context)
{
    super(context);
    birdImage = Bitmap.createBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.toucan));
    paint = new Paint();
    paint.setStrokeWidth(5);
    paint.setColor(Color.BLACK);
    top = new Wire(getWidth(),(float)getHeight()/3, true);
    bottom = new Wire(getWidth(), (float)getHeight()*2/3, false);

}

//called every frame
protected void onDraw(Canvas canvas)
{
    canvas.drawBitmap(birdImage, getWidth(), getHeight(), paint);
    top.draw(canvas, paint);
    bottom.draw(canvas, paint);
}

public void update()
{

}

}

and the manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yellomecha.pianobirds"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.yellomecha.pianobirds.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

4
  • at which line you get the Exception? Commented Feb 18, 2014 at 21:45
  • This is certainly not the whole stacktrace, is it? Commented Feb 18, 2014 at 21:47
  • Please post the full LogCat trace, it will help us to discover the error Commented Feb 18, 2014 at 21:56
  • I added the LogCat trace Commented Feb 18, 2014 at 22:20

2 Answers 2

2

Your NullPointerException is on line 20 of Wire.java. Make sure every object on that line is initialized before you access it.

Sign up to request clarification or add additional context in comments.

Comments

-1

Place line by line

log.i(TAG,"OK1");
... code line
log.i(TAG,"OK2");
... code line
log.i(TAG,"OK3");
.
.
.

to find which line of code has NULL error.

3 Comments

This is not the right way to do debugging. You can simply look at the full LogCat trace to discover the line causing the NPE..
I'm use this method and it's easy way
@OmidOmidi Your method was a great way to debug before code stepping debuggers were introduced. I used to solely troubleshoot code doing line-by-line checks like yours because it was difficult to learn debuggers but once I learned them then debugging was WAY more efficient. I suggest googling how to use the eclipse debugger (or whatever java app you're using) and learn it from top to bottom.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.