1

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)

George
  • 5,808
  • 15
  • 83
  • 160
  • Do you know which line throws the NullPointerException? – Bort Feb 01 '12 at 16:26
  • Without telling us *where* the error is occurring, it's impossible to help you. – Brian Roach Feb 01 '12 at 16:28
  • 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 Feb 01 '12 at 16:28
  • I updated.I put the Logcat.Mytime will always be <100. – George Feb 01 '12 at 16:40
  • what is line 109 in linegraph.java – L7ColWinters Feb 01 '12 at 16:51
  • @L7ColWinters:It's the " y.add(mylab[i]); "in the for loop. – George Feb 01 '12 at 16:57
  • the local mylab array is obviously null, where is your extras intent defined? Oh and by the way on your first post of code you iterate over the loop checking whether an int is less than a double, make sure to only check an int vs int, make a temp if you have to right before the loop. – L7ColWinters Feb 01 '12 at 17:09
  • @L7ColWinters:You mean like that? int temp=(int)(mytime); and then for (int i=0;i<=temp;i++){.. Still the same with that.(The extras i have them in the onCreate) – George Feb 01 '12 at 17:30
  • yeah, but where are you getting the extras, i don't see it being assigned.. – L7ColWinters Feb 01 '12 at 17:46
  • @L7ColWinters :I have "Bundle extras=getIntent().getExtras();" (updated) .I am not sure if you mean that,sorry. – George Feb 01 '12 at 17:50
  • maybe? http://stackoverflow.com/questions/5944503/android-getintent-getextras-returns-null – L7ColWinters Feb 01 '12 at 17:55
  • @L7ColWinters:No,it's not like that ,unfortunatelly.. – George Feb 01 '12 at 17:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7247/discussion-between-l7colwinters-and-george) – L7ColWinters Feb 01 '12 at 18:12

4 Answers4

0

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));
             }
blank
  • 17,852
  • 20
  • 105
  • 159
  • :mytime will always be <100 . – George Feb 01 '12 at 16:40
  • ... maybe. There's just not enough info to find out why - tell us which line the error is happening on. I can only guess the getmylab() is returning null. – blank Feb 01 '12 at 19:38
0

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));
         }
poitroae
  • 21,129
  • 10
  • 63
  • 81
0

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.

G. Bach
  • 3,869
  • 2
  • 25
  • 46
0

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 :).

George
  • 5,808
  • 15
  • 83
  • 160