i'm trying to calculate the distance between 2 points with Java Point, it works fine till i say for example: double point1x = point1.getX();. cause when i do, i receive a error. can somebody help me find the problem in my code?
public class ClickAndCalc extends JFrame implements MouseListener {
public ClickAndCalc() throws IOException{
addMouseListener(this);
}
public static void Window() throws IOException {
String path = "kaakfoto.jpg"; //imported image
File file = new File(path);
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
JLabel label = new JLabel(new ImageIcon(image)); //add the image to the JLabel
System.out.println("height: " + height); //print out the height and width of the image
System.out.println("width: " + width);
ClickAndCalc frame = new ClickAndCalc(); //make a new frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.setSize(width+3, height+26); //set the size of the frame the same as the size of the image
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
int i=0;
Point p1, p2, p3;
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) { //when clicked add point for 3 times
if(i==0) {
p1 = e.getPoint();
System.out.println("coordinates Point1: X=" + p1.getX() + ". Y=" + p1.getY());
}
else if(i==1) {
p2 = e.getPoint();
System.out.println("coordinates Point2: X=" + p2.getX() + ". Y=" + p2.getY());
}
else if(i==2) {
p3 = e.getPoint();
System.out.println("coordinates Point3: X=" + p3.getX() + ". Y=" + p3.getY());
}
i++;
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
//down here is some mistake i can't find
double distanceAB(double x1, double y1, double x2, double y2) {
return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}
double x1 = p1.getX(), //convert getX and getY to doubles
y1 = p1.getY(),
x2 = p2.getX(),
y2 = p2.getY(),
distanceAB;{
distanceAB = distanceAB(x1, y1, x2, y2); //calculate the distance between point A and B
System.out.println("the distance between the points AB is " + distanceAB + ".");
}
public static void main(String[] args) throws IOException {
Window(); //add the window (frame, label, image)
}
}
The problem is here:
double x1 = p1.getX(), //convert getX and getY to doubles
y1 = p1.getY(),
x2 = p2.getX(),
y2 = p2.getY(),
distanceAB;
You are defining member variable of the class ClickAndCalc
. These member variables are initialized when a new instance of the class is created, and that is the moment p1.getX()
, p1.getY()
, p2.getX()
and p2.getY()
are called. At this point, however, p1
and p2
are null
, so you get a NullPointerException
.
You can call calculate the distance between p1
and p2
only when you've determined the coordinates, that is, after you've clicked twice on the frame. The easiest solution is to move the code to the mousePressed
method:
public void mousePressed(MouseEvent e) { //when clicked add point for 3 times
if (i==0) {
...
}
else if (i==1) {
p2 = e.getPoint();
System.out.println("coordinates Point2: X=" + p2.getX() + ". Y=" + p2.getY());
double x1 = p1.getX();
double y1 = p1.getY();
double x2 = p2.getX();
double y2 = p2.getY();
double distanceAB = distanceAB(x1, y1, x2, y2);
System.out.println("the distance between the points AB is " + distanceAB + ".");
}
...
}