This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                3年前关闭。
            
        

ist怎么会得出c,而今天在fetchingData()中不可见?
我在主类中运行代码时收到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));

最佳答案

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

  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));
    }

10-07 12:51
查看更多