Results 1 to 5 of 5
- 10-21-2011, 09:57 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
ArrayIndexOutOfBoundsException problem
Im working on a program that is supposed to read in a Car or SoldCar object and add it to an array. Some other methods will follow but right now im having trouble getting the program to read in my data. We cant use scanner so this way is different for me. The program compiles, but then throws an ArrayIndexOutOfBoundsException at line 67 and i dont know why. If someone could take a look and tell me if what I have written is even setting me in the right direction. Btw, I dont use ArrayList very much, so I dont know if im even adding the objects into the ArrayList correctly. Thanks
Java Code:import java.util.ArrayList; import java.io.FileReader; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; public class SellingCars { public static void main(String[] args) { String type; int id, modelYear; double dealerCost; double priceSold; Date dateArrived, dateSold; String customer; String makeMod; ArrayList<Car> cars = new ArrayList<Car>(); FileReader reader=null; Car car; SoldCar sCar; String temp=""; try { reader = new FileReader("cars.txt"); BufferedReader in = new BufferedReader(reader); while(temp != null) { temp =in.readLine(); String[] data = temp.split(" "); if (data[0] == "X") break; else if (data[0] == "C") { dealerCost = Double.parseDouble(data[1]); id = Integer.parseInt(data[2]); dateArrived = new Date(data[3], Integer.parseInt(data[4]) , Integer.parseInt(data[5])); modelYear = Integer.parseInt(data[6]); makeMod = data[7]; car = new Car(dealerCost, id, dateArrived, modelYear, makeMod); cars.add(car); } else if (data[0] == "S1") { id = Integer.parseInt(data[1]); priceSold = Double.parseDouble(data[2]); customer = data[3]; dateSold = new Date(data[4], Integer.parseInt(data[5]) , Integer.parseInt(data[6])); sCar = new SoldCar(id, priceSold, customer, dateSold); cars.add(sCar); } else { dealerCost = Double.parseDouble(data[1]); id = Integer.parseInt(data[2]); dateArrived = new Date(data[3], Integer.parseInt(data[4]) , Integer.parseInt(data[5])); modelYear = Integer.parseInt(data[6]); makeMod = data[7]; priceSold = Double.parseDouble(data[8]); customer = data[9]; dateSold = new Date(data[10], Integer.parseInt(data[11]) , Integer.parseInt(data[12])); sCar = new SoldCar(dealerCost, id, dateArrived, modelYear, makeMod, priceSold, customer, dateSold); cars.add(sCar); } temp = in.readLine(); } } catch (IOException ioe) { ioe.printStackTrace(); } } }
-
Re: ArrayIndexOutOfBoundsException problem
I'm assuming that the line causing your error is this one?:
You are working with hard-coded array indices (for example the [8] above is a hard-coded number), but are doing so without knowing that the array contains at least 9 or more members. You can test this out, and it would be a good idea for you to do this, if only to see what your program is doing. I would add the following lines in your code above:Java Code:priceSold = Double.parseDouble(data[8]);
Another problem is that you are comparing Strings using the == operator:Java Code:try { reader = new FileReader("cars.txt"); BufferedReader in = new BufferedReader(reader); while (temp != null) { temp = in.readLine(); String[] data = temp.split(" "); // *** added lines of code: System.out.println("data length: " + data.length); // how many items are really in the array System.out.println("show data: " + java.util.Arrays.toString(data));
Note that == checks if two objects are one and the same, and that's not what you care about here. Rather you want to know if the data[0] String contains one char only and that char is 'X', and to find that out, you'll want to use the equals method. So change that line toJava Code:if (data[0] == "X") break;
You'll also want to enclose all blocks of code including if blocks in curly braces as I've done above.Java Code:if (data[0].equals("X")) { break; }
- 10-21-2011, 10:27 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Re: ArrayIndexOutOfBoundsException problem
Ok i fixed the == problem, that was an oversight on my part. But now I am getting a NullPointerException at line 35, where I split the data up
- 10-21-2011, 10:31 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
-
Re: ArrayIndexOutOfBoundsException problem
The only variable that can be null on that line and cause a NPE is temp, and this is not surprising:
You check that temp isn't null when the while loop begins, but then read in a value immediately at the start of the loop, and that value could be null. The solution is to read in the value in the while boolean statement itself:Java Code:while (temp != null) { temp = in.readLine(); String[] data = temp.split(" ");
Java Code:while ((temp = in.readline()) != null) { // temp = in.readLine(); // *** comment or remove this line! String[] data = temp.split(" ");
Similar Threads
-
value of ArrayIndexOutOfBoundsException: 20 ?
By aneuryzma in forum New To JavaReplies: 1Last Post: 03-28-2011, 03:46 PM -
Error: ArrayIndexOutOfBoundsException. Where is the problem?
By ArticRoot in forum New To JavaReplies: 11Last Post: 02-22-2011, 05:23 PM -
ArrayIndexOutOfBoundsException
By er1c550n20 in forum New To JavaReplies: 2Last Post: 04-07-2010, 06:50 PM -
ArrayIndexOutOfBoundsException
By Corey in forum New To JavaReplies: 5Last Post: 02-02-2010, 01:25 AM -
ArrayIndexOutofBoundsException help
By filly444 in forum New To JavaReplies: 9Last Post: 09-03-2008, 05:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks