简体   繁体   English

Java 2D NullPointerException

[英]Java 2D NullPointerException

Recently I've started coding some Java 2D. 最近,我开始对Java 2D进行编码。

I made this: 我做的:

    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);
}  

I did import JFrame and java.awt.*, but there's still a problem. 我确实导入了JFrame和java.awt。*,但是仍然存在问题。

When I run it, I get this: 运行它时,我得到以下信息:

    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

How can I solve it? 我该如何解决?

You state: 您声明:

Exception in thread "main" java.lang.NullPointerException
at game.Game.paintComponent(Game.java:41) -  
    comp2D.setFont(fontx); - Sets Font

This means that comp2D is null and you're trying to call a method on a null variable. 这意味着comp2D为null,并且您试图在null变量上调用方法。

at game.Game.next(Game.java:36) - paintComponent(null); 
     - calls the paintComponent public void from the next() public void

This means that you're calling paintComponent directly and passing in null! 这意味着您要直接调用paintComponent并传递null!

So you're calling paintComponent directly and passing in null! 因此,您要直接调用paintComponent并传递null! It should come as no surprise that the Graphics object is null and will throw a NPE if you try to call methods on it. 毫不奇怪,Graphics对象为null,如果您尝试在其上调用方法,它将抛出NPE。

Solution: 解:

  • You almost never call paintComponent directly. 您几乎永远不会直接调用paintComponent。
  • Instead have the JVM call it when you call repaint(). 而是在调用repaint()时让JVM调用它。 The JVM will pass in a valid Graphics object. JVM将传入有效的Graphics对象。
  • Most important -- read the painting with Swing tutorial . 最重要的是- 使用Swing教程阅读绘画 You can't guess at this stuff and expect it to work. 您无法猜测这些东西并期望它能工作。
  • Be sure that your paintComponent method is held within a JPanel or other JComponent derived component. 确保您的paintComponent方法保存在JPanel或其他JComponent派生的组件中。
  • Be sure that your override is valid by using the @Override annotation for paintComponent. 通过对paintComponent使用@Override批注,确保您的替代有效。
  • Don't neglect to call the super.paintComponent(...) method within your override. 不要忽略在覆盖范围内调用super.paintComponent(...)方法。
  • For example, have your other method change a class field, say have it change a String field called text, then call repaint() , and then have your paintComponent(...) method use the text field for the text to print in the JPanel. 例如,让您的其他方法更改一个类字段,比如说让它更改一个名为text的String字段,然后调用repaint() ,然后让paintComponent(...)方法使用该文本字段在屏幕上打印文本JPanel的。 This is just an example. 这只是一个例子。 You can change any of the drawing component's fields, and then use them inside of paintComponent(...) . 您可以更改任何图形组件的字段,然后在paintComponent(...)内部使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM