NullPointerException构造函数

时间:2017-03-03 10:23:33

标签: java nullpointerexception

怎么会出现c和今天在fetchingData()中不可见? 在main-class中运行代码时出现NullPointer-Exception:/ 谢谢你的帮助!

public class InputData {

    Calendar c;
    Date today;

    String DATE_FORMAT = "MM/dd/yyyy";
    SimpleDateFormat sdf;

    Double[][] hPrice;
    Double[][] qhPrice;
    Double[][] qhEua;
    Double[][] qhGas;
    Integer[][] qhTemperature;
    Integer[][] qhAirpressure;

    Date[][] qhDate; // may be useful some day
    Date[][] qhWeatherDate; // may be useful some day

    public InputData() {
        Calendar c = Calendar.getInstance();
        Date today = new Date();
        today = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        System.out.println(sdf.format(today));
    }

    public void startFetching() {
        // +++ Access-Import EUA's +++
        Access aQuery = new Access();
        c.add(Calendar.DATE, -1);
        today = c.getTime();
        aQuery.eua(sdf.format(today));

1 个答案:

答案 0 :(得分:3)

您将对象保存在局部变量中,并且不使用类成员。 更改您的代码:

  public InputData() {
        this.c = Calendar.getInstance();
        this.today = new Date();
        today = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        System.out.println(sdf.format(today));
    }