Inserting into an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vitaminz
    New Member
    • Aug 2008
    • 11

    Inserting into an object

    Hi, I am currently designing a program and I've run into a problem - It's probably really easy to solve, but heck it's late and I can't think lol.

    I'm designing a program to store details of cars (i.e. a garage), and the car details are all stored in a text file, and the text file is terminated with ****. The program first loads the existing car details using a BufferedReader, then each cars details is converted into a position in the carDetails object. The user can then perform manipulation to the data, delete a car, add a new car etc. Then when a manipulation is complete, the data is written back to the text file using a PrintWriter.

    I'm currently trying to add a new car, and I have this code to do so:

    Code:
                tempRegNo = Text.readString("Please enter the new cars registration number.");
                    tempManufacturer = Text.readString("Please enter the manufacturer");
                    tempModel = Text.readString("Please enter the model");
                    tempYear = Text.readInt("Please enter the year");
                    tempPrice = Text.readInt("Please enter the price");
                    
                    carDetails[activeCars].setRegNo(tempRegNo);
                    carDetails[activeCars].setManufacturer(tempManufacturer);
                    carDetails[activeCars].setModel(tempModel);
                    carDetails[activeCars].setYear(tempYear);
                    carDetails[activeCars].setPrice(tempPrice);
                    activeCars++;
    The activeCars variable comes from the method where the details are loaded from the text file - each time it loads a car, activeCars increments by 1.

    But when I run this, I enter the new cars details, and I get a 'NullPointerExc eption' error. I'm assuming it's looking in the wrong position or something, but I honestly can't figure it out at the moment...just wondering if anyone can offer any pointers?

    Hopefully that doesn't sound too confusing :p

    Thanks
  • Toothpick
    New Member
    • Aug 2008
    • 5

    #2
    did you define the array?

    example Object[] cars = new cars[6];

    and not just Object[] cars;

    Comment

    • vitaminz
      New Member
      • Aug 2008
      • 11

      #3
      Thanks for the reply :)

      Yeah, I have this in the public class:

      private static final int MAX_CARS = 10;
      private Car [] carDetails = new Car[MAX_CARS];

      and in the database currently is only 4 cars, so it hasn't reached the max :-?

      Comment

      • Toothpick
        New Member
        • Aug 2008
        • 5

        #4
        post the whole nullpointer exception line

        Comment

        • vitaminz
          New Member
          • Aug 2008
          • 11

          #5
          The NullPointer references two methods: this line from the code I posted:
          Code:
          carDetails[activeCars].setRegNo(tempRegNo);
          And the reference in the main method which starts the opens that method.

          If it's any help, this is the method for loading the car details, maybe I've messed up this.

          Code:
                inputCarFile = Text.open ( textFilesPath + "car details.txt" );   
                    
                    activeCars = 0;
                    
                aRegNo = Text.readString ( inputCarFile );
          
                while ( aRegNo.equals ( "****" ) == false )
                {
          
                  aManufacturer = Text.readLine ( inputCarFile );
                  aModel = Text.readLine ( inputCarFile );
                  aYear = Text.readInt ( inputCarFile );
                  aPrice = Text.readInt ( inputCarFile );
          
                  carDetails[activeCars] = new Car( aRegNo, aManufacturer, aModel, aYear, aPrice );
                  System.out.println(carDetails[ activeCars ]);
                     
                  activeCars++;
                  System.out.println("");
          
                  aRegNo = Text.readString ( inputCarFile );
          
                }//end-while
          
                inputCarFile.close();
          Many thanks again :)

          EDIT: If it's any help this is what my car details file looks like (Ignore the complete nonsense cars lol)

          Code:
          C04ABC
          BMW
          X219
          2004
          40000
          
          C04XYZ
          Honda
          Civ
          2004
          10000
          
          L17LLL
          Rover
          MGF1.8
          1998
          4000
          
          V40GOR
          Vauxhall
          Omega 2
          1998
          1000
          
          ****

          Comment

          Working...