【发布时间】:2014-11-21 21:18:48
【问题描述】:
我的任务是从文件中读取数据,对其进行格式化,然后将其输出到另一个 .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;
}
}
以及main的相关部分
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 时,它告诉我recID 是null。 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
【问题讨论】:
-
您是否已单步执行代码?错误的完整堆栈跟踪是什么?
-
嗯,看起来,reader.getNextRecord 在这一行 recType = RecordType.valueOf(reader.getNextRecord());返回空值。考虑到您说 recID 也是 null,我想说,读者在第一次读取 null 后返回
-
问题出在 recType = RecordType.valueOf(reader.getNextRecord());似乎 reader.getNextRecord() 返回 null。但我需要输入文件才能找到错误所在
-
此输入数据不适用于您指定的分隔符
",\n"。如果您想阅读整行,请使用in.nextLine()和in.hasNextLine()。 (编辑:您的程序仍然可以读取该文件,但它会一次读取整个文件) -
附带说明,
RecordType.valueOf("GAS")应该是RecordType.GAS
标签: java design-patterns nullpointerexception java.util.scanner token