Results 1 to 3 of 3
  1. #1
    j4sbass is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default IndexOutOfBoundsException need some help please...

    Im writing some code where I am just trying to move an object with keys right now. I can display it and move it, but it seems to randomly stop and receive and error wile doing so:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 1
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Input.getKey(Input.java:24)
    at GameObject.Update(GameObject.java:45)
    at Game.run(Game.java:26)
    at Main.main(Main.java:8)

    Index and size are rarely the same 2 numbers, Ive seen it go as high as 72 and as low as 0. Heres the code where the problem is, i have KeyAdapter attached to my JFrame, the error highlights on line 8, this line of my getKey() function:
    if (getInputBuffer().get(i) != null && getInputBuffer().get(i).getKeyCode() == asciiCode)

    Java Code:
    public static boolean getKey(int asciiCode)
    	{
    		getInstance();
    		for(int i = getInputBuffer().size() - 1; i >= 0; i--)
    		{
    			if (i >= 0 && i < getInputBuffer().size())
    			{
    				if (getInputBuffer().get(i) != null && getInputBuffer().get(i).getKeyCode() == asciiCode)
    				{
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    	
    	public void keyPressed(KeyEvent e)
    	{
    		getInputBuffer().add(e);
    	}
    	
    	public void keyReleased(KeyEvent e)
    	{
    		for(int i = getInputBuffer().size() - 1; i >= 0; i--)
    		{
    			if (e.getKeyCode() == getInputBuffer().get(i).getKeyCode())
    			{
    				getInputBuffer().remove(i);
    			}
    		}
    	}
    	
    	public static Input getInstance()
    	{
    		if (instance == null)
    			{
    				instance = new Input();
    			}
    		return instance;
    	}
    	
    	public static List<KeyEvent> getInputBuffer()
    	{
    		if(inputBuffer == null)
    		{
    			inputBuffer = new ArrayList<KeyEvent>();
    		}
    		return inputBuffer;
    	}
    any help please?
    Last edited by j4sbass; 10-10-2012 at 10:08 AM.

  2. #2
    j4sbass is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: IndexOutOfBoundsException need some help please...

    I changed it to using an array list and it worked for a wile, but now it just takes longer to glitch out. still will do it every once in a wile.

    any suggestions?
    Last edited by j4sbass; 10-11-2012 at 03:33 AM.

  3. #3
    taherrah is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default Re: IndexOutOfBoundsException need some help please...

    I am taking a grad class in Java using threads, I am getting this same error message i am posting my code below and the output message. If anyone can help
    My code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class Sort{ //start of the sort class
    private static String[] arrThread;
    /**
    * You are to implement this method. The method should invoke one or more
    * threads to read and sort the data from the collection of Files. The
    * method should return a sorted list of all of the String data contained
    * in the files.
    * @param files
    * @return
    * @throws IOException
    */


    public static String[] threadedSort(File[]files)throws IOException {
    //Defining and Starting a thread
    {
    Thread T;
    T = new Thread(new someMethod("Parameter s"));
    T.start(); //Starts the thread process
    try{
    T.join();
    }
    catch(InterruptedException ex){
    Logger.getLogger(Sort.class.getName()).log(Level.S EVERE,null,ex);
    }

    String[] arrThread = new String[0]; // initialized arrThread
    System.out.println("File value at index: " + arrThread);
    //code to print out results from sorted files

    return arrThread;

    }
    }
    public static class someMethod extends Thread{ //this is a constructor call
    String s;
    someMethod (String str)
    {
    s = str;
    System.out.println("Hello");
    }

    @Override
    @SuppressWarnings("empty-statement")
    public void run() {
    int n = 0;
    System.out.println(file());
    //if file() returns a printable value then this is correct T.yield();
    for(int i=0; i<(n-1); i++)
    {
    System.out.println(file());

    }
    //throw new UnsupportedOperationException("Not supported yet.");
    }

    private boolean file() {
    return false;
    //throw new UnsupportedOperationException("Not yet implemented");
    }
    }

    /**
    * Given an array of files, this method will return a sorted list of the String data contained in each of the files.
    *
    * @param files the files to be read
    * @return the sorted data
    * @throws IOException thrown if any errors occur reading the file
    */
    public static String[] sort(File[] files) throws IOException {

    String[] sortedData = new String[0];

    for (File file : files) {
    String[] data = getData(file);
    data = MergeSort.mergeSort(data);
    sortedData = MergeSort.merge(sortedData, data);
    }

    return sortedData;

    }

    /**
    * This method will read in the string data from the specified file and return the data as an array of String objects.
    *
    * @param file the file containing the String data
    * @return String array containing the String data
    * @throws IOException thrown if any errors occur reading the file
    */
    private static String[] getData(File file) throws IOException {

    ArrayList<String> data = new ArrayList<String>();
    BufferedReader in = new BufferedReader(new FileReader(file));

    // Read the data from the file until the end of file is reached
    while (true) {
    String line = in.readLine();
    if (line == null) {
    // the end of file was reached
    break;
    }
    else {
    data.add(line);
    }
    }

    //Close the input stream and return the data
    in.close();
    return data.toArray(new String[0]);

    }

    private static class printable {

    public printable() {
    }
    }

    private static class is {

    public is() {
    }
    }
    }

    Output file
    run:
    The data returned by Sort.sort is sorted
    Hello
    false
    File value at index: [Ljava.lang.String;@2b49a2c8
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at SortTest.main(SortTest.java:24)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

Similar Threads

  1. IndexOutOfBoundsException
    By PorgrammingNoob117 in forum AWT / Swing
    Replies: 2
    Last Post: 05-17-2011, 03:29 AM
  2. Replies: 4
    Last Post: 12-27-2010, 04:43 PM
  3. IndexOutOfBoundsException
    By Luciform in forum New To Java
    Replies: 8
    Last Post: 05-24-2010, 04:44 AM
  4. IndexOutOfBoundsException with AWT-EventQueue-0
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 03-10-2009, 02:52 PM
  5. IndexOutOfBoundsException
    By aldo1987 in forum New To Java
    Replies: 6
    Last Post: 04-30-2008, 04:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •