来自Point2D.setLocation()的Java NullPointerException
当我尝试在Point2D对象上使用setLocation()方法时,我不断得到一个NullPointerException。我的代码中有6或7个Point2D对象,它只会抛出这个对象的异常。下面是代码片段: 导入java.awt.geom.*来自Point2D.setLocation()的Java NullPointerException,java,swing,nullpointerexception,point,Java,Swing,Nullpointerexception,Point,当我尝试在Point2D对象上使用setLocation()方法时,我不断得到一个NullPointerException。我的代码中有6或7个Point2D对象,它只会抛出这个对象的异常。下面是代码片段: 导入java.awt.geom.* public class Box { private Point2D coord; private int height; private int width; public Box() { co
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();
有什么想法吗?谢谢 添加以下内容:
coord = new Point2D();// you may need to pass some parameters to the constructor of the Point2D according to it's definition
在尝试使用它之前。(您尝试在初始化之前访问此对象)添加以下内容:
coord = new Point2D();// you may need to pass some parameters to the constructor of the Point2D according to it's definition
在尝试使用它之前。(您尝试在初始化此对象之前访问它)初始化坐标:
coord = Point2D.Float();
或
coord = Point2D.Double();
取决于x,y数据类型。初始化坐标:
coord = Point2D.Float();
或
coord = Point2D.Double();
取决于x,y数据类型。如何在MainGUI中创建对象?您需要在创建MainGUI之前调用它,并且始终会首先调用构造函数,所以您希望如何创建它?您的对象是如何在MainGUI中创建的?您需要在创建MainGUI之前调用它,并且总是首先调用构造函数,那么您希望如何创建它呢?