SoulMagnet
SoulMagnet

Reputation: 173

Android NoSuchElementException and Scanner

I am trying create an integer array for my app by reading a txt file from raw folder. The txt has number for every line, looks like this:

test.txt

1
3
4
5
6
2
-9
...

My array is intended to be like this Integer[] vector = {1,2,3,4,5,6,2,-9,...}

The code I am deplouying (which is actually inside a click callback method):

public void plotSPLinfBaffle(View v) {
    List<Integer> vector = new ArrayList<Integer>();

    InputStream in = getResources().openRawResource(R.raw.test);
    Scanner sc = new Scanner(new InputStreamReader(in));

    while(sc.hasNext()){

        vector.add(Integer.parseInt(sc.nextLine()));
    }

    Log.d("clicked" , sc.nextLine());

    Integer[] splVector = vector.toArray(new Integer[0]);
}

You see, I keep getting this run time error and logcat messages:

02-21 14:04:35.599: D/AndroidRuntime(19925): Shutting down VM
02-21 14:04:35.599: W/dalvikvm(19925): threadid=1: thread exiting with uncaught exception (group=0x40018578)
02-21 14:04:35.619: E/AndroidRuntime(19925): FATAL EXCEPTION: main
02-21 14:04:35.619: E/AndroidRuntime(19925): java.lang.IllegalStateException: Could not execute method of the activity
02-21 14:04:35.619: E/AndroidRuntime(19925):    at android.view.View$1.onClick(View.java:2144)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at android.view.View.performClick(View.java:2485)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at android.view.View$PerformClick.run(View.java:9080)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at android.os.Handler.handleCallback(Handler.java:587)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at android.os.Looper.loop(Looper.java:130)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at java.lang.reflect.Method.invokeNative(Native Method)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at java.lang.reflect.Method.invoke(Method.java:507)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at dalvik.system.NativeStart.main(Native Method)
02-21 14:04:35.619: E/AndroidRuntime(19925): Caused by: java.lang.reflect.InvocationTargetException
02-21 14:04:35.619: E/AndroidRuntime(19925):    at java.lang.reflect.Method.invokeNative(Native Method)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at java.lang.reflect.Method.invoke(Method.java:507)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at android.view.View$1.onClick(View.java:2139)
02-21 14:04:35.619: E/AndroidRuntime(19925):    ... 11 more
02-21 14:04:35.619: E/AndroidRuntime(19925): Caused by: java.util.NoSuchElementException
02-21 14:04:35.619: E/AndroidRuntime(19925):    at java.util.Scanner.nextLine(Scanner.java:1417)
02-21 14:04:35.619: E/AndroidRuntime(19925):    at com.soulmagnet.speakerparametrix2.PlotInfBaffle.plotSPLinfBaffle(PlotInfBaffle.java:136)
02-21 14:04:35.619: E/AndroidRuntime(19925):    ... 14 more

Still, when in debug mode placing break-points in line:

22 vector.add(Integer.parseInt(sc.nextLine()));

I can see the vector variable in every iterration but when its done filling up something kills my activity...

I just cant understand whats going wrong, thats why I am seeking help!

Thank you in advance!

Upvotes: 2

Views: 2305

Answers (3)

andr
andr

Reputation: 16064

Look at the stacktrace carefully and at the documentation of the Scanner.nextLine() method.

You are eating all the lines in the input file in this loop:

while (sc.hasNext()) {
    vector.add(Integer.parseInt(sc.nextLine()));
}

Then however, in this line:

Log.d("clicked" , sc.nextLine());

you call nextLine() on an input that has already been ended. As per documentation of Scanner.nextLine():

Throws:

NoSuchElementException - if no line was found

this call causes the Scanner to throw NoSuchElementException. Simple remove the Log.d() line - or remember last element in the loop and then log its value to LogCat (if that is your intention).

Upvotes: 3

Daniel  Magnusson
Daniel Magnusson

Reputation: 9674

Do you see "clicked" in your log?

if you can, everything is fine up until this log print.

Interesting part in your log;

14:04:35.619: E/AndroidRuntime(19925): ... 11 more 02-21 14:04:35.619: E/AndroidRuntime(19925): Caused by: java.util.NoSuchElementException 02-21 14:04:35.619: E/AndroidRuntime(19925): at java.util.Scanner.nextLine(Scanner.java:1417) 02-21 14:04:35.619: E/AndroidRuntime(19925): at com.soulmagnet.speakerparametrix2.PlotInfBaffle.plotSPLinfBaffle(PlotInfBaffle.java:136) 02-21 14:04:35.619: E/AndroidRuntime(19925): ... 14 more

Documentation for Scanner says this exception will be thrown when there is no new line.

This is your problem

Log.d("clicked" , sc.nextLine()); 

Upvotes: 0

Veger
Veger

Reputation: 37915

After your while-loop, sc does not contain lines anymore (hence the loop exited).

Later you have

 Log.d("clicked" , sc.nextLine());

This tries to grab another line (which is not present anymore), resulting in the error:

E/AndroidRuntime(19925): Caused by: java.util.NoSuchElementException
E/AndroidRuntime(19925):    at java.util.Scanner.nextLine(Scanner.java:1417)
E/AndroidRuntime(19925):    at com.soulmagnet.speakerparametrix2.PlotInfBaffle.plotSPLinfBaffle(PlotInfBaffle.java:136)

(which is somewhat hidden in the complete error log)

So, remove that line, and it should work properly.

Upvotes: 2

Related Questions