你好,我有一个例外
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ModelsEvenements.Actions.test(Actions.java:18)
at ModelsEvenements.Designer$GestionActions.actionPerformed(Designer.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)
当我想要访问另一个班级的标签时。标签受到保护,并且类可以正确扩展
设计师:
public class Designer extends JFrame {
private static final long serialVersionUID = 1L;
public JButton btnCounter;
protected JLabel lblNombreClic;
Actions a;
protected int test = 1;
public Designer() {
}
public Designer(String t) {
super("Essaie Modèle");
lblNombreClic = new JLabel("Number of clics " + 0);
setSize(350, 200);
JPanel panneau = new JPanel();
lblNombreClic.setText( "test" );
btnCounter = new JButton("c");
btnCounter.setMnemonic('C');
GestionActions compteurListener = new GestionActions();
//3-enregistrer le gestionnaire auprès du bouton
btnCounter.addActionListener(compteurListener);
panneau.add(btnCounter);
panneau.add(lblNombreClic);
setContentPane(panneau); // comme add(panneau)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// TODO Auto-generated constructor stub
a = new Actions();
}
public class GestionActions implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
a.test();
}
}
}
操作:
public class Actions extends Designer{
private static final long serialVersionUID = 1L;
private int counter = 0;
public Actions() {
}
public void test( ) {
counter++;
lblNombreClic.setText( "Number of clics " + counter );
}
}
编辑:DESIGNER是我的窗户。 我从main.cs主文件中提取了一个字符串文本。
当我单击按钮时,它会自动执行测试操作。 动作不是无限的,因为它不会再次使用字符串调用构造函数。
它只是向我显示错误。
我希望能够直接在Actions类中使用所有按钮和将来的文本框。
如何修复?