• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" java.lang.NullPointerException  RSS feed

 
Christian Mucyo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the parent class




This is one Subclass



And I have two other Subclass that I wont because they are basically the same as this one above. here is the class that contains Main():



I have not finish the coding yet, but at this point I am getting the same error over and over, can't seem to fix it:


Exception in thread "main" java.lang.NullPointerException
at CollegeList.main(CollegeList.java:41)



It is the first time I see this error and I don't understand it, can anyone please help me.
 
Paulo DeLimma
Greenhorn
Posts: 9
C++ Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is attempting to call methods on null references. In Java, we work with arrays of references to objects, not arrays of objects. So before any method call, the reference must be pointing to some object. If you call the constructor for each one of the elements you want to insert before calling setFirstName(), it will work fine. For example: emp[x] = new CollegeEmployee(), before emp[x].setFirstName().
 
Christian Mucyo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate your comment, it was very helpful.

But what about the display() method ? I am getting the same error each time I try to call it.
I called it like this:

emp[x].display();
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same principle as the previous answer gave you applies to each element of an array. If you execute "Object o[] = new Object()[4];", you only allocate space for 4 objects in an array. You have not yet allocated the objects IN the array, only the space to hold references TO those objects. So you would need to, at some point somehow, either execute something like "o[0] = new Object();", or "Object singleObject = new Object(); o[0] = singleObject;"

When you attempt emp[4].setFirstName();, if you haven't executed "new Employee()" in some fashion and assigned it to that element of the array, then that element holds null, and calling setFirstName() on it results in a null pointer exception.
 
Christian Mucyo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I worked on it this morning and I got what you said.
Also one Last question, This program has to display at the end a report off all data entered for each type of person(CollegeEmployee, Faculty and Student)

And if there were no data entered for one type of person I have to display a message,
how can I search in the array if there is data or not?? I have never done that before and I have no clue.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You would search for existence of data the same way you can check anything else. For exampleHave a look in the API documentation and find what the println() method does, and how it copes with nulls and similar.

I think one object-oriented way to do this is to put such information into the toString() method in each class.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

You would search for existence of data the same way you can check anything else. For example


Of course, one has to wonder why there's a null in that list in the first place. Is it really valid for that to be the case? It seems unlikely. And if there's no valid reason for there to be a null there, then the presence of one is an error, and we should not be coding around it--we should let it throw the exception, and if it does, then fix the buggy code that put the null there in the first place.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree, Jeff, you are right there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!