简体   繁体   中英

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.

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)

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 . The System.out.writeln calls were inserted for debugging. 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. 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. How can the object be null when it wasn't null immediately before the method was called? Does anyone have any ideas? I should mention that the field "this.file" is a RandomAccessFile opened with "rw".

DataTable.java:255 is the line

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

DataTable.java:159 is the line

writeExternal(this.current);

and Setup.java:62 is the line

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 . 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.

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 . 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.

Change the type of the parameter to Integer and make sure the method handles being passed a null.

Disclaimer: I may have the field and method names wrong, but I'm pretty sure about the general reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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