2011-05-01 5 views
1

이 줄은 '현재 컨텍스트에서 알 수없는'컴파일러 오류를 생성합니다. 왜?java : 현재 컨텍스트에서 변수를 알 수 없습니까?

if (inputMatrix[newPosition.i][newPosition.j]=='*'){ 
     // variable not known in current context, why? 
     } 

방법 선언 :

static Point moveForward(Point oldPosition, int matrixSize, char orientation, char [][] inputMatrix){ 

     // add possible new Position 
     Point newPosition; 

     //first return oldPosition border positions in which the robot shouldn't move 

     if ((orientation=='O')&&(oldPosition.j==0)) 
       return oldPosition; 

     if ((orientation=='E')&&(oldPosition.j==(matrixSize-1))) 
       return oldPosition; 

     if ((orientation=='N')&&(oldPosition.i==0)) 
       return oldPosition; 

     if ((orientation=='S')&&(oldPosition.i==(matrixSize-1))) 
       return oldPosition; 


     if ((orientation=='O')) 
      newPosition = new Point(oldPosition.i, oldPosition.j-1); 
     if ((orientation=='E')) 
      newPosition = new Point(oldPosition.i, oldPosition.j+1); 
     if ((orientation=='S')) 
      newPosition = new Point(oldPosition.i-1, oldPosition.j); 
     if ((orientation=='N')) 
      newPosition = new Point(oldPosition.i+1, oldPosition.j); 


     //then return oldPosition for positions in which the robot is blocked by * 
     if (inputMatrix[newPosition.i][newPosition.j]=='*'){ 
     // variable not known in current context, why? 
     } 




     return null; 


    } 

답변

6

newPosition가 초기화 된 보장되지 않기 때문에. 파일 시작 부분에

Point newPosition = null; 

을 사용하십시오. 최소한 변수 값을 null로 초기화합니다.

관련 문제