views:

93

answers:

4

Hey guys,

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!

A: 

If you get a NullPointerException at the line

float original[] = b.getFloatArray("tweets"); 

then the only option is that b is null. Can this.getIntent().getExtras() return null in some cases? you should check it.

Eyal Schneider
+1  A: 

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.

Jon Skeet
+1  A: 
PM - Paresh Mayani
+3  A: 

There is a logic flaw in your makeview method, you need to add the extras to the intent before it's started. Also it's highly recommended to use a constant (GLCamTest.TWEETS) for the key.

public void makeview() {
    Intent myIntent = new Intent(this, GLCamTest.class);
    myIntent.putExtra(GLCamTest.TWEETS, camObjCoord);//assuming camObjCoord is float[]
    this.startActivity(myIntent);
}

And on the other side

Bundle b = this.getIntent().getExtras();
float original[];
if (b!=null) {
    original = b.getFloatArray(GLCamTest.TWEETS);
}
if (original!=null) {
   //do something with original
}
Pentium10