-1

I made this program, it deals with classes and relationships, when I run this, it gives me the error "Exception in thread "main" java.lang.NullPointerException", my IDE doesn't detect any error in the coding, can anyone help me out, this is very annoying.

import java.util.Vector;
class Flight {
    private String id;
    private String destination;
    private Time depart;
    private Time arrival;
    private Vector passengerList;
    public Flight(String a, String b, Time c, Time d) {
        id = a;
        destination = b;
        depart = c;
        arrival = d;
    }
    public void addPassenger(Passenger a) {
        passengerList.add(a);
    }
    public void printInfo() {
        System.out.println("Id " + id);
        System.out.println("Destination " + destination);
        System.out.println("Depart " + depart.getHour() + " " + depart.getMinute());
        System.out.println("Arrival " + arrival.getHour() + " " + arrival.getMinute());
        System.out.println("Number of passengers " + passengerList.size());
    }
}
class Time {
    private int hour;
    private int minute;
    public Time(int a, int b) {
        hour = a;
        minute = b;
    }
    public int getHour() {
        return hour;
    }
    public int getMinute() {
        return minute;
    }
}
class Passenger {
    private String name;
    private int age;
    public Passenger(String a, int b) {
        name = a;
        age = b;
    }
}
class FlightTester {
    public static void main(String[] args) {
        Time dep = new Time(8, 10);
        Time arr = new Time(9, 00);
        Flight f = new Flight("PK-303", "Lahore", dep, arr);
        Passenger psg1 = new Passenger("Umair", 30);
        Passenger psg2 = new Passenger("Manzoor", 44);
        f.addPassenger(psg1);
        f.addPassenger(psg2);
        f.printInfo();
    }
}
Alex K
  • 8,020
  • 9
  • 37
  • 55
Yottr
  • 43
  • 3

3 Answers3

0

private Vector passengerList;

is not initialized, it could be the issue.

anji_rajesh
  • 359
  • 2
  • 11
0
    Vector passengerList are not initialized.

when you call

    f.addPassenger(psg2);

you will get NullPointException.

use List would be better than vector

user3644708
  • 2,376
  • 2
  • 15
  • 31
0
private Vector passengerList;

You never initialize this, so when your code hits anything referencing passengerList you get a null pointer exception.

AndrewGrant
  • 756
  • 7
  • 17