Potential loss of precision / [type] cannot be dereferenced
I've looked around a lot, but I just can't seem to find a good solution for this ...
Point mouse = MouseInfo.getPointerInfo().getLocation();
int dx = (BULLET_SPEED*Math.abs(x - mouse.getX()))/
(Math.abs(y - mouse.getY()) + Math.abs(x - mouse.getX()))*
(x - mouse.getX())/Math.abs(x - mouse.getX());
In this constellation I get: Possible loss of precision when I change, for example (x - mouse.getX())
to (x - mouse.getX()).doubleValue()
, it says double cannot be dereferenced, when I add intValue () somewhere where it says int cannot be dereferenced. What's my mistake?
[x, y - integers | BULLET_SPEED is static final int
]
Thanks!
a source to share
Just click (int)
int i = (int) 3.14159; // compiles fine
This won't avoid losing precision, but it makes it explicit to the compiler what you want, so the code will compile.
Keep in mind where casting is a priority.
int i = (int) .1D + .1D; // doesn't compile!!!
int j = (int) (.1D + .1D); // compiles fine
Related questions
About dereferencing errors
Primitives are not objects; they have no members.
Integer ii = 42; // autoboxing
int i = ii.intValue(); // compiles fine
int j = i.intValue(); // doesn't compile
see also
So, in this particular case, you can do the following:
int dx = (int) (BULLET_SPEED*Math.abs(x - mouse.getX()))/
(Math.abs(y - mouse.getY()) + Math.abs(x - mouse.getX()))*
(x - mouse.getX())/Math.abs(x - mouse.getX());
a source to share