-1

Testing a Android app on physical phone. Im using Android Studio. I'm not sure why I'm getting java.lang.IndexOutOfBoundsException:

Process: com.packtpub.libgdx.outtacluck.android, PID: 27731
java.lang.IndexOutOfBoundsException: Invalid index 4, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.packtpub.libgdx.outtacluck.game.WorldController.highScoreCheck(WorldController.java:1048)
        at com.packtpub.libgdx.outtacluck.game.WorldController.update(WorldController.java:375)
        at com.packtpub.libgdx.outtacluck.screens.GameScreen.render(GameScreen.java:40)
        at com.packtpub.libgdx.outtacluck.screens.DirectedGame.render(DirectedGame.java:67)
        at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:424)
        at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1531)
        at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248)

But my nothing ever gets added to the scoresData array

Here's the class. I declared it the same

public class ScoreManager {


public  List<Double> scoresData = new ArrayList<Double>();


public ScoreManager()
{
try{
        // Open the file that is the first
        FileInputStream fstream = new FileInputStream(Constants.SCORES_TRACKER);

        // Use DataInputStream to read binary NOT text.
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

        String strLine;


        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
            // Add number from file to list
            scoresData.add( Double.parseDouble(strLine));
        }
        //Close the input stream
        br.close();

        System.out.println(scoresData);
    }catch (Exception e){
        e.printStackTrace();
      }
    System.out.println("scoresData.size is " + scoresData.size());

    }

public static double parseInt(String input) throws NullPointerException, ParseException{
    if(input == null){
        throw new NullPointerException();
    }

    input = input.trim();

    NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
    ParsePosition parsePosition = new ParsePosition(0);
    Number number = numberFormat.parse(input, parsePosition);

    if(parsePosition.getIndex() != input.length()){
        throw new ParseException("Invalid input", parsePosition.getIndex());
    }

    return number.doubleValue();
}


public int getArrayElement(int index)
{
    if(scoresData.isEmpty())
    {
        System.out.println("scoresData is empty");
        return 0;
    }
    else
    {
        double x = scoresData.get(index);
        return (int) x;
    }
  }
}
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
  • What's on this line: `at com.packtpub.libgdx.outtacluck.game.WorldController.highScoreCheck(WorldController.java:1048)` ? – Tudor Luca Mar 05 '16 at 23:52
  • line1048 is double lowestTopScore = scoreManager.scoresData.get(4); //arraylist should always be sorted so I grab the last element – user3846448 Mar 05 '16 at 23:58
  • 1
    Well, there is your problem. You're trying to get an item out of an ArrayList which is empty. Also, read this on how to use a List: http://stackoverflow.com/questions/6231973/difference-between-list-list-listt-liste-and-listobject – Tudor Luca Mar 06 '16 at 00:01
  • OP, you still haven't shown us where `getArrayElement()` is called from, but hopefully the answer is now clear. – Andrew Regan Mar 06 '16 at 00:09

1 Answers1

-2

I just tested this and it populates the arraylist just fine. i don't think your arraylist declaration is correct according to the above code. Try this.

List<Double> scoresData = new ArrayList<Double>();
neal
  • 880
  • 6
  • 11
  • How does this answer the question / explain the error? – Andrew Regan Mar 06 '16 at 00:01
  • I edited my qustion to include the class. I think I declared it the same way – user3846448 Mar 06 '16 at 00:05
  • it's helpful to understand if the data is getting populated in the first place. Of course you're going to get an out of bounds exception if there's no data in the list. So, to begin with, ensure data is being populated into the list. Next, if data is populated, determine why there is no data in that index. It's called testing. – neal Mar 06 '16 at 00:05
  • Neal this tested just fine when I ran it as a desktop Application. Only had probs when I tried running as Android – user3846448 Mar 06 '16 at 00:09