Results 1 to 5 of 5
  1. #1
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Post 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();
                }
          }
       }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ArrayIndexOutOfBoundsException problem

    I'm assuming that the line causing your error is this one?:
    Java Code:
    priceSold = Double.parseDouble(data[8]);
    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:
          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));
    Another problem is that you are comparing Strings using the == operator:
    Java Code:
    if (data[0] == "X")
       break;
    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 to

    Java Code:
    if (data[0].equals("X")) {
       break;
    }
    You'll also want to enclose all blocks of code including if blocks in curly braces as I've done above.

  3. #3
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default 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

  4. #4
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: ArrayIndexOutOfBoundsException problem

    Quote Originally Posted by Fubarable View Post
    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:
          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));
    I added those lines to see what was going in, and it looks like the program is only taking every other line of data in the text file. Any ideas?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ArrayIndexOutOfBoundsException problem

    Quote Originally Posted by coasterguy10 View Post
    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
    The only variable that can be null on that line and cause a NPE is temp, and this is not surprising:

    Java Code:
    while (temp != null) {
       temp = in.readLine();
       String[] data = temp.split(" ");
    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 = in.readline()) != null) {
       //  temp = in.readLine(); // *** comment or remove this line!
       String[] data = temp.split(" ");

Similar Threads

  1. value of ArrayIndexOutOfBoundsException: 20 ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-28-2011, 04:46 PM
  2. Replies: 11
    Last Post: 02-22-2011, 06:23 PM
  3. ArrayIndexOutOfBoundsException
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 04-07-2010, 07:50 PM
  4. ArrayIndexOutOfBoundsException
    By Corey in forum New To Java
    Replies: 5
    Last Post: 02-02-2010, 02:25 AM
  5. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 06:16 PM

Posting Permissions

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