I get a NullPointerException when I run this. It occurs on the line listings[i].input();
Am I declaring this right? I want an array of 3 listing objects, input to them with my input() method, then output in reverse order.
public static void main(String[] args) {
Listing[] listings = new Listing[3];
for (int i = 0; i < listings.length; i++) {
listings[i].input();
}
for (int i = listings.length - 1; i >= 0; i--) {
System.out.println(listings[i]);
}
}
You didn’t construct the individual
Listingobjects. Solistings[0] == null. This is true for all elements of the array – they’re all initialized tonullby default.You must first say
listings[i] = new Listing()or the like, before yourlistings[i].input().