首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >来自Point2D.setLocation()的Java NullPointerException

来自Point2D.setLocation()的Java NullPointerException
EN

Stack Overflow用户
提问于 2014-04-20 14:18:52
回答 2查看 346关注 0票数 0

当我尝试在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();

有什么想法吗?谢谢!

EN

回答 2

Stack Overflow用户

发布于 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

在尝试使用它之前。(您尝试在初始化此对象之前访问它)

票数 4
EN

Stack Overflow用户

发布于 2018-10-24 03:38:46

初始化coord:

coord = Point2D.Float();

coord = Point2D.Double();

取决于x,y数据类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23179139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档