Java初心者です。
申し訳ありませんが、エラー内容や直し方についてご教示ください。
前提・実現したいこと
javaで選択した画像を透過して重ねて表示させるシステムを作っています。
1枚目の画像を選択して表示した後、
2枚目の画像を選択して表示しようとした際、以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Hello.open2(Hello.java:99) at Hello.access$1(Hello.java:89) at Hello$2.actionPerformed(Hello.java:59) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.AbstractButton.doClick(AbstractButton.java:376) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:842) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:886) at java.awt.Component.processMouseEvent(Component.java:6539) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6304) at java.awt.Container.processEvent(Container.java:2239) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2297) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476) at java.awt.Container.dispatchEventImpl(Container.java:2283) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84) at java.awt.EventQueue$4.run(EventQueue.java:733) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74) at java.awt.EventQueue.dispatchEvent(EventQueue.java:730) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
該当のソースコード
Java
1import java.awt.event.ActionEvent; 2import java.awt.event.ActionListener; 3import java.io.File; 4 5import javax.swing.ImageIcon; 6import javax.swing.JFileChooser; 7import javax.swing.JFrame; 8import javax.swing.JLabel; 9import javax.swing.JMenu; 10import javax.swing.JMenuBar; 11import javax.swing.JMenuItem; 12import javax.swing.filechooser.FileNameExtensionFilter; 13 14public class Hello extends JFrame { 15 JLabel label; 16 JLabel label2; 17 public static void main(String[] args) { 18 new Hello(); 19 } 20 21 public Hello() { 22 init(); 23 } 24 25 private void init() { 26 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 27 setTitle("画像透過"); 28 setBounds(400, 100, 1000, 800); 29 30 // ラベル作成 31 label = new JLabel(); 32 add(label); 33 34 // メニュー作成 35 JMenuBar mb = new JMenuBar(); 36 JMenu m = new JMenu("ファイル(F)"); 37 m.setMnemonic('F'); 38 39 JMenuItem item1 = new JMenuItem("1枚目"); 40 41 JMenuItem item2 = new JMenuItem("2枚目"); 42 43 44 45 // メニューを選んだ時の動作を設定 46 item1.addActionListener(new ActionListener() { 47 @Override 48 public void actionPerformed(ActionEvent e) { 49 // ファイル選択ダイアログを表示し、選択したファイルをラベルに設定 50 // するメソッドを呼び出す 51 open(); 52 } 53 }); 54 item2.addActionListener(new ActionListener() { 55 @Override 56 public void actionPerformed(ActionEvent e) { 57 // ファイル選択ダイアログを表示し、選択したファイルをラベルに設定 58 // するメソッドを呼び出す 59 open2(); 60 } 61 }); 62 63 64 65 66 mb.add(m); 67 m.add(item1); 68 m.add(item2); 69 setJMenuBar(mb); 70 71 // フレームを表示 72 setVisible(true); 73 } 74 75 // ファイル選択ダイアログを表示し、選択したファイルをラベルに設定 76 private void open() { 77 JFileChooser fc = new JFileChooser(); 78 // 画像ファイルの拡張子を設定 79 fc.setFileFilter(new FileNameExtensionFilter("画像ファイル", "png", "jpg", 80 "Jpeg", "GIF", "bmp")); 81 // ファイル選択ダイアログを表示、戻り値がAPPROVE_OPTIONの場合、画像ファイルを開く 82 if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 83 File f = fc.getSelectedFile(); 84 // アイコンをラベルに設定 85 ImageIcon icon = new ImageIcon(f.getPath()); 86 label.setIcon(icon); 87 } 88 } 89 private void open2() { 90 JFileChooser fc = new JFileChooser(); 91 // 画像ファイルの拡張子を設定 92 fc.setFileFilter(new FileNameExtensionFilter("画像ファイル", "png", "jpg", 93 "Jpeg", "GIF", "bmp")); 94 // ファイル選択ダイアログを表示、戻り値がAPPROVE_OPTIONの場合、画像ファイルを開く 95 if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 96 File f = fc.getSelectedFile(); 97 // アイコンをラベルに設定 98 ImageIcon icon = new ImageIcon(f.getPath()); 99 label2.setIcon(icon); 100 } 101 } 102 103} 104
補足情報(FW/ツールのバージョンなど)
使用方法:
1.実行後、フレームを展開
2.ファイルタブの「1枚目」ボタンから画像を選択
→画像を表示
3.ファイルタブの「2枚目」ボタンから画像を選択
→エラー発生
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/28 09:12
2019/07/28 09:27
2019/07/28 10:02