Class.getResource()返回null
我正试图在JPanel上显示图片,但我不断收到错误:
java.lang.IllegalArgumentException:input == null!
我不明白发生了什么事。
这是我正在使用的代码:
public void actionPerformed(ActionEvent e) { try { Image image=ImageIO.read(getClass().getResource("img/" +num.getText()+".jpg")); Image resized = image.getScaledInstance(200, 200, 100); pictureFrame.setIcon(new ImageIcon(resized)); } catch (Exception ex){ ex.printStackTrace(); } }
这只会导致我得到错误!
堆栈跟踪产生以下内容:
Java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(ImageIO.java:1362) at work.Item.actionPerformed(Item.java:96) 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:6297) at javax.swing.JComponent.processMouseEvent(JComponent.java:3275) at java.awt.Component.processEvent(Component.java:6062) at java.awt.Container.processEvent(Container.java:2039) at java.awt.Component.dispatchEventImpl(Component.java:4660) at java.awt.Container.dispatchEventImpl(Container.java:2097) at java.awt.Component.dispatchEvent(Component.java:4488) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166) at java.awt.Container.dispatchEventImpl(Container.java:2083) at java.awt.Window.dispatchEventImpl(Window.java:2489) at java.awt.Component.dispatchEvent(Component.java:4488) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:668) at java.awt.EventQueue.access$400(EventQueue.java:81) at java.awt.EventQueue$2.run(EventQueue.java:627) at java.awt.EventQueue$2.run(EventQueue.java:625) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) at java.awt.EventQueue$3.run(EventQueue.java:641) at java.awt.EventQueue$3.run(EventQueue.java:639) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:638) 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)
我该如何解决这个问题? 我已经检查了图像的位置,并尝试从不同的位置,总是得到相同的错误!
我正在使用Netbeans IDE。
假设getClass()
返回com.foo.bar.MyActionListener
,则getClass().getResource("img/foo.jpg")
将在foo.jpg
包中com.foo.bar.img
名为foo.jpg
的文件。 如果图像不在此包中,或者如果它位于此包中,但其根目录不在类path中,则该方法将返回null。
如果img文件夹位于类path的根目录,则应该使用getClass().getResource("/img/foo.jpg")
(注意前导/
)或getClass().getClassLoader().getResource("img/foo.jpg")
。
你应该给你的源文件的相对path。 例如,如果你有这个:
src --img --classes
而你在classes
文件夹,你应该写这个:
getClass().getResource("../img/" +num.getText()+".jpg")
- 当readdir()返回文件名时,stat()错误'No such file or directory'
- java.lang.ClassNotFoundException:org.hibernate.HibernateException