更新:如果我将代码移动到mouseclick事件中(即不是调用initMarksScreen()而只是直接放入代码)它按预期工作。所以我的问题是在自己的方法中调用代码。这是否意味着如果我想在另一个点或不同的按钮上执行相同的步骤,我必须直接在那里使用代码而不是我可以调用的方法?
我对Java很新,并且尝试创建一个添加包含JTable的JScrollPane的按钮。
它被称为:
public void mouseClicked(java.awt.event.MouseEvent evt) {
initMarksScreen();
}
代码是:
public final void initMarksScreen() {
String[] columnNames = {"Student ID",
"Last Name",
"Firstname",
"Status",
"Degree",
"Candidate No.",
"Stage",
"Year",
"Code",
"Title",
"Grade Mode",
"Mark",
"Result"};
Object[][] data = {
{"100123456", "Cooper","Sheldon", "Signed Up", "BSc Physics","1201234","1","12","PH1001","Blackholes and Revelations","D",new Integer(99),"P"},
{"100123456", "Cooper","Sheldon", "Signed Up", "BSc Physics","1201234","1","12","PH1025","Astrophysics","D",new Integer(95),"P"}
};
JTable tMarks = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(tMarks);
tMarks.setFillsViewportHeight(true);
panelCentral.add(scrollPane); --!!ERROR AT THIS LINE!!
panelCentral.revalidate();
}
面板声明为:
private static final Container panelCentral = null;
由于我对Java很陌生,我想有一个简单的解决办法,但是如果我需要添加一个SSCCE。
一切正常,直到我点击按钮,然后我得到的错误是:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUI.mainScreen.initMarksScreen(mainScreen.java:312)
at GUI.mainScreen$2.mouseClicked(mainScreen.java:183)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:2)
如果panelCentral
是JPanel并且未初始化,则在添加
panelCentral = new JPanel();
panelCentral.add(scrollPane);