473,698 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting into an object

11 New Member
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:

Expand|Select|Wrap|Line Numbers
  1.             tempRegNo = Text.readString("Please enter the new cars registration number.");
  2.                 tempManufacturer = Text.readString("Please enter the manufacturer");
  3.                 tempModel = Text.readString("Please enter the model");
  4.                 tempYear = Text.readInt("Please enter the year");
  5.                 tempPrice = Text.readInt("Please enter the price");
  6.  
  7.                 carDetails[activeCars].setRegNo(tempRegNo);
  8.                 carDetails[activeCars].setManufacturer(tempManufacturer);
  9.                 carDetails[activeCars].setModel(tempModel);
  10.                 carDetails[activeCars].setYear(tempYear);
  11.                 carDetails[activeCars].setPrice(tempPrice);
  12.                 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
Aug 7 '08 #1
4 1441
Toothpick
5 New Member
did you define the array?

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

and not just Object[] cars;
Aug 7 '08 #2
vitaminz
11 New Member
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 :-?
Aug 7 '08 #3
Toothpick
5 New Member
post the whole nullpointer exception line
Aug 7 '08 #4
vitaminz
11 New Member
The NullPointer references two methods: this line from the code I posted:
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1.       inputCarFile = Text.open ( textFilesPath + "car details.txt" );   
  2.  
  3.           activeCars = 0;
  4.  
  5.       aRegNo = Text.readString ( inputCarFile );
  6.  
  7.       while ( aRegNo.equals ( "****" ) == false )
  8.       {
  9.  
  10.         aManufacturer = Text.readLine ( inputCarFile );
  11.         aModel = Text.readLine ( inputCarFile );
  12.         aYear = Text.readInt ( inputCarFile );
  13.         aPrice = Text.readInt ( inputCarFile );
  14.  
  15.         carDetails[activeCars] = new Car( aRegNo, aManufacturer, aModel, aYear, aPrice );
  16.         System.out.println(carDetails[ activeCars ]);
  17.  
  18.         activeCars++;
  19.         System.out.println("");
  20.  
  21.         aRegNo = Text.readString ( inputCarFile );
  22.  
  23.       }//end-while
  24.  
  25.       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)

Expand|Select|Wrap|Line Numbers
  1. C04ABC
  2. BMW
  3. X219
  4. 2004
  5. 40000
  6.  
  7. C04XYZ
  8. Honda
  9. Civ
  10. 2004
  11. 10000
  12.  
  13. L17LLL
  14. Rover
  15. MGF1.8
  16. 1998
  17. 4000
  18.  
  19. V40GOR
  20. Vauxhall
  21. Omega 2
  22. 1998
  23. 1000
  24.  
  25. ****
  26.  
Aug 7 '08 #5

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

Similar topics

3
2166
by: sms1 | last post by:
My Participants table in Access 2002 contains about 1500 records. The Notes field is OLE. I want to use Insert Object to insert MS Word into the Notes field. It is easily done using: Insert Object, etc.; no problem for inserting one at a time. Problem: I don't want to go through this same Insert Object procedure 1500 separate times. There MUST be a way to do a "group" Insert Object in the Notes field so all 1500 are completed at once...
0
2344
by: Lyn | last post by:
I am having a frustrating time trying to do a simple insert of a BMP picture into a Bound Object Frame using both VBA and the Insert | Object... dialog. I had a separate post thread going on this and closed it off when I thought I had resolved the problem, but now I am back at square one. So I thought I would start afresh with a new post. Here is the problem... A few months ago, I set up a Bound Object Frame on a form which was tied to...
1
615
by: ajk | last post by:
. Hi, All: I know how to insert files into a Word doc using C#. However, the program I've written to do this runs much too slowly. The "myObj".Application.Selection.InsertFile method executes at a snails pace. Here are the detais: I wrote a C# program that creates a new Word doc and then loops through a
6
2053
by: Pushpendra Vats | last post by:
Hi , I am trying to insert records into database. I am trying to write the following code. On button1 click event i am inserting five records and after that i am calling the update method of dataadapter to update the records in database. Records are inserting but all the five records have the same value and that too of that last ones.. More over i tried to use the acceptchanges methods for datatable and dataset but of no use
1
2585
by: yuchang | last post by:
Hi, Using the FormView control is very efficient. But I want to do some action,like showing a success message or redirect to another page, after inserting, updating and deleting. How do I get these message?
0
3001
by: pd123 | last post by:
I'm new to C# and .net and I'm trying to create a form that will register users in a sql server database. I have the following code but when I run the code I get an error " The name 'Peter' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted. Where Peter is the value entered in the textbox for firstname (fnameTBox) I'm sure the problem is something obvious but I...
0
1632
by: tom c | last post by:
I am going through "Walkthrough: Editing and Inserting Data in Web Pages with the DetailsView Web Server Control" found at http://msdn2.microsoft.com/en-us/library/sdba1d59.aspx I am using Access instead of SQL Server. Everything works fine until I get to the last step, where I try to insert a record. When I hit the New link it lets me add the new data, but then when I hit Insert, I get the yellow page which I have paseted below. I...
3
4270
by: rcoco | last post by:
Hi, I want to share this problem. I have a datagrid that will help me Insert data into sql database. So I made a button On my form so that when I press the button a new row on datagrid should be created and I could be able to insert data. But with this code below I've failed could someone help me and tell me where I'm going wrong: private void Page_Load(object sender, System.EventArgs e) { if (! IsPostBack)
12
4909
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back instead I don't need to supply the iterator. But what
2
3082
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography section: Discography --------------------- DiscID
0
8678
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
9166
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
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.