gpt4 book ai didi

java - 数组上出现空异常

转载 作者:行者123 更新时间:2023-12-01 23:49:45 24 4
gpt4 key购买 nike

我对 Java 和编程非常陌生,正在从事一个类项目,我们正在学习 I/O、数组和对象,并且我们有一些非常具体的指导原则需要遵循。当编译器到达“countryInfo[count].setName(name);”时,它给了我

线程“main”中出现异常 java.lang.NullPointerException 在 Main.main(Main.java:53)

如果我将其注释掉,下一行会给我同样的错误。我确信有更有效的方法来重写这段代码,但由于我们是新手,我们不允许这样做。在我问之前,我读了很多关于空异常的内容……如果我在某个地方错过了它,我深表歉意。我迷路了:(

public class Main {

/**
* @param args the command line arguments
*/
private static Country[] countryInfo = new Country[43];

public static void main(String[] args) throws IOException {
String name = "";
String capital = "";
String region = "";
int region_Nbr = 0;
int capital_population = 0;

// TODO code application logic here
String filename = "Countries.txt";
String inputString;

FileInputStream fis1 = new FileInputStream(filename);
BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
inputString = br1.readLine();

while (inputString != null) {
int count = 0;
System.out.print(inputString + "\n");

name = inputString.substring(0, 13).trim();
//System.out.print(name + ", "); //echo

capital = inputString.substring(24, 36).trim();
//System.out.print(capital + ", ");//echo

region = inputString.substring(40, 56).trim();
//System.out.print(region + ", "); //echo

region_Nbr = Integer.parseInt(inputString.substring(64, 66).trim());
//System.out.print(region_Nbr + ", ");//echo

capital_population = Integer.parseInt(inputString.substring(72, inputString.length()).trim());
//System.out.print(capital_population + "\n");

countryInfo[count].setName(name);
countryInfo[count].setCapital(capital);
countryInfo[count].setRegion(region);
countryInfo[count].setRegionNum(region_Nbr);
countryInfo[count].setPopulation(capital_population);
inputString = br1.readLine();

count++;
} //end while

}

}

public class Country {

private String name;
private String capital;
private String region;
private int region_Nbr;
private int capital_population;



public void setName(String w) {
this.name = w;

} //end get name

最佳答案

你永远不会用对象填充你的数组。目前你有一个数组,但它是空的并且只填充了空值。解决方案是在使用之前创建对象来填充它。将数组想象成类似于鸡蛋箱。你不能用 crate 里的鸡蛋做饭,除非你先把鸡蛋装满。

    countryInfo[count] = new Country(); // **** add
countryInfo[count].setName(name);
countryInfo[count].setCapital(capital);
countryInfo[count].setRegion(region);
countryInfo[count].setRegionNum(region_Nbr);
countryInfo[count].setPopulation(capital_population);
inputString = br1.readLine();
<小时/>

编辑,您声明:

I'm not sure I understand why I needed to add that line, I thought that private static Country[] countryInfo = new Country[43];

当您创建数组对象(也称为引用类型数组)时,您将创建一个空变量集合,这些变量被赋予任何引用变量的默认值 null。这与创建基元类型的数组不同,后者会将数组初始化为基元的默认值(0 表示整数, false 表示 boolean 值,等等)。

同样,这类似于创建一个空的鸡蛋盒或一个 parking 场。为了使数组充满对象,您必须自己将对象放入数组中。您需要将 parking 场装满汽车,然后才能选择一辆并开走一辆。仅仅拥有一个空位的 parking 场对您来说没有任何用处。

关于java - 数组上出现空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16474031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com