Results 1 to 8 of 8
  1. #1
    vitaminz is offline Member
    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0

    Default Deleting from an object

    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' function working correctly.

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

    C04ABC
    BMW
    X219
    2004
    40000

    C04XYZ
    Honda
    Civ
    2004
    10000

    L17LLL
    Rover
    MGF1.8
    1998
    4000

    V40GOR
    Vauxhall
    Omega 2
    1998
    1000

    R45GER
    BMW
    Ver2
    1995
    1000

    B34FJF
    Honda
    Civic
    1992
    500

    ****
    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 NullPointerException error...Any ideas? Here is my code for the sellCar method.

    Java Code:
        public void sellCar() throws IOException {
        if (activeCars != 0) {
    
        tempRegNo = Text.readString("Please enter the registration number of the car you wish to sell.");
            while(search < activeCars && found == false){
                if(carDetails[search].getRegNo().equalsIgnoreCase(tempRegNo)) {
                    found = true;
                }
                else {
                    search++;
                }
            }
        
        if (found == 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.");
        
        } else {
            found = false;
        }
        
        }
    
        }
        
        else {
            Text.showMessage("There are no car details in the database.");
        }
        
        
        correct = '?';
        tempRegNo = null;
        tempManufacturer = null;
        tempModel = null;
        tempPrice = 0;
        tempYear = 0;
        }
    And here is the method that saves the objects back to the text file, the line the NullPointerException points to the getRegNo(); line.

    Java 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();
            
           }
           
       }
    Many thanks :)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Looks like there is no element: carDetails[printcount]. ie that element in the array is null.
    Are you sure activeCars has the correct value?
    Does the carDetails array have any empty spots? If it does, you need to test for them.

    Add println() statements to your code to show the values as you fill the array, etc

  3. #3
    vitaminz is offline Member
    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the reply, I seem to got a bit further now - I realise I need to move back cars in positions after the deleted car, so it fills the gap. However I'm stumped on how to do this..I currently have this code:
    Java Code:
    carDetails[search] = null;
                    for (int move = search+1; move <= MAX_CARS; move++ )
                    {
                    carDetails[search] = carDetails[search-1];
                    }
                    activeCars--;
                    Text.showMessage("The car has now been sold.");
    That does delete the position, however all that seems to do is duplicate the previous entry over the deleted one.

    The 'search' position in line 1 is the position of the file to be deleted, and is found using a search before that.

    Max cars is a final variable I defined previously.

    And activeCars is the number of cars in the database.

    Any ideas?:)

    Thanks :)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    There are better containers to hold data when you need to add/delete elements from it. Arrays are sort of static and require a lot of programing if you are going to delete elements or add elements past the array's capacity. Look at using another container type such as ArrayList or some of the other classes in the Collection framework (a fancy word for a bunch of classes that are containers and can hold other objects in different manners and relationships).
    Otherwise, to be able to "delete" an entry from your array make a special class(that extends the normal class) to put in your array that says its a deleted entry and then test for it in your processing loop and skip it.

  5. #5
    vitaminz is offline Member
    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0

    Default

    Thanks for the suggestion, yeah after researching ArrayList it does seem like a great solution, a lot more versatile than objects...However this is for a piece of coursework, and we've been told we must use Java objects to accomplish this - Which is rather annoying!

    Thanks anyway :)

  6. #6
    vitaminz is offline Member
    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0

    Default

    Just editted this out as I've realised something...

    As soon as the car is deleted, it's parsed into a text file, where blank lines are completely ignored...

    So I think what I need to do is set every field to just blank of the position I'm deleting to blank - i.e. a space. However 2 of my fields are integers, and I can't set them to blank :( is there a way to set an integer to just, blank space somehow? Thanks
    Last edited by vitaminz; 08-10-2008 at 03:40 AM.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    we must use Java objects
    Not sure what you mean by that. Java has primitives like int, boolean, double, byte ... everything else is an object.

    Add a flag to the objects that are in the array that says the element is deleted.
    Unless, of course, if the assignment is to write code to move the contents of the array around. The way I've used to design this kind of code is to use a paper and pencil, draw a list, set pointers and work out how to move contents and pointers to get the desired result.

  8. #8
    vitaminz is offline Member
    Join Date
    Aug 2008
    Posts
    5
    Rep Power
    0

    Default

    Ah sorry I get a tad confused with the terminology. What I mean is, the array is created like this:
    Java Code:
    private Car [] carDetails = new Car[MAX_CARS];
    and a new car is made by using something like this:

    Java Code:
    carDetails[activeCars] = new Car( tempRegNo, tempManufacturer, tempModel, tempYear, tempPrice );
    The Car object is referenced in a different class, Car.java

    Java Code:
    public class Car 
    {  
      private String manufacturer;
      private String model;
      private String regNo;
      private int    year;
      private int    price;
      
      public Car(String regNo, String manufacturer, String model, int year, int price)
      {
        this.regNo = regNo;
        this.manufacturer = manufacturer;
        this.model = model;
        this.year = year;
        this.price = price;
      }
    Hope I'm not too confusing here lol, thanks

Similar Threads

  1. Deleting Contractions
    By theonly in forum New To Java
    Replies: 3
    Last Post: 04-27-2008, 07:44 PM
  2. Deleting All rows in the JTable
    By surot in forum New To Java
    Replies: 1
    Last Post: 04-16-2008, 10:44 AM
  3. Deleting a File that is opened
    By ravian in forum Advanced Java
    Replies: 6
    Last Post: 01-30-2008, 02:05 PM
  4. Deleting an empty directory
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:17 AM
  5. deleting elements
    By nalinda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 01:42 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
  •