gpt4 book ai didi

java - JFrame java 应用程序中找不到资源

转载 作者:行者123 更新时间:2023-12-01 12:57:29 25 4
gpt4 key购买 nike

我目前正在使用 JFrama 构建一个小型 Java 游戏。那年我得到了一些外星人,它们都是图像,并且加载正确。现在我想为玩家放置图像,但是当我这样做时,我的整个项目都烧毁了。我遇到这样的异常

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gamepackage.Game.draw(Game.java:50)
at gamepackage.Game.paint(Game.java:45)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1100(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)

我试图找出问题出在哪里,但最终还是到了这里ImageIcon ii = new ImageIcon(this.getClass().getResource(player));

playerprivate String player = "../img/player_64.png";

外星人的图像以相同的方式加载,并且两张图片的方向相同。所以我尝试了很多方法,但到目前为止没有任何效果。

最佳答案

如果您查看 getResource() 的规范:

Returns: A URL object or null if no resource with this name is found

您应该打印此方法返回的 URL 对象。它很可能为空,这意味着您的路径不正确。

关于java - JFrame java 应用程序中找不到资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23762528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com