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 Item
s 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 Item
s 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
.