Results 1 to 4 of 4
  1. #1
    blackstyle18 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Array index out of bounds error

    i am trying to select an item in a JList and compare the index to the position in the vector. if they are equal then the element at that postion will be moved then the JList will display the new list without the selected element.

    here is my code

    int i = vehicleJList.getSelectedIndex();
    System.out.println(i);
    try{
    String [] bLine = new String[carVector.size()];
    int p = 0;
    //BufferedReader readCar1 = new BufferedReader(new FileReader(carFile));
    Scanner carScanner1 = new Scanner(carFile);
    while(carScanner1.hasNextLine() == true && carScanner1.next() != null){
    bLine[p] = carScanner1.nextLine();
    while(p <= carVector.size() && (foundCar != true || foundVan != true)){
    for (int o = 0; o < carVector.size(); o++){
    System.out.println(carVector.get(o) + " " +carVector.get(i));
    if(carVector.get(o).equals(carVector.get(i))){
    carVector.removeElementAt(o);
    foundCar = true;
    break;
    }
    else{
    System.out.println("not it");

    }
    }
    for(int o = 0; o < vanVector.size(); o++){
    System.out.println(p);
    System.out.println(vanVector.get(o) + " " + vanVector.get(i));
    if(vanVector.get(o).equals(vanVector.get(i))){
    foundVan = true;
    vanVector.removeElementAt(o);

    break;
    }
    else{
    System.out.println("not it");
    }
    }

    }
    if(foundCar = true){
    vehicleJList.setListData(carVector);
    }
    if(foundVan = true){
    vehicleJList.setListData(vanVector);
    }

    }

  2. #2
    blackstyle18 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default error code

    here is the error i get

    regnum2 VW Golf Manual 2008 Black HatchBack 3 5
    regnnumm1 Vauxall Corsa Manual 2004 Blue HatchBack 5 5
    1
    regnum2 VW Golf Manual 2008 Black HatchBack 3 5 regnnumm1 Vauxall Corsa Manual 2004 Blue HatchBack 5 5
    not it
    regnnumm1 Vauxall Corsa Manual 2004 Blue HatchBack 5 5 regnnumm1 Vauxall Corsa Manual 2004 Blue HatchBack 5 5
    java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by blackstyle18 View Post
    i am trying to select an item in a JList and compare the index to the position in the vector. if they are equal then the element at that postion will be moved then the JList will display the new list without the selected element.

    here is my code
    Java Code:
    int i = vehicleJList.getSelectedIndex();
    System.out.println(i);
    try
    {
    	String [] bLine = new String[carVector.size()];
    	int p = 0;
    
    	//BufferedReader readCar1 = new BufferedReader(new FileReader(carFile));
    	Scanner carScanner1 = new Scanner(carFile);
    
    	while (carScanner1.hasNextLine() && (carScanner1.next() != null))
    	{
    		bLine[p] = carScanner1.nextLine();
    		while ((p <= carVector.size()) && !(foundCar && foundVan))
    		{
    			for (int o = 0; o < carVector.size(); o++)
    			{
    				System.out.println(carVector.get(o) + " " +carVector.get(i));
    				if (carVector.get(o).equals(carVector.get(i)))
    				{
    					carVector.removeElementAt(o);
    					foundCar = true;                      
    					break;
    				}
    				else
    					System.out.println("not it");
    			}
    			for (int o = 0; o < vanVector.size(); o++)
    			{
    				System.out.println(p);
    				System.out.println(vanVector.get(o) + " " + vanVector.get(i));
    				if(vanVector.get(o).equals(vanVector.get(i)))
    				{
    					foundVan = true;
    					vanVector.removeElementAt(o);
    					break;
    				}
    				else
    					System.out.println("not it");
    			}
                    }
    
    		if (foundCar = true)  vehicleJList.setListData(carVector);
    		if (foundVan = true)  vehicleJList.setListData(vanVector);
    	}
    
    	//END OF FRAGMENT

    I thought I should make the code clearer before trying to help you.
    Last edited by Hibernate; 12-28-2010 at 02:32 AM. Reason: Increasing readability

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    I think the problem might be a typo.
    To avoid this problem (if you are using Eclipse) go to
    Window → Preferences → Java → Compiler → Errors/Warnings → Potential programming problems
    and set “Possible accidental boolean assignment (e.g. 'if (a = b)'):” to “Error”.


    Java Code:
    if(foundCar = true){
    vehicleJList.setListData(carVector);
    }
    if(foundVan = true){
    vehicleJList.setListData(vanVector);
    }
    The if-condition will always yield 'true' because you assign the variables
    'foundCar' and 'foundVan' with 'true' which gives the same effect as

    Java Code:
    foundCar = true;
    vehicleJList.setListData(carVector);
    foundVan = true;
    vehicleJList.setListData(vanVector);
    }

Similar Threads

  1. Help Array Index out of bounds exception
    By star400040 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 10:24 PM
  2. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-18-2010, 11:25 PM
  3. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 04:02 PM
  4. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM
  5. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM

Posting Permissions

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