解決したいこと
Java初心者です。
SwingでGUI開発をしています。ボタンのクリックイベントによって
JLabelに表示させるテキストを変更させようとしたときにエラーが発生しました。
発生している問題・エラー
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.JLabel.setText(String)" because "this.lblNewLabel" is null at testFotm.actionPerformed(testFotm.java:56) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342) at java.desktop/java.awt.Component.processEvent(Component.java:6379) at java.desktop/java.awt.Container.processEvent(Container.java:2263) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
該当するソースコード
java
1 2import java.awt.BorderLayout; 3import java.awt.EventQueue; 4 5import javax.swing.JFrame; 6import javax.swing.JPanel; 7import javax.swing.border.EmptyBorder; 8import javax.swing.JButton; 9import javax.swing.JLabel; 10import java.awt.Font; 11import javax.swing.SwingConstants; 12import java.awt.event.ActionListener; 13import java.awt.event.ActionEvent; 14 15public class testFotm extends JFrame implements ActionListener { 16 private JPanel contentPane; 17 private JButton btnNewButton; 18 private JLabel lblNewLabel; 19 20 /** 21 * Launch the application. 22 */ 23 public static void main(String[] args) { 24 EventQueue.invokeLater(new Runnable() { 25 public void run() { 26 try { 27 testFotm frame = new testFotm(); 28 frame.setVisible(true); 29 } catch (Exception e) { 30 e.printStackTrace(); 31 } 32 } 33 }); 34 } 35 36 /** 37 * Create the frame. 38 */ 39 public testFotm() { 40 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 41 setBounds(100, 100, 450, 300); 42 contentPane = new JPanel(); 43 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 44 contentPane.setLayout(new BorderLayout(0, 0)); 45 setContentPane(contentPane); 46 47 JButton btnNewButton = new JButton("\u30AF\u30EA\u30C3\u30AF\u3057\u3066\u304F\u3060\u3055\u3044"); 48 btnNewButton.addActionListener(this); 49 contentPane.add(btnNewButton, BorderLayout.SOUTH); 50 51 JLabel lblNewLabel = new JLabel("\u30AF\u30EA\u30C3\u30AF\u3055\u308C\u305F\u3089\u6587\u5B57\u304C\u304B\u308F\u308A\u307E\u3059\uFF01"); 52 lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER); 53 lblNewLabel.setFont(new Font("UD デジタル 教科書体 NP-B", Font.PLAIN, 18)); 54 contentPane.add(lblNewLabel, BorderLayout.CENTER); 55 } 56 public void actionPerformed(ActionEvent event) { 57 lblNewLabel.setText("クリックされました。"); 58 } 59 60} 61
自分で試したこと
検索
###環境
Windowsのeclipse
回答1件
あなたの回答
tips
プレビュー