繁体   English   中英

我收到 null 指针异常错误,我找不到源

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

我正在创建一个程序,机场工作人员可以输入飞机和航班信息,之后机场用户可以打印出输入的飞机和航班信息。 运行第一个方法startAirportPanel(),并在阅读器扫描器中输入1添加飞机信息后,输入飞机ID和飞机容量后会出现null指针异常错误。

结果:

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


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

在此处输入 22 并按回车后,我会得到 null 指针异常。

这是 object 航班:


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;
}

} 

这是用户界面:


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) + ")");
    }

}

最后,主class启动程序:

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.fly = fly;
this.reader = reader;

阅读器已经被实例化,因此您只需将当前值替换为:当前值。 然而,苍蝇没有被实例化。 因此,您只需将 null 设置为 null。 无论您在飞行中拨打什么电话,都会导致 NPE。

删除this.reader = reader; 并通过实际实例化更改有关 fly 的行。

编辑:

所以,你的构造函数应该是这样的:

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

我还会重新检查您的航班构造函数。

有问题的代码部分是(在 UserInterface class 中):-

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


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

}

只有 fly 变量类型 (Flights) 被声明,没有航班 class object 的任何分配。

使用“新”关键字修复此问题以实例化航班 class:-

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


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

但是,可以按照@Stultuske 的建议以更好的方式重构代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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