2017-03-03 140 views
0

如何得到c和今天在fetchingData()中不可見? 在main-class中運行代碼時,我得到一個NullPointer異常:/ 感謝您的幫助!NullPointerException構造函數

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)); 
+0

其中是fetchingData()方法? –

+0

'fetchingData()'在哪裏?你的'main()'看起來像什麼?你怎麼知道它是引發Exception的''''?什麼是堆棧跟蹤? – QBrute

+0

對不起其實其startFetching() - 方法 – Andrew

回答

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

ahhh我很笨。對不起,打擾了。非常感謝! – Andrew

+0

不客氣。您也可以將我的答案標記爲解決方案;) – Markus