-4

您好,我正在尝试在 java 中创建一个记录数组,您必须在其中输入 3 个详细信息,即城镇名称、人口和所在的县。在此之前输出您要求的县的所有数据。我想知道是否有人可以告诉我为什么如果我进入一个城镇的人口时会发生 null.point.exception 而当我进入另一个城镇时不会发生。

import java.util.*;
public class CathedralTowns
{
public static String name;

String population;
String county;
public static int count = 0;
public static int continuation = 0;
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
     int loop1 = 0;

    while (loop1 <= 0) {
        System.out.println("Please enter the name of the town. ('no' to end)");
        String nameEntered = input.nextLine();
        System.out.println("Please enter the county in which the town resides. ('no' to end)");
        String countyEntered = input.nextLine();
        System.out.println("Please enter the population of the town. ('no' to end)");
        String populationEntered = input.nextLine();

        if (nameEntered.equals("no") || populationEntered.equals("no") || countyEntered.equals("no") ) {
            loop1 = 5; 
            System.out.println("Thank you for entering your county.");
            continuation = 1;
        }
        WorkingDemCathedrals(nameEntered, populationEntered, countyEntered); 
    }   

}

public static void WorkingDemCathedrals(String nameEntered, String populationEntered, String countyEntered) {
    Scanner input = new Scanner(System.in);
    CathedralTowns[] allTowns = new CathedralTowns[50];
    allTowns[count] = new CathedralTowns();
    int loop2 = 0;
    int loop3 = 0;
    while (loop2 == 0){

        allTowns[count].name = nameEntered; 
        allTowns[count].population = populationEntered; //the error relates back to here according to bluej
        allTowns[count].county = countyEntered;
        if (continuation == 1) {
            loop2 = 1;
            System.out.println("please enter the name of a county for which you wish to know the details.");
            String countyOfChoice = input.nextLine();
            while (loop3 > 0){

                    if ((allTowns[loop3].county).equals(countyOfChoice)){
                        System.out.println(allTowns[loop3].name);
                        System.out.println(allTowns[loop3].population);
                        loop3 = -2;
                    }
                     loop3 = loop3 +1;   
            }
        }
        count = count + 1;

    }
}

}

4

3 回答 3

4

默认情况下,Object数组中的元素。null在尝试访问元素之前初始化元素

for (int i=0; i < allTowns.length; i++) {
    allTowns[i] = new CathedralTowns();
}
于 2013-10-21T16:26:49.373 回答
0

NullPointerException 发生在“population”而不是“name”是因为“name”字段是静态的,而“population”是非静态的。

此外,CathedralTowns 数组的分配必须按照第一个答案进行。

最终while (loop2 == 0)可能会陷入无限循环。如果用户想要输入多个县的详细信息,则此 while 循环没有结束条件。

于 2013-10-21T16:36:59.317 回答
0

这条线很可疑

allTowns[count] = new CathedralTowns();

您在数组中只分配一个对象,而在分配长度为 50 的数组之前有一行。

CathedralTowns[] allTowns = new CathedralTowns[50];

更不用说它很容易出现ArrayIndexOutOfBoundsExceptionifcount等于或大于50

然后你开始循环和递增count,这就是它发生的地方!

您应该遍历整个数组并在每个插槽中分配一个对象。

于 2013-10-21T16:33:17.577 回答