最近我开始编写一些 Java 2D 代码。
我做的:
public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D) comp;
Font fontx = new Font("Verdana", Font.BOLD, 5);
comp2D.setFont(fontx);
comp2D.drawString("Hello World!", 5, 50);
}
我确实导入了 JFrame 和 java.awt.*,但还是有问题。
当我运行它时,我得到了这个:
Exception in thread "main" java.lang.NullPointerException
at game.Game.paintComponent(Game.java:41) - comp2D.setFont(fontx); - Sets Font
at game.Game.next(Game.java:36) - paintComponent(null); - calls the paintComponent public void from the next() public void
at game.Game.main(Game.java:26) - next.next(); - calls a public void called "next" using an object called "next" (this public void throws InterruptedException)
Java Result: 1
我该如何解决?