简体   繁体   English

Java Applet错误,NullPointerException

[英]Java Applet Error, NullPointerException

I just started learning about java applets and came upon this error. 我刚刚开始学习Java小程序,并遇到了此错误。 Can someone help me? 有人能帮我吗?

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;


public class MakeDots extends Applet implements MouseListener{

    private Graphics graphics = null;

    public void init() {
        this.addMouseListener(this);
    }

    public void paint(Graphics g) {
        this.setSize(800, 600);
        g.create();
    }
    public void drawDot(int x, int y) {
        int r = (int)(Math.random()*256);
        int b = (int)(Math.random()*256);
        int g = (int)(Math.random()*256);
        Color color = new Color(r, g, b);
        graphics.setColor(color);
        graphics.fillOval(x, y, 3, 3);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int mouseX = e.getX();
        int mouseY = e.getY();
        drawDot(mouseX, mouseY);
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

}

This error comes up: 出现此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MakeDots.drawDot(MakeDots.java:26)
    at MakeDots.mouseClicked(MakeDots.java:34)
    at java.awt.Component.processMouseEvent(Component.java:6417)
    at java.awt.Component.processEvent(Component.java:6179)
at java.awt.Component.dispatchEventImpl(Component.java:4776)
    at java.awt.Container.dispatchEventImpl(Container.java:2142)
    at java.awt.Component.dispatchEvent(Component.java:4604)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:717)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:690)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:687)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I don't know why! 我不知道为什么!

This is not how you do Swing or AWT drawing: 这不是您执行Swing或AWT绘图的方式:

private Graphics graphics = null;

Instead if an AWT application, you should draw in the paint method of a Component or child of this class) and use the Graphics object provided by the JVM. 相反,如果是AWT应用程序,则应绘制Component的paint方法或此类的子级),并使用JVM提供的Graphics对象。

Much better would be to create a Swing application and draw in the paintComponent(Graphics g) method of a JComponent or child of this, again using the Graphics object provided by the JVM. 最好使用JVM提供的Graphics对象,创建一个Swing应用程序并绘制JComponent的paintComponent(Graphics g)方法或该方法的子级。 Most important, Google and read the tutorials. 最重要的是,Google并阅读了教程。 I can speak from experience by telling you that you shouldn't guess at this stuff as you'll invariably guess wrong. 我可以通过经验告诉您,您不应该猜这些东西,因为您总是会猜错。

You're trying to call a method on the null pointer graphics . 您试图在空指针graphics上调用方法。 Graphics must be instantiated and point to an instance before you can call methods on it. 图形必须实例化并指向实例,然后才能在其上调用方法。

See more here What is a NullPointerException, and how do I fix it? 在此处查看更多信息什么是NullPointerException,以及如何解决?

Although both the answers were helpful, none of them gave me a solution. 尽管两个答案都很有帮助,但没有一个给我解决方案。 Kon's answer gave me what was wrong: 康的回答给了我什么地方出了错:
g.create();
Then I found out it should be: 然后我发现应该是:
graphics = g.create();

Again, thanks for all the suggestions! 再次感谢您的所有建议!

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

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