0

I'm currently taking an Introduction to Java class where we are currently on Arrays topics. We have a class lab where are suppose to create a simple array program consisting a two classes (Passenger.java and Demo.java). The array can be of any sizing (minimum 4) and we can hard code details into few elements.

So I declared the array in the Demo.java with size of 10, and I initialized/hard code 8 of the elements (leaving 2 elements un-initialized) with one of the constructor in Passenger.java. Then using a for-loop and counters I determine the number of elements is not null, and initialize the remaining element with the counter value increase by 1 (e.g. passengers[index++] = new Passenger()).

However, when I tried to call a method after initializing the newly initialized element I got java.lang.NullPointerException error. So I tested by called exact index which is 8 (passengers[8] = new Passenger()) and then calling the method again, it works.

Hence may I know which part of my code is having problem.

Note:

Passenger Class


import java.util.Scanner;

public class Passenger {

    private String title;
    private String firstName;
    private String lastName;

    public Passenger() {

    }

    public Passenger(String title, String firstName, String lastName) {
        this.title = title;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public void enterDetails() {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter your title: ")
        title = keyboard.next();
        System.out.print("Enter your first name: ")
        firstName = keyboard.next();
        System.out.print("Enter your last name: ")
        lastName = keyboard.next();
    }

}

Demo Class

public class Demo {
    public static void main(String[] args) {
        Passenger[] passengers = new Passenger[10];
        passengers[0] = new Passenger("Mr", "Benjamin", "Parker");
        passengers[1] = new Passenger(....);
        passengers[2] = new Passenger(....);
        passengers[3] = new Passenger(....);
        passengers[4] = new Passenger(....);
        passengers[5] = new Passenger(....);
        passengers[6] = new Passenger(....);
        passengers[7] = new Passenger(....);

        int index = 0;
        for (int i = 0; i < passengers.length; i++) {
            if (passengers[i] != null)
                index++;
        }
        passengers[index++] = new Passenger();
        passengers[index].enterDetails();
    }
}
papert1ger
  • 59
  • 5

3 Answers3

0

After loop index value is 8.

passengers[index++] = new Passenger();

After this line executes Passenger Object saved on 8 index and after that index value is 9 because it is post increment. So actually this thing happen.

passengers[8] = new Passenger();
index= 8+1;

And

passengers[9] is null and you are trying to access passengers[9].enterDetails(). Thats why you are getting Null pointer exception.

Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
0
passengers[index++] = ...
passengers[index].enterDetails()

You allocate passengers[8] but then call enterDetails on passengers[9] because index was incremented.

passengers[index] = ...
passengers[index].enterDetails()
index++;
Cristian Bidea
  • 679
  • 4
  • 12
0

Actually the problem is with index++ which is post increment which means index in incremented after new Passenger(); is allocated to passengers[8] and become 9 and you are getting passengers[index] which is passengers[9] == null

passengers[index++] = new Passenger();
passengers[index].enterDetails()

either you should use passengers[index] so that index will not be incremented

passengers[index] = new Passenger();
passengers[index].enterDetails()

or passengers[++index] so that the index will be incremented before allocation

passengerspassengers[++index] = new Passenger();
passengers[index].enterDetails()
Syed Mehtab Hassan
  • 1,297
  • 1
  • 9
  • 23