-1

Say that I create a class Item, with two member variables: name and score; and two methods: getName() and getScore(). Then I create another class List, which has one member variable: table (which is array of Item), and three methods: printList(), addItem() and Instantiation() (which is used to solve the problem below). Here the problem comes: In printItem(), I want to print all the Items in List if the Item is not null. I'm using:

if(list.table[i].getName().equals(null))

It does not looks good even though I already have instantiated all the Item's name and score to null before I add Items to the list. It looks like whenever there is a element which is not a "blank" element, which means the name and score are not null, the conditional above will not work, and instead I get a java.lang.NullPointerException.

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Do you want to check if the item is `null` or if the name of the item is `null`? – Jesper Nov 29 '14 at 21:39
  • NOTHING equals null. – Yoda Nov 29 '14 at 22:01
  • 1
    @Yoda: In Java, `null == null` is `true`. Or you couldn't have null checks. – Makoto Nov 29 '14 at 22:14
  • @Makoto I deliberately used word "equals". As stated here: http://stackoverflow.com/a/1692882/1123020 identity: a variable holds the same instance as another variable. equality: two distinct objects can be used interchangeably. they often have the same id. – Yoda Nov 29 '14 at 22:54

2 Answers2

2

Make without equals(). Just == like so:

if(list.table[i].getName() == null)

You cannot make any operations on null objects. This is the reason, why so often in code before making anything we see:

if (object != null) { 
object.doSomething(); 
} 
Mateusz Pryczkowski
  • 1,874
  • 1
  • 16
  • 20
  • I check the code again and run it step by step. I find a interesting thing: that for the element in list: list.table[i], even though I have already instantiated them (by setting every parameters of Item null), if I do not operate or change the values of parameters of Item, the Item still do not exist. I believe this is the reason why if(list.table[i].getName() == null) is failed: the object does not even exist! I can not understand: since I have already instantiate the Items in table. – Haolin Zhang Nov 30 '14 at 00:39
0

If the ith entry in the table is null then you can't call getName on it (or you'll get a NullPointerException, since you would be trying to call a method on a null reference (meaning there is no object there)). Be aware equals is an instance method so you don't want to call it on something that might be null. Something like:

Item item = table[i];
if (item != null) {
    System.out.println(item.getName());
}

will work.

If item is not null but getName returns null you won't get an NPE here, it will print "null" to stdout. (Of course you can check whether item.getName() == null if you want to avoid printing a null name.)

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • I think you are right. I check the code and run step by step, found that the objects even do not exist (do not show on the console), so use the method of getName() will cause error. – Haolin Zhang Nov 30 '14 at 00:45
  • the first thing is to check whether the object exist or not, if exist, then check the name is null or not. – Haolin Zhang Nov 30 '14 at 01:07