I looked through the network and found out how to add an image to JPanel .
This works fine (when the image is loaded into the ImagePanel constructor), but I want to dynamically change the image (for example: when a button is clicked). I tried to implement this, but keep getting Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException when I click the button.
Can anyone help? I am new to Java. Thanks!
Here is my extended JPanel class:
import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JPanel; public class ImagePanel extends JPanel{ private BufferedImage image; public ImagePanel() { this.loadImage("/home/raph/Images/shortcurt_unity.png"); } public void loadImage(String filename) { try { this.image = ImageIO.read(new File(filename)); this.repaint(); } catch (IOException ex) {
I ran it in my JFrame :
private static ImagePanel jPanel1;
And Netbeans automatically generated the following (extracting jpanel-related code):
javax.swing.JPanel jPanel1 = new ImagePanel(); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 1260, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 124, Short.MAX_VALUE) );
The call I make with the button:
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) { jPanel1.loadImage("/home/raph/Images/shortcurt_unity.png"); }
Please note that when in debugging I cannot enter loadImage , I get an exception until it is reached:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at musicchecker.MusicCheckerGUI.jButton10ActionPerformed(MusicCheckerGUI.java:276) at musicchecker.MusicCheckerGUI.access$400(MusicCheckerGUI.java:26) at musicchecker.MusicCheckerGUI$5.actionPerformed(MusicCheckerGUI.java:124) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6267) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6032) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
source share