我正在尝试编写的程序有点问题。我希望能够从filechooser获取图片并将其打印在JPanel上。现在检索图片有效,但我似乎无法将它们放在JLabel数组中,我得到一个NullPointerException。我知道这个例外意味着什么,但我看不出我做错了什么或怎么做,有人能帮助我找到我的错误吗?
private class OpenFileHandler implements ActionListener {
public BufferedImage [] getBufferedImages() {
chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new FileNameExtensionFilter("Images", "PNG", "JPG", "GIF", "BMP"));
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
imagesFile = chooser.getSelectedFiles();
}
imagesBuf = new BufferedImage[imagesFile.length];
for (int i = 0; i<imagesFile.length; i++) {
try {
imagesBuf[i] = ImageIO.read(imagesFile[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
return imagesBuf;
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == openMenu) {
getBufferedImages();
for (int i = 0; i<imagesBuf.length; i++) {
labels[i] = new JLabel(new ImageIcon(imagesBuf[i]));
picturePanel.add(labels[i]);
}
}
}
}
这就是我得到的错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at be.maarten.gallery.GalleryFrame$OpenFileHandler.actionPerformed(GalleryFrame.java:118)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$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 :(得分:1)
如果您的GalleryFrame
行118
指向以下行:
picturePanel.add(labels[i]);
然后我认为picturePanel
未初始化。请在actionPerformed
方法中使用之前更正/执行初始化。它应该工作正常。
如果不是这种情况,请在第118行分享声明。