aspose file tools*
The moose likes Beginning Java and the fly likes Creating array of objects from user input and saving to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating array of objects from user input and saving to a file" Watch "Creating array of objects from user input and saving to a file" New topic
Author

Creating array of objects from user input and saving to a file

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
I have created a class to create Student objects. Now I am having trouble getting user input to create an array of objects and save to a .txt file and then reading the objects back via toString. I kind of have an idea of how to read back data that is written in a file(see below), but I'm having problems figuring out how to get the user input into the file instead. We can only use Text I/O.

With this code, I'm still getting an error: Exception in thread "main" java.lang.NullPointerException
at TestStudent.main(TestStudent.java:15)





My text file:
Nicolas Young
1234-5678
12/10/1990
Stephanie Elizabeth Jameson
2233-4455
3/15/1992
Taylor Michelle Smith
6767-8989
2/5/1989
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

Kd Martin wrote:I have created a class to create Student objects. Now I am having trouble getting user input to create an array of objects and save to a .txt file and then reading the objects back via toString. I kind of have an idea of how to read back data that is written in a file(see below), but I'm having problems figuring out how to get the user input into the file instead. We can only use Text I/O.

With this code, I'm still getting an error: Exception in thread "main" java.lang.NullPointerException
at TestStudent.main(TestStudent.java:15)



Question. Did you create the Student instances for your Student array?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
What does that mean? Sorry, I'm such a noob.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19063
    
  40

Kd Martin wrote:What does that mean? Sorry, I'm such a noob.


It means that you never created any Student objects. Creating an array for the Student objects, doesn't actually create any Student objects.

Henry
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

declaring an array of objects is like saying "give me an egg carton that can hold 10 eggs". Unless and until you actually get one or more eggs and put them into the array, all you have is an empty carton.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
You have to populate your array. Array elements are initialized to null when it's an array of objects, that's why you are getting NPE when you try to use them.



Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
Ok. I created the objects in the array. Now I'm getting the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Student.setNames(Student.java:23)
at TestStudent.main(TestStudent.java:21)




Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
You should the Array's length property to avoid hard-coding the length. Instead of this


try this


Now the exception you are getting seems to be in here somewhere


Is there a name value that, when split this way, does not result in an array of at least two elements? Perhaps something is being passed in here that contains no spaces at all.
>
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
All of the names I saved to a file have at least a first and last name...
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
Is your data file missing a line? Maybe you are reading in a date and trying to treat it as a name.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating array of objects from user input and saving to a file