473,809 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting from a text file

11 New Member
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 NullPointerExce ption 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 2217
BigDaddyLH
1,216 Recognized Expert Top Contributor
Exactly what line generates the NullPointerExce ption?
Aug 7 '08 #2
JosAH
11,448 Recognized Expert MVP
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
vitaminz
11 New Member
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
vitaminz
11 New Member
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
1426
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 that text from the file..can anyone help me?
14
3287
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. For example, if my site is www.test.com and the cookie contains: label1 fob
2
1552
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 know how could i update this text file programatically and using a web interface. what i need is not to over write the content of the text file but to delete it's content and then write a new content. id there any way to do this?
5
2697
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. But when I installed the app from a CD-ROM (Release folder is in D:), when I remove the app in Control Panel these problems occur: Control Panel does not delete the application folders. When I try to delete them I get message "Cannot delete file:...
2
2393
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 specified line. the problem is, once found, i can't delete it because the filemode is in openmode.input. is there a way to chance the openmode on the run?(if so, what is it?) and maybe there is a simpler way to search a text file and deleting the...
2
1716
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 textbox and deleting that it is just deleting the first line of data in the file.
13
2467
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 logic would help me accomplish this?
3
2150
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 is read the file in, delete that block (including the startdb and enddb) and write out the result as a TIF. I can manually do this by loading them in a HEX editor, deleting the block and saving it, opens fine... I'm just not sure how to do it...
4
1481
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 the file. How can I do that. I have some code for deleting a row from the table, but it's only deleting the row from the table, and not the entry itself. Please help! Is there a relatively easy way to do this ? Private Sub cmdAddDelivery_Click() ...
0
1528
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 text file using vb... pls help me.
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.