简体   繁体   English

字符串数组中的 NullPointerException

[英]NullPointerException In a String Array

When ever I run this method to give out the required data about a customer - it's a flight club program - it returns nothing as if nothing has ever been entered.每当我运行此方法来提供有关客户的所需数据时——它是一个飞行俱乐部程序——它什么也不返回,就好像什么都没有输入一样。

NullPointerException most be the issue, because also when I run another method (will follow, too) to give some data, NullPointerException appears, however only when there has been no data entered: NullPointerException 最有问题,因为当我运行另一个方法(也将遵循)来提供一些数据时,会出现 NullPointerException,但是只有在没有输入数据时才会出现:

First method which gives data about the user:第一种提供用户数据的方法:

public static void ausgabeBuchung(String[] name, String[] surname, int[] time, int[] flightNumber, String[] description, String[] reservation) {
    System.out.println("Alle ihrer Buchungen");
    for (int i = 0; i < name.length-1; i++) {
        System.out.println("the flight number: " + flightNumber[i]);
        System.out.println("the description: " + description[i]);
        System.out.println("time of the reservation: " + time[i]);
        System.out.println("name of the person: " + name[i]);
        System.out.println("Surname of the person: " + surname[i]);
        System.out.println("Reservation type: " + reservation[i] + "\n");
    }
}

Second Method which checks if any flights are available:第二种检查是否有可用航班的方法:

public static void verfügbar( String[] name, String[] description, int[] time, String[] reservation) {
    for (int i = 0; i < description.length; i++) {
        if (vorname[i] == " ") {
            System.out.println("The flight with the description: " + description[i] + " is free the whole day");
        } else if (buchung[i].equals("Ganze Tag")) {
            System.out.println("The flight with the description: " + description[i] + " is reserved the whole day");
        }
        else{
            System.out.println("The Flight with the description " + description[i] + " ist from: " + time[i]++ + " till: " + time[i] + " reserved");
        }
    }
}

It looks like you are using the index of a set of arrays as a sort of suedo-Object.看起来您正在使用一组 arrays 的索引作为一种 suedo-Object。 The problem you are probably running into in the first method is the name array has a different number of values in it than the other arrays.您可能在第一种方法中遇到的问题是名称数组中的值数量与其他 arrays 不同。 If each index of these arrays are all related to only each other then why not just create a Reservation object?如果这些 arrays 的每个索引都彼此相关,那么为什么不创建一个预留 object 呢?

Public class Reservation {
   String Name;
   String surname;
   int time;
   int flightNumber;
   String description;
   String reservation;
}

And then create just a single array of Reservations.然后只创建一个 Reservations 数组。

This will make it much easier to iterate through without worrying about keeping the array sizes the same.这将使迭代更容易,而不必担心保持数组大小相同。

Check if any of these Arrays is not null before use, maybe when you calling the method to send some null array使用前检查这些 Arrays 是否不是 null 之前,也许当你调用方法发送一些 null 数组时

You should check if any argument is null您应该检查是否有任何参数是 null

public static void ausgabeBuchung(String[] name, String[] surname, int[] time, int[] flightNumber, String[] description, String[] reservation) {
    if ( name == null || surename == null || time == null || flightNumber == null || description == null || reservation == null ) {
      throw new IllegalArgumentException("Please enter valid arrays");
    }
}
i < name.length-1

is your issue, as your loop will always omit the last name.是您的问题,因为您的循环将始终省略姓氏。

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

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