开发者

Null pointer exception when using Bundle to pass data

开发者 https://www.devze.com 2023-01-07 07:01 出处:网络
I\'ve got a float array camObjCoord declared as.. public static float camObjCoord[] = new float[8000];

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, GLCamTe开发者_JS百科st.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!


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.


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
}


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

}

I think error you made is that you have started the activity this.startActivity(myIntent); and after that you are passing the bundle value

 Bundle b = new Bundle();
    b.putFloatArray("tweets", camObjCoord);

. ---->>> so it will pass nothing. so on receiver side

Bundle b = this.getIntent().getExtras();
    float original[] = b.getFloatArray("tweets");

---->>> this above code receive "null" value.

So try to bundle first and then start your activity.

Try this:

  public void makeview() {
    Intent myIntent = new Intent(this, GLCamTest.class);

    Bundle b = new Bundle();
    b.putFloatArray("tweets", camObjCoord);

this.startActivity(myIntent);

}


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.


this.getIntent().getExtras() has returned null. Make sure that in makeview you haven't done anything after creating the bundle. Do you need:

myIntent.putExtras(b);

at the end


Well, I had similiar problem. in my case the null point exception was happen when I checked if my bundle.getString() was equall to null.

here is how IN MY CASE I solved it:

Intent intent = getIntent();        
    if(intent.hasExtra("nomeUsuario")){
        bd = getIntent().getExtras();
        if(!bd.getString("nomeUsuario").equals(null)){
            nomeUsuario = bd.getString("nomeUsuario");
        }
    }


Try this , it works for me

 Intent i = getIntent();

   if (i == null) 
       Log.d(" DEBUG AGILANBU ", "Here the intent value was null");           
   else
       Log.d(" DEBUG ", "Intent OK");

   String strGetVal  = i.getStringExtra("PackageName"); // Here we set an exception points to this line
   Log.d("*** DEBUG", rec + " " + strGetVal);

Happie Coding

0

精彩评论

暂无评论...
验证码 换一张
取 消