我正在尝试编写的程序有点问题。我希望能够从文件选择器中获取图片并将它们打印在 JPanel 上。现在可以检索图片,但我似乎无法将它们放入 JLabels 数组中,我得到了 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)