0

我正在制作一个 Java 程序,它允许用户绘制选定的几何对象。

我想知道当我按下绘图按钮时,哪些特定的代码块会产生这个错误。

该代码片段基本上将接收用户输入的值,并将其传递给用于绘制形状/对象的四个类之一。

编辑:这是actionPerforemed全班

   public void actionPerformed (ActionEvent e)
    {
    if (e.getSource() == exitButton)
    {
        System.exit(0);
    }

    if (e.getSource() == clearButton)
    {
        field1.setText("");
        field2.setText("");
        field3.setText("");
        field4.setText("");
        field5.setText("");
        field6.setText("");
        field7.setText("");
        field8.setText("");


        clearButton.setEnabled(false);
        drawButton.setEnabled(false);
    }

    if (e.getSource() == drawButton)
    {
        if (combo.getSelectedIndex() == 1)
        {
            x1 = Integer.parseInt(field1.getText());
            y1 = Integer.parseInt(field4.getText());

            Dot.paintPoint(g, x1, y1);
        }

        if (combo.getSelectedIndex() == 2)
        {
            x1 = Integer.parseInt(field1.getText());
            y1 = Integer.parseInt(field4.getText());
            x2 = Integer.parseInt(field2.getText());
            y2 = Integer.parseInt(field5.getText());


            Line.paintLine(g,x1,y1,x2,y2);
        }

        if (combo.getSelectedIndex() == 3)
        {
            x1 = Integer.parseInt(field1.getText());
            y1 = Integer.parseInt(field4.getText());
            w = Integer.parseInt(field7.getText());
            h = Integer.parseInt(field8.getText());

            Oval.paintOval(g,x1,y1,w,h);
        }

        if (combo.getSelectedIndex() == 4)
        {
            x1 = Integer.parseInt(field1.getText());
            y1 = Integer.parseInt(field4.getText());
            x2 = Integer.parseInt(field2.getText());
            y2 = Integer.parseInt(field5.getText());
            x3 = Integer.parseInt(field3.getText());
            y3 = Integer.parseInt(field6.getText());

            Traingle.paintTriangle(g,x1,y1,x2,y2,x3,y3);
        }
    }
}

以下是四门课。

为了提出一个观点:

import java.awt.*;

public class Dot
{
protected static int x1,y1;


public Dot()
{
    setPoint(0,0);
}

public Dot(int x1, int y1)
{
    setPoint(x1,y1);
}

public void setPoint(int x1, int y1)
{
    this.x1 = x1;
    this.y1 = y1;
}

public static int getX1()
{
    return x1;
}

public static int getY1()
{
    return y1;
}

public static void paintPoint(Graphics g, int x1, int y1)
{
    g.drawString(".", x1,y1);
}

}

制作一条线:

import java.awt.*;

public class Line extends Dot
{
protected static int x2,y2;


public Line()
{
    setPoint(0,0,0,0);
}

public Line(int x1, int y1, int x2, int y2)
{
    setPoint(x1,y1,x2,y2);
}

public void setPoint(int x1, int y1, int x2, int y2)
{
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
}

public static int getX2()
{
    return x2;
}

public static int getY2()
{
    return y2;
}

public static void paintLine(Graphics g, int x1, int y1, int x2, int y2)
{
    g.drawLine(x1,y1,x2,y2);
}



}

制作椭圆形:

import java.awt.*;

public class Oval extends Dot
{
protected static int w,h;


public Oval()
{
    super(0,0);
}

public Oval(int x1, int y1)
{
    super(x1,y1);

}

public void setPoint(int x1, int y1)
{
    this.x1 = x1;
    this.y1 = y1;
    this.w = w;
    this.h = h;
}


public static int getWidth()
{
    return w;
}

public static int getHeight()
{
    return h;
}

public static void paintOval(Graphics g, int x1,int y1, int w, int h)
{
    g.drawOval(x1,y1,w,h);
}



}

并制作一个三角形:

import java.awt.*;

public class Traingle extends Line
{
protected static int x3,y3;


public Traingle()
{
    setPoint(0,0,0,0,0,0);
}

public Traingle(int x1, int y1, int x2, int y2)
{
    setPoint(x1,y1,x2,y2,x3,y3);
}

public void setPoint(int x1, int y1, int x2, int y2,int x3, int y3)
{
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    this.x3 = x3;
    this.y3 = y3;
}

public static int getX3()
{
    return x3;
}

public static int getY3()
{
    return y3;
}

public static void paintTriangle(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3)
{
    g.drawLine(x1,y1,x2,y2);
    g.drawLine(x2,y2,x3,y3);
    g.drawLine(x1,y1,x3,y3);
}



}

错误是这样的:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Dot.paintPoint(Dot.java:36)
at ShapeDriver.actionPerformed(ShapeDriver.java:451)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
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)

任何帮助,将不胜感激。提前致谢。

4

1 回答 1

0

没有足够的代码可以肯定地说,但我认为您的 Graphics 对象为空(您传递给绘制方法的 g )。

现在,更重要的是,这段代码很糟糕。您正在使用静态变量作为变量。我建议您阅读此Java 静态与实例

于 2013-09-29T13:57:37.780 回答