代码抛出NullPointerException

时间:2014-11-21 21:18:49

标签: java design-patterns nullpointerexception java.util.scanner token

我有一项任务是从文件中读取数据,对其进行格式化,然后将其输出到另一个.csv文件。我正在测试我的阅读,我得到一个空指针,我似乎无法弄清楚为什么。涉及三个类和一个接口。我有处理器一次将数据拉出一个完整的记录集。当然主要是将每个记录集编译成一系列ArrayLists。我还有一个Reader,它一次从一个条目中读取文件中的数据。

阅读器

public class CabRecordReader {
boolean moreRecords;
File inputFile;
Scanner in;

public CabRecordReader(String inputPath) throws FileNotFoundException {
    inputFile = new File(inputPath);
    in = new Scanner(inputFile);
    in.useDelimiter(",\n");
    moreRecords = true;
}

public boolean hasMoreRecords() {
    moreRecords = in.hasNext();
    return moreRecords;
}
public String getNextRecord() {
    String line;
    moreRecords = in.hasNext();
    if(moreRecords) {
        line = in.next();
        return line;
    } else {
        return null;
    }
}
}

处理器

package edu.trident.Olliff.AcmeFileRead;

import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CabRecordProcessor implements CabRecord {

String output;
CabRecordReader reader;
RecordType recType;
Date recDate;
String recID;
double recValue;
double recGalCost;

SimpleDateFormat formatter = new SimpleDateFormat("yyyy/mm/dd");

public CabRecordProcessor(String inputPath, String outputPath) {
    try {
        reader = new CabRecordReader(inputPath);
    } catch (FileNotFoundException e) {
        System.err.println("File not Found...");
        e.printStackTrace();
    }
    output = outputPath;
}

public void readFullRecord() {
    try {
        recDate = formatter.parse(reader.getNextRecord());
        recID = reader.getNextRecord();
        recType = RecordType.valueOf(reader.getNextRecord());
        recValue = Double.parseDouble(reader.getNextRecord());
        if(recType == RecordType.valueOf("GAS")) {
            recGalCost = Double.parseDouble(reader.getNextRecord());
        }
    } catch (ParseException e) {
        System.err.println("Error with the Date on " + recID);
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        System.err.println("Error with the Type on " + recID);
        e.printStackTrace();
    } catch (NullPointerException e) {
        System.err.println("Getting this for some reason at " + recID);
        e.printStackTrace();
    }
}

public boolean butWait() {
    return reader.hasMoreRecords();
}

@Override
public RecordType getType() {
    return recType;
}

@Override
public Date getDate() {
    return recDate;
}

@Override
public String getCabId() {
    return recID;
}

@Override
public double getValue() {
    return recValue;
}

@Override
public double getPerGallonCost() {
    return recGalCost;
}

}

主要的相关部分

ArrayList<String> cabIDs = new ArrayList<String>();
    ArrayList<Date> dates = new ArrayList<Date>();
    ArrayList<RecordType> types = new ArrayList<RecordType>();
    ArrayList<Double> values = new ArrayList<Double>();
    ArrayList<Double> gasCosts = new ArrayList<Double>();

    int tracker = 0;

    CabRecordProcessor processor = new CabRecordProcessor(inputPath, outputPath);

    while(processor.butWait()) {
        processor.readFullRecord();
        cabIDs.add(processor.getCabId());
        dates.add(processor.getDate());
        types.add(processor.getType());
        values.add(processor.getValue());
        if(processor.getType() == RecordType.valueOf("GAS")) {
            gasCosts.add(tracker,processor.getPerGallonCost());
        }
        tracker++;
    }

当我抓住NullPointerException时,它告诉我recIDnull。似乎processor.butWait()没有正确评估,但我不明白为什么不。你能解释一下吗?

示例输入

2014/02/19,CAR27,SERVICE,12
2014/02/15,CAR54,FARE,12
2014/02/15,CAR54,SERVICE,12
2014/02/16,CAR54,FARE,12
2014/02/19,CAR54,SERVICE,12
2014/02/18,CAR42,SERVICE,12
2014/02/16,CAR42,FARE,12
2014/02/19,CAR42,SERVICE,12
2014/02/14,CAR27,GAS,18,3.20
2014/02/14,CAR42,GAS,18,3.20
2014/02/20,CAR42,GAS,18,3.20
2014/02/16,CAR27,GAS,20,3.11
2014/02/16,CAR42,GAS,20,3.11
2014/02/14,CAR14,GAS,22,3.20
2014/02/15,CAR14,FARE,22

1 个答案:

答案 0 :(得分:2)

在汇编了您粘贴的所有代码段(http://ideone.com/f2nwnB)之后,请在下次发布完整的SSCCE以保存其他人的时间...您错过了RecordType enum和其他一些那里的东西)我发现你的错误。你正在使用(可能是由于拼写错误)

in.useDelimiter(",\n");

而不是

in.useDelimiter("[,\n]");

。更改它应修复一个您遇到的问题。此外,你应该改进你的方法的名称(其中一些非常笨拙和非标准... butWait()hasMoreRecords(),就像代码中的其他地方一样,在这里会好很多)并使用正确枚举(即RecordType.GAS而不是RecordType.valueOf("GAS"))。