gpt4 book ai didi

java - JFrame Paint() 上的 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 05:06:12 26 4
gpt4 key购买 nike

我无法弄清楚为什么会抛出 nullpointerexception。它并没有真正影响任何东西(至少我不认为),因为一切仍然被正确地绘制出来,但我不喜欢错误消息。

如您所见,paint() 调用了 this.game.paint(g2)。为了尝试调试,this.game.print 中的所有内容都已被注释掉 - 该函数不执行任何操作,因此它不应该成为问题的一部分。

public class Application extends JFrame {

private Game game;///<stores the rules for the game and facilitates interactions

public Application() throws RoyException {
this.game = new Game(this, 2);
}


// @Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
try{
this.game.paint(g2);
}catch(Exception e)
{
System.out.println("CAUGHT " + e.getMessage());
e.printStackTrace();
}
}

public static void main(String[] args) {
try {
Application app = new Application();
} catch (RoyException e) {
System.out.println("An error occurred while running the program: " + e.getMessage());
System.out.print("Stack trace: ");
e.getStackTrace();
}
}

}

这是堆栈跟踪 - Application.java:153 is this.game.paint(g2);

CAUGHT null java.lang.NullPointerException
at bullshitakemushrooms.Application.paint(Application.java:153)
Game created.
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:671)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

最佳答案

显然,在设置可见性/大小后设置窗口的图标图像是一个坏主意。

移动此行后

this.app.setIconImage(new ImageIcon("mushy.jpg").getImage());

之前

    this.app.setSize(this.maxX, this.maxY);
this.mmenu.setVisible(true);
app.setVisible(true);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setAlwaysOnTop(true);

空指针异常抛出已停止。经验教训:首先设置窗口图标。

关于java - JFrame Paint() 上的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12633749/

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