Holly
Holly

Reputation: 1976

Diagnose a "RuntimeException: Unable to start activity Caused by: java.lang.NullPointerException"

I don't know where to turn, i was following an android tutorial here and upon the latest update i'm getting a runtime error and i'm really struggling to find the source of it!

What im aiming for is a small "orb" icon to appear on the screen in the lop left, that the player can drag and drop across the surface.

Now i've only made a few changes to the program since i last compiled it successfully. All of those were in the GameView class which you can see here: http://pastebin.com/zsYKFmuP. And i also created a brand new class called Orb which is here: http://pastebin.com/wQKqcVtV

If you have experience with android development then looking over my code shouldn't be too troublesome however i thank anyone who takes the time to make any suggestion! All advice will be appreciated!

The logcat has this to say:

 FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{biz.hireholly.tutorial/biz.hireholly.tutorial.MainActivity}: java.lang.NullPointerException
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:130)
     at android.app.ActivityThread.main(ActivityThread.java:3701)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:507)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
     at biz.hireholly.tutorial.models.Orb.<init>(Orb.java:13)
     at biz.hireholly.tutorial.GameView.<init>(GameView.java:34)
     at biz.hireholly.tutorial.MainActivity.onCreate(MainActivity.java:25)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
     ... 11 more

The relevant source from the Orb class:

public class Orb {

        private Bitmap bitmap;  //image
        private int x;  //x coord
        private int y;  //y coord
        private boolean touched; //if orb is touched/picked up
        //just to make things simpler to read
        int halfBmpX = bitmap.getWidth() /2; // <<<=== Line 13
        int halfBmpY = bitmap.getHeight() /2;

Upvotes: 0

Views: 680

Answers (4)

MByD
MByD

Reputation: 137362

You try to call a method on bitmap before it was assigned, and it is still null:

int halfBmpX = bitmap.getWidth() /2;

You should move the following two lines inside your constructor:

halfBmpX = bitmap.getWidth() /2;
halfBmpY = bitmap.getHeight() /2;

and keep the declaration of them outside the constructor:

int halfBmpX, halfBmpY;

Upvotes: 2

gotomanners
gotomanners

Reputation: 7916

In Line 13 & 14 of your Orb class you do this

int halfBmpX = bitmap.getWidth() /2;
int halfBmpY = bitmap.getHeight() /2;

when bitmap hasn't been initialized and is null hence the Null Pointer Exception.

Move the right bitmap.getWidth() /2; bits inside the constructor, after bitmap has been initialized.

Upvotes: 1

Jon Newmuis
Jon Newmuis

Reputation: 26502

You have the following code in your Orb class:

private Bitmap bitmap;  //image
private int x;  //x coord
private int y;  //y coord
private boolean touched; //if orb is touched/picked up
//just to make things simpler to read
int halfBmpX = bitmap.getWidth() /2;
int halfBmpY = bitmap.getHeight() /2;

You've made two references to the bitmap object without initializing it. You should set halfBmpX and halfBmpY in the constructor instead.

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160191

Your Orb class attempts to initialize halfBmpX (and Y) from a null reference.

That initialization should take place in the constructor after you have a valid Bitmap to initialize from.

public Orb(Bitmap bmp, int x, int y) {
    this.bitmap = bmp;
    this.x = x;
    this.y = y;
    halfBmpX = bmp.getWidth() / 2;
    halfBmpY = bmp.getHeight() / 2;
}

Explanation: those initializations at the beginning of the class are run before the constructor runs, so bitmap doesn't exist as anything other than a null reference.

Upvotes: 2

Related Questions