【问题标题】:NullPointerException thrown by code [closed]代码抛出 NullPointerException [关闭]
【发布时间】: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 时,它告诉我recIDnullprocessor.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


【解决方案1】:

编译完所有粘贴的sn-ps后(http://ideone.com/f2nwnB,下次请完整发布SSCCE以节省其他人的时间......你错过了RecordTypeenum和其他一些东西)我已经发现你的错误。您正在使用(可能是由于拼写错误)

in.useDelimiter(",\n");

而不是

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

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

【讨论】:

  • 您能解释一下(或发布一个链接)SSCCE 是什么吗?请,谢谢。
  • After compiling all the snippets you pasted ...你的意思是,After reading Toms comment :P。很好,我们都发现了问题,你让我不用写答案:D。 +1
猜你喜欢
  • 1970-01-01
  • 2012-11-25
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 2015-06-02
  • 2015-04-23
  • 1970-01-01
  • 2011-03-20
相关资源
最近更新 更多