简体   繁体   中英

I'm getting a null pointer exception error and I can't find the source

I'm creating a program where an airport worker can input plane and flight information, after which airport users can print out the plane and flight information that was input. After running the first method startAirportPanel(), and typing 1 into the reader scanner to add plane information, I would get a null pointer exception error after the plane ID and plane capacity are entered.

outcome:

Airport panel
---------------


Choose operation: 
[1] Add airplane
[2] Add flight
[x] Exit
1
Give plane ID: 
GHUA-HG
Give plane capacity: 
22

After typing in 22 here and pressing enter, I would get the null pointer exception.

Here is the object flights:


import java.util.ArrayList;

public class Flights {

    public HashMap<String,Integer> plane = new HashMap<String,Integer>();
    public HashMap<String,String> flight = new HashMap<String,String>();

    public Flights(){

        this.plane = plane;
        this.flight = flight;

    }
    public void planeMap(String id, Integer capacity){
        plane.put(id, capacity);
    }

    public void flightMap(String id, String departure, String destination){
        String flight1 = departure + "-" + destination;
        flight.put(id, flight1);
    }

    public ArrayList planeList(){
        ArrayList<String> keylist = new ArrayList<String>(plane.keySet());
        ArrayList<Integer> valuelist =  new ArrayList<Integer>(plane.values());
        ArrayList<String> newlist = new ArrayList<String>();

        for(int i = 0 ; i < keylist.size() ; i++){
            newlist.add(keylist.get(i) + " (" + valuelist.get(i) + ")");
        }

        for(int i = 0 ; i < newlist.size() ; i++){
            System.out.println(newlist.get(i));
        }
        return newlist;
    }

    public ArrayList flightList(){
        ArrayList<String> keylist = new ArrayList<String>(flight.keySet());
        ArrayList<String> valuelist =  new ArrayList<String>(flight.values());
        ArrayList<String> newlist = new ArrayList<String>();

        for(int i = 0; i < keylist.size(); i++){
            newlist.add(keylist.get(i) + " (" + plane.containsKey(keylist.get(i)) + ") " + "(" + valuelist.get(i) + ")");

        }
        for(int i = 0; i < newlist.size() ; i++){
            System.out.println(newlist.get(i));
        }
        return newlist;
    }

    public int planeInfo(String id){

        if(plane.containsKey(id)){
        return plane.get(id);
    }
        return 0;
}

} 

And here is the userinterface:


import java.util.HashMap;
import java.util.Scanner;
import java.util.ArrayList;

public class UserInterface {
    private Flights fly;
    private Scanner reader = new Scanner(System.in);


    public UserInterface(){
        this.fly = fly;
        this.reader = reader;




    }

    public void startFlightService(){



        System.out.println("Flight service ");
            System.out.println("--------------");
            System.out.println();
            System.out.println();

         while(true){

            System.out.println("Choose operation: ");
            System.out.println("[1] Print planes");
            System.out.println("[2] Print flights");
            System.out.println("[3] Print plane info");
            System.out.println("[x] Print Quit");

            if(reader.equals("x")){
                break;
            }
            if(Integer.parseInt(reader.nextLine()) == 1){
                printPlane();
            }
            if(Integer.parseInt(reader.nextLine()) == 2){
                printFlight();
            }
            if(Integer.parseInt(reader.nextLine()) == 3){
                printPlaneInfo();
            }
                else continue;

        }

    }

    public void startAirplanePanel(){


        System.out.println("Airport panel");
        System.out.println("---------------");
        System.out.println();
        System.out.println();



        while(true){
        System.out.println("Choose operation: ");
        System.out.println("[1] Add airplane");
        System.out.println("[2] Add flight");
        System.out.println("[x] Exit");

        String input = reader.nextLine();


        if(Integer.parseInt(input) == 1){
            addPlane();


        } if(Integer.parseInt(input) == 2){
            addFlight();

        }
        if(input.equals("x")){
            break;
        }
    }

}

    public void addPlane(){
        System.out.println("Give plane ID: ");
        String id = reader.nextLine();
        System.out.println("Give plane capacity: ");
        int capacity = Integer.parseInt(reader.nextLine());

        fly.planeMap(id,capacity);


    }

    public void addFlight(){
        System.out.println("Give plane ID: ");
        String id = reader.nextLine();
        System.out.println("Give departure airport code: ");
        String departure = reader.nextLine();
        System.out.println("Give destination airport code: ");
        String destination = reader.nextLine();



        fly.flightMap(id,departure,destination);
    }

    public void printPlane(){

        for(int i = 0; i < fly.planeList().size(); i++){
            System.out.println(fly.planeList());
        }






    }

    public void printFlight(){
        for(int i = 0; i < fly.flightList().size(); i++){
            System.out.println(fly.flightList());
        }

    }

    public void printPlaneInfo(){
        System.out.print("Give plane ID: ");
        String id = reader.nextLine();

        System.out.println(id + " (" + fly.planeInfo(id) + ")");
    }

}

Lastly, the main class to start the program:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Write your main program here. Implementing your own classes will be useful.

        Scanner reader = new Scanner(System.in);

        Flights flightss = new Flights();
        UserInterface hello = new UserInterface();

        hello.startAirplanePanel();
        }
}
        ```



This part of your constructor makes no sense:

this.fly = fly;
this.reader = reader;

the reader already is instantiated, so you just replace the current value with: the current value. The fly, however, isn't instantiated. So, you are just setting null to null. Whatever call you make on fly, will cause an NPE.

Remove the this.reader = reader; and change the line about fly with an actual instantiation.

EDIT:

So, your constructor should be something like:

public UserInterface(){
        this.fly = new Flights();
  }

I would also re-check your Flights constructor.

The part of the code that is problematic is ( in the UserInterface class ):-

private Flights fly;
private Scanner reader = new Scanner(System.in);


public UserInterface(){
    this.fly = fly;
    this.reader = reader;

}

Only fly variable type (Flights) is declared without any assignment of the Flights class object.

Fix this using the 'new' keyword for instantiation of the Flights class:-

private Flights fly = new Flights();
private Scanner reader = new Scanner(System.in);


public UserInterface(){
    this.fly = fly;
    this.reader = reader;
}

However code can be refactored in a better way as suggested by @Stultuske.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM