-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,269
  • 9
  • 39
  • 57
Yottr
  • 43
  • 3
  • Runtime exceptions are distinct concepts from compilation errors. There is a wealth of knowledge onlien about what a NullPointerException is and how to fix it. It is one of the most common runtime exceptions in Java and it's usuaully very easy to fix. Please read up on it. – Kon Apr 08 '15 at 20:23
  • Would you mind posting the error stack trace so we can help you further? – theguywhodreams Apr 08 '15 at 20:23
  • please give us an indication of where in your code the exception actually occurs, as searching through the entire code for it would be a pain. – Davis Broda Apr 08 '15 at 20:23
  • 3
    dont see `passengerList` initialized anywhere – Reimeus Apr 08 '15 at 20:24
  • Exception in thread "main" java.lang.NullPointerException – Yottr Apr 08 '15 at 20:28
  • at Flight.addPassenger – Yottr Apr 08 '15 at 20:28
  • at FlightTester.main – Yottr Apr 08 '15 at 20:28
  • @Yottr Stop putting the location of the error in a ton of comments. Edit this information into the question instead, and delete the comments that specify the location. It is more clear if it is there. – Davis Broda Apr 08 '15 at 20:29

3 Answers3

0

private Vector passengerList;

is not initialized, it could be the issue.

anji_rajesh
  • 369
  • 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,466
  • 2
  • 16
  • 32
0
private Vector passengerList;

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

AndrewGrant
  • 786
  • 7
  • 17