2015-07-10 95 views
0

我一直在尋找一個問題在我的代碼超過4小時,但無法找到它......問題是,我有一個Java類與一個JFrame ,我已經把JList和DefaultListModel放在了一起。然後我有另一個Java類,我嘗試添加項目到DefaultListModel。問題是,一切正常工作,將添加項添加到DefaultListModel。關於這裏有什麼問題的任何想法?添加項目到JList導致java.lang.NullPointerException

主要的Java類:

package ledpanelplayer; 

imports; //IMPORTS 

/** 
* 
* @author Dominik 
*/ 
public class LedPanelPlayer extends JFrame { 

    public static String RUN_PATH; 
    public static String LIB_PATH; 
    public static String PREFERENCES_FILE; 
    public static String LOG_FILE; 

    public static file_helper f; 
public static json_helper j; 
public static Player player; 
public static Preferences preferences; 
public static Schedule s; 
public static Console c; 

    public static JFrame WINDOW_FRAME; 
    public static int WINDOW_W; 
    public static int WINDOW_H; 
    public static String WINDOW_TITLE; 

    public static JList SCHEDULE_LIST; 
    public static DefaultListModel SCHEDULE_LIST_MODEL; 

    public static void main(String[] args) { 
     setLookAndFeel(); 
     setUIFont(new javax.swing.plaf.FontUIResource("Arial",Font.PLAIN,12)); 
     init(); //Init application 
    } 

    private static void setLookAndFeel() {} //Doesn't matter 
    public static void setUIFont (javax.swing.plaf.FontUIResource f){} //Doesn't matter 

    public static void init() { 
     RUN_PATH = new File("").getAbsolutePath()+"\\"; //Get local directory 
     LIB_PATH = RUN_PATH + "\\lib"; //Set libs directory 
     PREFERENCES_FILE = RUN_PATH + "\\preferences.lpp"; //Set preferences file path 
     LOG_FILE = RUN_PATH + "\\log.txt"; //Set log file path 

     f = new file_helper(); //Create new file_helper() 
    c = new Console(); //Create new Console() 
    c.init_files(); 
    j = new json_helper(); //Create new json_helper() 
    player = new Player(); //Create new Player() 
    preferences = new Preferences(); //Create new Preferences() 
    s = new Schedule(); //Create new Schedule() 

     WINDOW_FRAME = new JFrame(); //Create new JFrame() 
     WINDOW_W = 800; 
     WINDOW_H = 540; 
     WINDOW_TITLE = "Demo"; 

     WINDOW_FRAME.setSize(WINDOW_W,WINDOW_H); 
     WINDOW_FRAME.setLocationRelativeTo(null); 
     WINDOW_FRAME.setVisible(true); 
     WINDOW_FRAME.setTitle(WINDOW_TITLE); 
     WINDOW_FRAME.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     WINDOW_FRAME.getContentPane().setLayout(null); 
     WINDOW_FRAME.getContentPane().setBackground(new Color(237, 237, 237)); 
     WINDOW_FRAME.setResizable(false); 

     SCHEDULE_LIST_MODEL = new DefaultListModel(); 
     SCHEDULE_LIST = new JList(SCHEDULE_LIST_MODEL); //Create new JList() 
     SCHEDULE_LIST.setBounds(20, 20, 382, 444); 
     SCHEDULE_LIST.setBackground(new Color(226,226,226)); 
     //SCHEDULE_LIST_MODEL.addElement("<html><b>1.</b> Demo <p> duration: 10s | file: demo.mp4 | repeat: *36000</p></html>"); 
     WINDOW_FRAME.add(SCHEDULE_LIST); 
    } 

    public static void addElement(String e) { 
     SCHEDULE_LIST_MODEL.addElement(e); 
    } 
} 

其他類:

public static void updateList() { 
     for (int i = 0; i < SCHEDULE.size(); i++) { 
      try { 
       JSONObject object = (JSONObject)SCHEDULE.get(i); 
       String name = object.get("name").toString(); 
       String duration = object.get("duration").toString(); 
       String file = object.get("file").toString(); 
       String repeat = object.get("repeat").toString(); 
       LedPanelPlayer.addElement("<html><b>"+i+".</b> "+name+" <p> duration: "+(Integer.parseInt(duration)/1000)+"s | file: "+file+" | repeat: *"+repeat+"</p></html>"); 
      } 
      catch(Exception e) {e.printStackTrace();c.err("Schedule.updateList() : " + e.toString());} 
     } 
    } 

預先感謝您!

堆棧跟蹤:

java.lang.NullPointerException 
    at ledpanelplayer.LedPanelPlayer.addElement(LedPanelPlayer.java:369) 
    at ledpanelplayer.Schedule.updateList(Schedule.java:69) 
    at ledpanelplayer.Preferences.load(Preferences.java:50) 
    at ledpanelplayer.LedPanelPlayer.init(LedPanelPlayer.java:142) 
    at ledpanelplayer.LedPanelPlayer.main(LedPanelPlayer.java:98) 
+3

異常的堆棧跟蹤告訴你(和我們)問題在哪裏。你會不會在你的問題中包含它? – VGR

+1

你的'LedPanelPlayer'定義在哪裏? – Karthik

+0

如果這些字符串都已定義,那麼'LedPanelPlayer'就是你的問題 – BoDidely

回答

1

雖然VGR的回答(在首選項初始化時移動SCHEDULE_LIST_MODEL以使其不爲空)確實解決了你的問題,但我認爲你所遇到的問題僅僅是循環依賴問題的一個症狀,它會使你的程序單調乏味的(因爲你花了大約4小時的時間才發現),而只是在SCHEDULE_LIST_MODEL賦值的地方移動,最好是一個創可貼的解決方案。

您的程序結構如下:LedPanelPlayer對首選項和計劃具有依賴性,首選項對計劃具有依賴性,並且計劃對LedPanelPlayer具有依賴性。你所擁有的循環依賴問題在於LedPanelPlayer和Schedule都是相互依賴的函數。時間表需要LedPanelPlayer.addElement和LedPanelPlayer構造一個時間表(並且可能在未發佈的LedPanelPlayer的某個部分中使用它,否則它只是四處閒逛而無用)。

考慮將SCHEDULE_LIST_MODEL完全從LedPanelPlayer中移出,並使其成爲實際管理它的類的實例字段(Schedule)。然後將Schedule.getScheduleListModel()添加到Schedule中,以便需要處理Scheduling的任何內容都可以簡單地引用它,並使updateList不是靜態的並引用ScheduleListModel的新主目錄。由於首選項需要處理調度信息,因此它的構造函數中需要一個調度實例。

通過這種方式,LedPanelPlayer將依賴於Preferences和Schedule,Preferences對Schedule具有依賴性,但是現在Schedule將不再依賴於LedPanelPlayer正常運行,不僅解決您的直接問題,而且解決根本原因你的代碼更好讀,理解和維護啓動!)。

0

堆棧跟蹤表明您調用預置的load方法,但在你的問題的代碼,你只叫new Preferences()。沒有調用任何加載方法。無論如何,你的堆棧跟蹤是由代碼生成的,該代碼的init方法實際上調用Preferences.load,而Preferences.load又調用Schedule.updateList,後者又調用LedPanelPlayer.addElement。

問題是,所有這些都發生在SCHEDULE_LIST_MODEL被賦值之前。在Java中,默認情況下,所有對象字段都初始化爲null,除非您將它們初始化爲其他值。你不能在null上調用方法,這是你的異常的原因。

在調用任何首選項方法之前,將SCHEDULE_LIST_MODEL的賦值移至一行,並且問題應該消失。