473,387 Members | 1,516 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,387 software developers and data experts.

Inserting into an object

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 'NullPointerException' 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 1433
did you define the array?

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

and not just Object[] cars;
Aug 7 '08 #2
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
post the whole nullpointer exception line
Aug 7 '08 #4
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
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...
0
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...
1
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...
6
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...
1
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...
0
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...
0
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...
3
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...
12
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...
2
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...

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.