0

I'm making my very first text adventure game but keep getting an error thrown at me. I honestly have no clue what's going on or how to even start fixing the issue. I'll post my code and the error message I'm receiving exactly.

PFont computer; //creates a variable for the font to be used
int level = 0; //starting level
int branch = 0; //starting level

String[][] textOptions = new String[][]{
  {"Welcome to our text adventure. You are in a room.\n" //new line
  + "It looks like no one has been in here for years.\n"
  + "Would you like to look around? Y/N",""},

  {"You decide to look around the room.\n"
  + "You stand by your initial observation.\n"
  + "The room is covered in a thick layer of dust with cobwebs reaching out in every corner.\n"
  + "There are a few shelves, lined with decaying boxes and files. The terminal you are looking for is nowhere to be seen. \n"
  + "You are now in a hallway. There are two rooms to explore. L/R?",
    "You decide to leave this room. You are now in a hallway.\n"
  + "There are two rooms to explore. L/R?"},

  {"You decide to check the room to the right.",
   "You decide to check the room to the left."},

    };

void setup()
{
  size(1350,700);
  computer = createFont("computer.ttf",25);
}

void draw()
{
  background(0);
  textSize(20);
  textFont(computer);
  text(textOptions[level][branch],10,25);
  fill(0,239,31);
}

void keyPressed()
{
  if(key == 'y')
  {
    println("Player is looking around.");

    if(level < 2)
    {
      level = level+ 1;
      branch = 0;
    }
  }
  else if(key == 'n')
  {
    println("Player decided to leave this room.");

    if(level < 2)
    {
      level = level+ 1;
      branch = 1;
    }
  }

{
    if(key == 'r')
  {
    println("Player has chosen the room to the right.");

    if(level < 3)
    {
      level = level+ 1;
      branch = 2;
    }
  }
  else if(key == 'l')
  {
    println("Player has chosen the room to the left.");

    if(level < 3)
    {
      level = level+ 1;
      branch = 3;
    }
  }
}
}

I keep getting the error code:

java.lang.ArrayIndexOutOfBoundsException: 3
    at sketch_171010c.draw(sketch_171010c.java:50)
    at processing.core.PApplet.handleDraw(PApplet.java:2437)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

and it appears to be highlighting the portion of draw that says textOptions[level][branch] but I still don't understand what this means or how to fix this issue whatsoever. Any help or input on how to literally do anything would greatly be appreciated. Thank you!

  • https://docs.oracle.com/javase/8/docs/api/java/lang/ArrayIndexOutOfBoundsException.html – Robby Cornelissen Oct 11 '17 at 04:32
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Krease Oct 17 '17 at 16:57

1 Answers1

0

You should get into the habit of debugging your code so you understand exactly what it's doing.

The exception is caused by trying to access an index that an array doesn't have. Here's a simpler example:

String[] array = {"test"};
String x = array[100];

This code would throw an ArrayIndexOutOfBoundsException because the array only has a single element, but we're trying to access index 100. That's what's happening in your code.

Now, why that's happening is going to require debugging your code. Step through it with a piece of paper and a pencil to track the value of the variables, or use the debugger that comes with the Processing IDE.

Kevin Workman
  • 39,413
  • 8
  • 61
  • 94
  • I've solved the error being thrown but now I'm just having the issue of the 2nd key input not being recognized? Like, my game only recognizes the Y or N input for the options, when I try to put L or R it doesn't progress. – Desiree Cenname Oct 12 '17 at 20:44
  • @DesireeCenname It's still the same answer: you have to **debug** your code to understand exactly what's happening. Then if you get stuck, post a [mcve] instead of your whole project. Good luck. – Kevin Workman Oct 12 '17 at 20:47