1
votes

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)

4
Do you know which line throws the NullPointerException?Bort
Without telling us where the error is occurring, it's impossible to help you.Brian Roach
What line are you getting the error on? Also: I'm wary of the way you instantiated the array. Unless you can guarantee that mytime will always be less than 100, I would make the array equal to the size of mytime.Otra
I updated.I put the Logcat.Mytime will always be <100.George
what is line 109 in linegraph.javaL7ColWinters

4 Answers

0
votes

if myTime > the size of the myLab array you will get an ArrayIndexOutOfBounds error

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));
             }
0
votes

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));
         }
0
votes

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.

0
votes

My mistake!

I had another activity which was on the middle!I had totally forgotten that!I couldn't see it!

Sorry!

Thank you all for your help and especially L7ColWinters :).