• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does that mean? Sorry, I'm such a noob.
 
Henry Wong
author
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12188
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of the names I saved to a file have at least a first and last name...
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your data file missing a line? Maybe you are reading in a date and trying to treat it as a name.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic