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
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().
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.
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.
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.
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.