简体   繁体   English

为什么这会引发NullPointerException?

[英]Why is this throwing a NullPointerException?

I'm pulling my hair out trying to figure out why I'm getting a NullPointerException in this code. 我想弄清楚为什么在此代码中出现NullPointerException

Here are the relevant code snippets. 以下是相关的代码段。

(main) (主要)

...

    try (RawData setupData = new RawData(
            new File(fileName), log);) {

        while (true) {

            record = setupData.nextRecord();
            if (record == null) 
                break;

            System.out.println(record.getCountryCode());
            System.out.println(record.getCountryID());
            table.put(record);
            index.put(record);

            count++;

        }


    } catch (IOException e) {

        e.printStackTrace();

    } 

...

(put method) (投入方法)

public void put(DataTableRecord country) {

    this.current = country;

    try {

        this.file.seek(this.current.getCountryID() * RECORD_SIZE);
        if (!exists(this.current.getCountryID())) {

            writeExternal(this.current);
            this.size++;
            if (country.getCountryID() > this.last)
                this.last = country.getCountryID();

        }

    } catch (IOException e) {

        e.printStackTrace();

    } 

}

(writeExternal method) (writeExternal方法)

private void writeExternal(DataTableRecord data) throws IOException {

    System.out.println(data.getCountryCode());
    this.file.writeUTF(data.getCountryCode());
    this.file.writeShort(data.getCountryID());
    this.file.writeUTF(data.getName());
    switch (data.getContinent()) {

    case AFRICA :
        this.file.writeShort(1);
        break;

    ...

    case SOUTH_AMERICA :
        this.file.writeShort(7);
        break;

    }

    this.file.writeInt(data.getArea());
    this.file.writeLong(data.getPopulation());
    this.file.writeFloat(data.getLifeExpectancy());

}

Every time, the loop works successfully twice, and on the third iteration gives me a NullPointerException . 每次循环都成功执行两次,并且在第三次迭代中,我得到了NullPointerException The System.out.writeln calls were inserted for debugging. 已插入System.out.writeln调用以进行调试。 From the writeln calls, I can see that the object that I pass to the writeExternal method is not null immediately before the method gets called. writeln调用中,我可以看到传递给writeExternal方法的对象在调用该方法之前不为null。 On the third iteration, I get the following error: 在第三次迭代中,出现以下错误:

Exception in thread "main" java.lang.NullPointerException
    at edu.wmich.cs3310.jwhite_cotw.DataTable.writeExternal(DataTable.java:255)
    at edu.wmich.cs3310.jwhite_cotw.DataTable.put(DataTable.java:159)
    at edu.wmich.cs3310.jwhite_cotw.Setup.main(Setup.java:62)

I can't figure out where the null is coming from. 我不知道null的来源。 How can the object be null when it wasn't null immediately before the method was called? 对象如何能为空时,它不是null立即调用这个方法之前? Does anyone have any ideas? 有人有什么想法吗? I should mention that the field "this.file" is a RandomAccessFile opened with "rw". 我应该提到字段“ this.file”是一个用“ rw”打开的RandomAccessFile

DataTable.java:255 is the line DataTable.java:255是这一行

this.file.writeUTF(data.getCountryCode());

DataTable.java:159 is the line DataTable.java:159是这一行

writeExternal(this.current);

and Setup.java:62 is the line 和Setup.java:62是行

table.put(record);

Any suggestions (even just a point in the right direction) would be most appreciated. 任何建议(甚至只是正确方向的一点)都将受到赞赏。

The error is when parameter to writeExternal is null . 错误是当writeExternal参数为null So the statement is problematic 所以这个陈述是有问题的

writeExternal(this.current);

if you change that to 如果您将其更改为

writeExternal(country);

it could save you from error, but probably you should redesign your code to handle nullpointer exception or at least declare it using throws clause. 它可以使您免于出错,但是您可能应该重新设计代码以处理nullpointer异常,或者至少使用throws子句对其进行声明。

Because I'm psychic, I can tell what the problem is... 因为我很通灵,所以我可以说出问题所在...

The countryCode field has type Integer , but the parameter type of exists() is int . countryCode字段的类型为Integer ,但exist exists()的参数类型为int This means that when passing countryCode to the method, the Integer is auto-unboxed to an int, but if countryCode is null this action causes an NPE. 这意味着当将countryCode传递给该方法时,Integer将自动拆箱为int,但是如果countryCode为null,则此操作将导致NPE。

Change the type of the parameter to Integer and make sure the method handles being passed a null. 将参数的类型更改为Integer,并确保该方法处理传递的null。

Disclaimer: I may have the field and method names wrong, but I'm pretty sure about the general reason. 免责声明:字段和方法名称可能不正确,但是我很确定一般原因。

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

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