[英]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.