[英]Java Slick2D Line Constructor throws NullPointerException
In Java Slick2D, I am attempting to make a line using a constructor that takes two float arrays, as detailed here: http://slick.ninjacave.com/javadoc/org/newdawn/slick/geom/Line.html在 Java Slick2D 中,我试图使用带有两个浮点数组的构造函数来创建一条线,详情如下: http : //slick.ninjacave.com/javadoc/org/newdawn/slick/geom/Line.html
My code is the following:我的代码如下:
float[] floatArray1 = { 10.0f, 155.0f };
float[] floatArray2 = { 20.0f, 165.0f };
Line line1 = new Line ( floatArray1, floatArray2 );
However, this third line (line 263 in my code) throws a NullPointerException:但是,第三行(我的代码中的第 263 行)抛出 NullPointerException:
java.lang.NullPointerException
at org.newdawn.slick.geom.Line.set(Line.java:217)
at org.newdawn.slick.geom.Line.set(Line.java:138)
at org.newdawn.slick.geom.Line.<init>(Line.java:112)
at view.play.Character.checkIntersectionMovementVector(Character.java:263) (my method)
Why is this happening?为什么会这样?
Edit: It is worth noting that using its constructor that takes four float values instead of two float arrays of length two works, and throws no exception:编辑:值得注意的是,使用它的构造函数接受四个浮点值而不是两个长度为 2 的浮点数组是有效的,并且不会抛出任何异常:
Line line = new Line ( 10.0f, 155.0f, 20.0f, 165.0f );
Looks like a bug in the Line
class.看起来像是
Line
类中的一个错误。 The Line.set()
method that is eventually called is this:最终调用的
Line.set()
方法是这样的:
public void set(float sx, float sy, float ex, float ey) {
super.pointsDirty = true;
start.set(sx, sy); // this is line 217
end.set(ex, ey);
float dx = (ex - sx);
float dy = (ey - sy);
vec.set(dx,dy);
lenSquared = (dx * dx) + (dy * dy);
}
However, the start
instance variable of the Line
class isn't initialized in the constructor you call:但是,
Line
类的start
实例变量并未在您调用的构造函数中初始化:
public Line(float[] start, float[] end) {
super();
set(start, end); // line 112
}
You should report the bug to the Slick2d maintainers.您应该向 Slick2d 维护者报告该错误。 As a workaround, you should be able to use the
Vector2f
input constructor:作为一种解决方法,您应该能够使用
Vector2f
输入构造函数:
public Line(Vector2f start, Vector2f end)
As the set()
method used here does initialize start
:由于这里使用的
set()
方法确实初始化了start
:
public void set(Vector2f start, Vector2f end) {
super.pointsDirty = true;
if (this.start == null) {
this.start = new Vector2f();
}
this.start.set(start);
if (this.end == null) {
this.end = new Vector2f();
}
this.end.set(end);
vec = new Vector2f(end);
vec.sub(start);
lenSquared = vec.lengthSquared();
}
The four float input constructor also works because it calls the Vector2f
constructor above:四浮点输入构造函数也可以工作,因为它调用了上面的
Vector2f
构造函数:
public Line(float x1, float y1, float x2, float y2) {
this(new Vector2f(x1, y1), new Vector2f(x2, y2));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.