My code :
static List<Object> data;
private void addItem(List<Object> list) {
try {
data = new ArrayList<Object>();
list.add("test");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ListTest test = new ListTest();
test.addItem(data);
}
Above code throws NullPointerException
. The code below does not throw NPE.
static List<Object> data = new Vector<Object>();
private void addItem(List<Object> list) {
try {
list.add("test");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ListTest test = new ListTest();
test.addItem(data);
}
Above code does not throw NullPointerException
. I don't understand the difference between both.