Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 12871
62
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.
 
I've never won anything before. Not even a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic