当我尝试在Point2D对象上使用setLocation()方法时,我总是得到一个NullPointerException。我的代码中有6或7个Point2D对象,它只抛出这个对象的异常。下面是代码片段:
导入java.awt.geom.*;
public class Box
{
private Point2D coord;
private int height;
private int width;
public Box()
{
coord.setLocation(0,0);
height = 40;
width = 200;
}
..。
并在此处创建对象:
public MainGUI()
{
frame = new JFrame();
view = new ViewPanel();
controls = new ControlPanel();
ballAction = new Ball();
boxAction = new Box();
springAction = new Spring();
有什么想法吗?谢谢!
发布于 2014-04-20 14:21:15
添加以下内容:
coord = new Point2D();// you may need to pass some parameters to the constructor of the Point2D according to it's definition
在尝试使用它之前。(您尝试在初始化此对象之前访问它)
发布于 2018-10-24 03:38:46
初始化coord:
coord = Point2D.Float();
或
coord = Point2D.Double();
取决于x,y数据类型。
https://stackoverflow.com/questions/23179139
复制相似问题