473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. V40GOR
  2. Vauxhall
  3. Omega 2
  4. 1998
  5. 1000
  6.  
  7. R45GER
  8. BMW
  9. Ver2
  10. 1995
  11. 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 NullPointerException error...Any ideas? Here is my code for the sellCar method.

Expand|Select|Wrap|Line Numbers
  1.     public void sellCar() throws IOException {
  2.      if (activeCars != 0) {
  3.      regFound = false;
  4.  
  5.      delRegNo = Text.readString("Please enter the registration number of the car you wish to sell.");
  6.          while(search < activeCars && delFound == false){
  7.              if(carDetails[search].getRegNo().equalsIgnoreCase(delRegNo)) {
  8.                  delFound = true;
  9.                  regFound = true;
  10.              }
  11.              else {
  12.                  search++;
  13.              }
  14.          }
  15.  
  16.      if (delFound == true) {
  17.      correct = Text.readChar("Here are the details of the car to be deleted: \n" +
  18.      "Manufacturer: " + carDetails[search].getManufacturer() + "\n" +
  19.      "Model: " + carDetails[search].getModel() + "\n" +
  20.      "Registration No: " + carDetails[search].getRegNo() + "\n" +
  21.      "Year: " + carDetails[search].getYear() + "\n" +
  22.      "Price: £" + carDetails[search].getPrice() + "\n" +
  23.      "\n" +
  24.      "Are these details correct? If so, press Y or y");
  25.  
  26.      if (correct == 'Y' || correct == 'y') {
  27.          carDetails[search] = null;
  28.          activeCars--;
  29.          System.out.println("Deleted.");
  30.  
  31.          delFound = true;
  32.      } else {
  33.          delFound = false;
  34.      }
  35.  
  36.      }
  37.  
  38.      }
  39.  
  40.      else {
  41.          Text.showMessage("There are no car details in the database.");
  42.      }
  43.  
  44.  
  45.      correct = '?';
  46.      }
Aug 7 '08 #1
4 2185
BigDaddyLH
1,216 Expert 1GB
Exactly what line generates the NullPointerException?
Aug 7 '08 #2
JosAH
11,448 Expert 8TB
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
Aug 7 '08 #3
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:

Expand|Select|Wrap|Line Numbers
  1.    public void saveCarDetails() throws IOException
  2.    {
  3.        {
  4.          PrintWriter outputFile;
  5.  
  6.          outputFile = Text.create ( textFilesPath + "car details.txt" );
  7.  
  8.        for ( int printcount = 0; printcount < activeCars; printcount++ )
  9.        {
  10.  
  11.          outputFile.println ( carDetails[printcount].getRegNo() );
  12.          outputFile.println ( carDetails[printcount].getManufacturer() );
  13.          outputFile.println ( carDetails[printcount].getModel() );
  14.          outputFile.println ( carDetails[printcount].getYear() );
  15.          outputFile.println ( carDetails[printcount].getPrice() );
  16.          outputFile.println ("");
  17.        }//end-for
  18.          outputFile.println ("****");
  19.         outputFile.close();
  20.  
  21.        }
  22.  
  23.    }
The exact line is line 11.

The variable is initialized in the public class:

Expand|Select|Wrap|Line Numbers
  1. private int search = 0;
Thanks for the suggestions, I'll be adding validation etc soon, just figured I'll get this working first lol.
Aug 7 '08 #4
Just saying I'm pretty sure that the problem is this line:
Expand|Select|Wrap|Line Numbers
  1. carDetails[search] = null;
Is that the right method to delete an object?
Aug 7 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Thaynann | last post by:
is there a way to delete a block of text i have started by looking for the speicific line of text that starts the block by looking for teh index of it...but i cannot figure out out to remove...
14
by: Alec S. | last post by:
Hi, I'm using JavaScript and Cookies for some customization in a web page. There may be several values in the cookie with names that are not known at runtime. I need a way of deleting them. ...
2
by: GMK | last post by:
Dear all in my asp.net application i have a text file that is installed with my application on the server. this text file is filled with data through a web interface in my application. i need to...
5
by: George | last post by:
VB.net 2003 standard, XP windows home edition. Installed first application OK today. When I removed the application via Control Panel, there were no problems and the app folders were deleted. ...
2
by: angus | last post by:
hello everybody, i'm having some problems searching a text file and then deleting the line. since i used fileopen(), i can use the eof() function to loop through the file, searching for the...
2
by: SiouxieQ | last post by:
Hi there, I'm using the code below to try to delete a name from a list of names in a file. Unfortunately it doesn't quite do what I want it to. Instead of looking for the name in the...
13
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or...
3
by: showson1 | last post by:
Hi all. We have some files that are basically TIF images with a text header. The header portion is blocked by {startdb} and {enddb}.. it is the same format for all the files. What I need to do...
4
by: MiziaQ | last post by:
Hey, I'm using the following code to write entries to a data file and then read them in an msflexgrid. I now would like to add code under a delete button to use the table(grid) to delete rows from...
0
by: aadsaca | last post by:
Deleting/Removing lines on Text File in VB -------------------------------------------------------------------------------- Hi there, i just want to know the syntax on how to remove line on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.