user1351008
user1351008

Reputation: 63

Error in Java line code

http://pastebin.com/gDMy7fPw

Above, Is my code for a shape creator, this is one part but stands alone.

I get an error on

g2d.drawLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());

which basically says that it Cannot invoke getX() on the primitive type double, Cannot invoke getY() on the primitive type double.

I can change any code except that method. Does anyone know how I can get rid of that error as I don't know what it means.

Upvotes: 0

Views: 188

Answers (2)

Betlista
Betlista

Reputation: 10547

Probably you wanted to use:

g2d.drawLine(x1, y1, x2, y2);

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502736

Your p1 and p2 variables are of type double. The first thing to do is work on your understanding. The compiler message says everything you should need:

Cannot invoke getX() on the primitive type double

It's absolutely right - you can't call getX() on double. So next you should work out why you're trying to call getX() at all. Should you not be calling it, or is the type of your variable wrong?

I suspect that instead of these variables:

private double x1;
private double y1;
private double x2;
private double y2;
private double p1;
private double p2;

you actually want:

private Point p1;
private Point p2;

Alternatively, get rid of p1 and p2 completely, and change your call to:

g2d.drawLine(x1, y1, x2, y2);

Upvotes: 6

Related Questions