Deleting from a text file

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

    Deleting from a text file

    Hi it's me again, I've pretty much finished a program I'm making, which does the following:

    The program is for a garage, and it does the following functions:

    Adds a new car
    Display cars
    Amend price of a car
    Delete car ('sell' car)
    Find a certain car

    Almost everything is working perfect, except that I can't get the 'delete'f unction working correctly.

    All the cars data is stored in a text file, like so:

    Code:
    V40GOR
    Vauxhall
    Omega 2
    1998
    1000
    
    R45GER
    BMW
    Ver2
    1995
    1000
    When the program is loaded, the text file data is converted into objects so I can manipulate them. When manipulation is finished, the data is written back to the text file.

    For some reason, my 'delete' functions only works if you delete the last entry in the text file, any other and I get a NullPointerExce ption error...Any ideas? Here is my code for the sellCar method.

    Code:
        public void sellCar() throws IOException {
         if (activeCars != 0) {
         regFound = false;
    
         delRegNo = Text.readString("Please enter the registration number of the car you wish to sell.");
             while(search < activeCars && delFound == false){
                 if(carDetails[search].getRegNo().equalsIgnoreCase(delRegNo)) {
                     delFound = true;
                     regFound = true;
                 }
                 else {
                     search++;
                 }
             }
         
         if (delFound == true) {
         correct = Text.readChar("Here are the details of the car to be deleted: \n" +
         "Manufacturer: " + carDetails[search].getManufacturer() + "\n" +
         "Model: " + carDetails[search].getModel() + "\n" +
         "Registration No: " + carDetails[search].getRegNo() + "\n" +
         "Year: " + carDetails[search].getYear() + "\n" +
         "Price: £" + carDetails[search].getPrice() + "\n" +
         "\n" +
         "Are these details correct? If so, press Y or y");
         
         if (correct == 'Y' || correct == 'y') {
             carDetails[search] = null;
             activeCars--;
             System.out.println("Deleted.");
         
             delFound = true;
         } else {
             delFound = false;
         }
         
         }
    
         }
         
         else {
             Text.showMessage("There are no car details in the database.");
         }
         
         
         correct = '?';
         }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Exactly what line generates the NullPointerExce ption?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      In line 6 you're using an index 'search'; where is it initialized? btw, you set a sold
      car to null but you don't anticipate for null values while you iterate over that array.
      An ArrayList<Car> would've been better.

      kind regards,

      Jos

      Comment

      • vitaminz
        New Member
        • Aug 2008
        • 11

        #4
        Thanks for the replies :)

        The line is actually in a different method, sorry I seem to have got confused! It's in the saveCarDetails method, but other methods also use this to save details, and they have no trouble. The saveCarDetails method is the following:

        Code:
           public void saveCarDetails() throws IOException
           {
               {
                 PrintWriter outputFile;
                 
                 outputFile = Text.create ( textFilesPath + "car details.txt" );
        
               for ( int printcount = 0; printcount < activeCars; printcount++ )
               {
                 
                 outputFile.println ( carDetails[printcount].getRegNo() );
                 outputFile.println ( carDetails[printcount].getManufacturer() );
                 outputFile.println ( carDetails[printcount].getModel() );
                 outputFile.println ( carDetails[printcount].getYear() );
                 outputFile.println ( carDetails[printcount].getPrice() );
                 outputFile.println ("");
               }//end-for
                 outputFile.println ("****");
                outputFile.close();
                
               }
               
           }
        The exact line is line 11.

        The variable is initialized in the public class:

        Code:
        private int search = 0;
        Thanks for the suggestions, I'll be adding validation etc soon, just figured I'll get this working first lol.

        Comment

        • vitaminz
          New Member
          • Aug 2008
          • 11

          #5
          Just saying I'm pretty sure that the problem is this line:
          Code:
          carDetails[search] = null;
          Is that the right method to delete an object?

          Comment

          Working...