NullPointerException using arrays
I have in one activity:
...
double []mylab=new double [100];
public void compute(){
...
double mytime=Double.parseDouble(timing.getText().toString().trim());
//fill array
for (int i=0;i<=mytime;i++){
mylab[i]=Math.exp(i);
//Arrays.fill(mylab,Math.exp(i));
}
...
i.putExtra("mylab",mylab);
startActivity(i);
}
and in the linegraph activity:
...
private double [] mylab =new double [100];
public double [] getmylab(){ return this.mylab;}
public void setmylab(double [] mylab){ this.mylab=mylab;}
...
public void onCreate(Bundle savedInstanceState){
Bundle extras=getIntent().getExtras();
double [] mylab=extras.getDoubleArray("mylab");
setmylab(mylab);
..
public Intent getIntent(Context context){
double []mylab=getmylab();
ArrayList<Double> x =new ArrayList<Double>();
ArrayList<Double> y =new ArrayList<Double>();
//fill x,y values
for (int i=0;i<=20;i++){
x.add(mytime/i);
}
for (int i=0;i<=20;i++){
y.add(mylab[i]);
}
...
I suppose the error lies where i fill the array?
-------------Logcat------------------------------
FATAL EXCEPTION: main E/AndroidRuntime(461):
java.lang.RuntimeException: Unable to start activity ComponentInfo
java.lang.NullPointerException E/AndroidRuntime(461): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(461): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(461): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(461): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(461): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(461): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(461): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(461): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(461): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(461): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(461): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(461): at dalvik.system.NativeStart.main(Native Method) E/AndroidRuntime(461): Caused by:
java.lang.NullPointerException E/AndroidRuntime(461): at com...LineGraph.getIntent(LineGraph.java:109) E/AndroidRuntime(461):
at com..LineGraph.onCreate(LineGraph.java:80) E/AndroidRuntime(461):
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(461): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
如果myTime> myLab数组的大小,您将得到一个ArrayIndexOutOfBounds错误
double mytime=Double.parseDouble(timing.getText().toString().trim());
//fill array
for (int i=0;i<=mytime;i++)
mylab[i]=Math.exp(i);
//Arrays.fill(mylab,Math.exp(i));
}
The error occurs because you are accessing an array index which does not exist. Most likely it's when you are filling the array.
// make sure `mytime` is less or equal than 100
for (int i=0;i<=mytime;i++){
mylab[i]=Math.exp(i);
//Arrays.fill(mylab,Math.exp(i));
}
Are you maybe calling
Intent getIntent()
on a newly created instance of the class? Because that might operate on a mylab
that is null
since the field is initialized as null at creation of each instance.