如何解决来自Point2D.setLocation()的Java NullPointerException异常?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (106)

当我尝试在Point2D对象上使用setLocation()方法时,我不断收到NullPointerException。我的代码中有6个或7个Point2D对象,它只会抛出此异常。这是代码片段:

import 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();
提问于
用户回答回答于

初始化协调:

coord = Point2D.Float();

要么

coord = Point2D.Double();

取决于x,y数据类型

用户回答回答于

加上:

coord = new Point2D();// you may need to pass some parameters to the constructor of the Point2D according to it's definition

扫码关注云+社区

领取腾讯云代金券