• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to put user input into an array and display them  RSS feed

 
Nur Sky
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to have user input of student details into an array addStudent() and being able to display them: viewDetails(). I am new to Java so I really need help.

error Exception in thread "main" java.lang.NullPointerException comes out. Error on "case 2 : index = addStudent(S1, index);" and "S1[index].setFirstName(kb.nextLine());" after entering firstName.



   
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why do you want all your input in an array? You can simply enter things into an array like this:-but I don't think that is a good idea. You can create a Student class, but I wouldn't use setName or anything like that. If you do that, you will have a Student object which is in an inconsistent state: it will have half a name and no ID number. Get the details into it via its constructor.
And why are you getting the null pointer exception? Probably because you forgot how arrays work.An array of reference types defaults to being filled with nulls so you need to get rid of them. You can use an array initialiserOr you can iterate the loop like this:-Why have you got so many things marked static?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!