Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Exception in thread "main" java.lang.NullPointerException

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
Eclipse IDE C++ Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 75698
354
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75698
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree, Jeff, you are right there.
 
Beware the other head of science - it bites! Nibble on this message:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic