Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null pointer exception when using Bundle to pass data

I've got a float array camObjCoord declared as..

public static float camObjCoord[] = new float[8000];

I'm then filling it's indexes in a class that does something like the following..

camObjCoord[1] = 2.5;

I'm then calling makeview()

       public void makeview() {
    Intent myIntent = new Intent(this, GLCamTest.class);
    this.startActivity(myIntent);
    Bundle b = new Bundle();
    b.putFloatArray("tweets", camObjCoord);
}

and then in the new class it's doing...

               public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle b = this.getIntent().getExtras();
    float original[] = b.getFloatArray("tweets");
    camObjCoord = original;
    counter++;
}   

But... I'm getting a Null pointer Exception at float original[] = b.getFloatArray("tweets"); and I don't know why. I've tried bundling before calling the intent etc. but I've had no luck at a fix. Anyone know why?

I've also included some of the error incase any of you are interested.

            07-14 11:14:35.592: ERROR/AndroidRuntime(7886): Caused by:  java.lang.NullPointerException
            07-14 11:14:35.592: ERROR/AndroidRuntime(7886):     at  org.digital.com.GLCamTest.onCreate(GLCamTest.java:41)
            07-14 11:14:35.592: ERROR/AndroidRuntime(7886):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            07-14 11:14:35.592: ERROR/AndroidRuntime(7886):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
            07-14 11:14:35.592: ERROR/AndroidRuntime(7886):     ... 11 more

Thanks!

like image 361
Skizit Avatar asked Jul 14 '10 10:07

Skizit


1 Answers

Okay, so that suggests that this.getIntent().getExtras() has returned null. Note that in makeview you haven't done anything after creating the bundle. Do you need to do:

myIntent.putExtras(b);

at the end perhaps? (I'm not an Android developer so I don't know the API, but that sounds likely...)

EDIT: As others have pointed out, you should potentially put the startActivity call after setting everything on the intent.

like image 51
Jon Skeet Avatar answered Sep 28 '22 17:09

Jon Skeet