Java Applet错误,NullPointerException

时间:2014-05-10 19:02:07

标签: java nullpointerexception applet

我刚开始学习java applet并遇到了这个错误。有人能帮助我吗?

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

    }

}

出现此错误:

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)

我不知道为什么!

3 个答案:

答案 0 :(得分:3)

这不是你做Swing或AWT绘画的方式:

private Graphics graphics = null;

相反,如果是AWT应用程序,则应该使用此类的Component或子类的paint方法进行绘制,并使用JVM提供的Graphics对象。

更好的方法是创建一个Swing应用程序并使用JVM提供的Graphics对象在JComponent的paintComponent(Graphics g)方法或其子类中绘制。最重要的是谷歌并阅读教程。我可以从经验中说出你不应该猜测这些东西,因为你总是猜错了。

答案 1 :(得分:1)

您尝试在空指针graphics上调用方法。必须先实例化图形并指向实例,然后才能在其上调用方法。

在此处查看更多What is a NullPointerException, and how do I fix it?

答案 2 :(得分:0)

尽管两个答案都有帮助,但没有一个给我一个解决方案。 Kon的回答告诉我出了什么问题:
g.create();
然后我发现它应该是:
graphics = g.create();

再次感谢所有的建议!