I am making a Java program which allows a user to draw a selected geometric object.
I would like to know what particular block/s of code produces this error when I press the Draw Button.
This code snippet will basically receive the values inputted from the user and will pass it to either one of four classes which is used for drawing the shape/object.
EDIT: Here is the full actionPerforemed
class
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);
}
}
}
Here are the four classes.
For Making a Point:
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);
}
}
For Making a Line:
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);
}
}
For Making an Oval:
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);
}
}
And for making a Triangle:
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);
}
}
And the error goes like this:
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)
Any help would be appreciated. Thanks in advance.