2014-04-20 4 views
0

Point2D 객체에서 setLocation() 메서드를 사용하려고하면 NullPointerException이 계속 발생합니다. 내 코드에서 6 또는 7 Point2D 개체가 있고이 경우에만 예외가 throw됩니다. 코드 스 니펫은 다음과 같습니다.Point2D.setLocation()의 Java NullPointerException

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(); 

어떤 아이디어가? 감사!

+0

MainGUI에서 객체가 어떻게 생성됩니까? MainGUI를 생성하기 전에 호출해야하며 생성자가 항상 먼저 호출되므로 생성 방법을 어떻게 예상 할 수 있습니까? – CodeCamper

답변

4

이 추가 :

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

그것을 사용하기 전에. (초기화하기 전에이 객체에 액세스하려고 시도합니다.)