我一直在尋找一個問題在我的代碼超過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)
異常的堆棧跟蹤告訴你(和我們)問題在哪裏。你會不會在你的問題中包含它? – VGR
你的'LedPanelPlayer'定義在哪裏? – Karthik
如果這些字符串都已定義,那麼'LedPanelPlayer'就是你的問題 – BoDidely