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

问题描述 投票:1回答:2

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

结果:

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


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

在这里输入22后按Enter,我将得到空指针异常。

这里是对象航班:


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

}

最后,主类启动程序:

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();
        }
}
        ```



java oop object nullpointerexception null
2个回答
4
投票
this.fly = fly; this.reader = reader;

阅读器已被实例化,因此您只需将当前值替换为:当前值。但是,苍蝇没有被实例化。因此,您只是将null设置为null。无论您在通话中拨打什么电话,都会产生NPE。

删除this.reader = reader;并使用实际实例更改关于fly的界线。

编辑:

因此,您的构造函数应类似于:

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

我还将重新检查您的Flights构造函数。


0
投票
private Flights fly; private Scanner reader = new Scanner(System.in); public UserInterface(){ this.fly = fly; this.reader = reader;

}

仅声明飞行变量类型(Flights),而没有对Flightes类对象进行任何分配。

使用'new'关键字实例化Flightes类来解决此问题:-

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

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

© www.soinside.com 2019 - 2024. All rights reserved.