I am trying to add null
to the ArrayList
where as it is integer type. But i am getting NullPointerException
.
Adding null
to the ArrayList
is a success, but while printing it exception is being raised.
Please let me know how to resolve this.
import java.util.Enumeration;
public enum Car {
lamborghini(123),tata(2),audi(50),fiat(15),honda(12);
private int price;
Car(int p) {
price = p;
}
int getPrice() {
return price;
}
}
import java.util.ArrayList;
public class Main {
public static void main(String args[]){
System.out.println("All car prices:");
for (Car c : Car.values())
System.out.println(c + " costs "
+ c.getPrice() + " thousand dollars.");
ArrayList<Integer> al = new ArrayList();
al.add(null);
for (Car p : Car.values())
al.add(p.getPrice());
System.out.println("Size: "+al.size());
// Loop through elements.
for (int i = 0; i < al.size(); i++) {
**int value = al.get(i);**
System.out.println("Element: " + value);
}
// switch (c)
}
}