2011-03-27 4 views
0

내 문제는 바로 그것입니다. 내가 문자열을 특정 크기로 선언하려고했는데, 모든 것이 정렬되었지만 작동하는 유일한 방법은 내가 사용하는 메인 클래스 내부의 main 메소드에 문자열을 넣는 것이다. 내가 생각할 수있는 모든 것을 시도했기 때문에 그것은 실망 스럽다. 다른 클래스에서 가져온 요소가 무효화되는 이유는 무엇입니까? 나는 move 클래스에 main 메소드를 추가하고 그것을 시도해 보려고 움직였다. 그러나 아무 소용이 없었다. 나는 문제가 끝나는 곳까지하고 시작되는 곳에서, 주요 하나가 무엇을 그null 포인터 다른 클래스에서 문자열 [예외] 호출

import java.util.Arrays; 
public class movesClass { 
    public String[] getMoves() { 
     return moves; 
    } 
    String[] moves; 
    public static String[] useMove(String[] moves) { 
     moves[0] = "punch"; 
     moves[1] = "kick"; 
     moves[2] = "defend"; 
     moves[3] = "run"; 
     moves[4] = "inventory"; 
     moves[5] = "rickroll"; 
     moves[6] = "heal"; 
     Arrays.sort(moves); 
     return moves; 
    } 
} 
And the body of the main one: 

    import java.io.*; 
import java.util.*; 
public class practiceBattle { 
    public static void main(String[] args) { 

     Scanner scan = new Scanner(System.in); 
     Random rand = new Random(); 
     inventoryClass quant = new inventoryClass(); 
     movesClass mov = new movesClass(); 
     String[] moves = mov.getMoves(); 

     int hp = 0; 
     int playerHp = 200; 

     String input = "default"; 

     int damage = 0; 

     int hitDif = 50; //default 
     int kickChance1 = 0; //default -- goes into hitChance 
     int kickChance = kickChance1 - hitDif; //chance to hit 
     int hitChance1 = 0; 
     int hitChance = hitChance1 - hitDif; 

     int runDif = 50; //default 
     int runChance1 = 0; //default -- goes into runChance 
     int runChance = runChance1 - runDif; //chance to run 

     int enemyMinAttack = 0; 
     int enemyAttack = 0; 

     int index = 0; 

     int[] levelArray = {1, 2, 3}; 
     String[] monsterArray = {"GNOLL", "TROLL", "DRAGON"}; 
     String monster = monsterArray[rand.nextInt(monsterArray.length)]; 
     int level = rand.nextInt(levelArray.length) + 1; 
     if(level == 1) 
     { 
      System.out.println("YOUR OPPONENT IS A BABY " + monster + "!"); 
     } 
     else 
     { 
      System.out.println("YOUR OPPONENT IS A LEVEL " + level + " " + monster + "!"); 
     } 
     if(level == 1) 
     { 
      hp = rand.nextInt(20) + 41; 
      enemyAttack = rand.nextInt(5) + 1; 
     } 
     else if(level == 2) 
     { 
      hp = rand.nextInt(20) + 91; 
      enemyAttack = rand.nextInt(6) + 5; 
     } 
     else if(level == 3) 
     { 
      hp = rand.nextInt(20) + 141; 
      enemyAttack = rand.nextInt(7) + 10; 
     } 
     enemyMinAttack = rand.nextInt(enemyAttack) + 1; 

     System.out.println("YOUR OPPONENT'S HP IS: " + hp + "\n"); 
     int permEnemyAttack = enemyAttack; 
     int permEnemyMinAttack = enemyMinAttack; 







    ext: 
     while(hp > 0) 
     { 
      enemyAttack = permEnemyAttack; 
      enemyMinAttack = permEnemyMinAttack; 

      do 
      { 
       if(playerHp == 0) 
       { 
        System.out.println("YOU HAVE DIED. GAME OVER!\n\n\n"); 
        break ext; 
       } 
       else 
       { 
        System.out.println("Choose an action:\nkick\npunch\nrun\ndefend\n"); 
        input = scan.nextLine(); 


        index = Arrays.binarySearch(moves, input); 
        System.out.println(index); 
        if(index < 0) 
        { 
         System.out.println("\n\n\nINVALID INPUT -- TRY AGAIN\n\n\n"); 
        } 
       } 

      } while(index < 0); 

: 다음은 첫 번째 프로그램이다. 모든 inpu는 크게 감사하겠습니다. 고맙습니다!

+0

스택 추적을 제공해 주시겠습니까? – Antonio

답변

3

moves 필드는 movesClass 안에 초기화하지 마십시오. 참조 필드의 기본값 인 null입니다. 당신이 그것을 초기화하기 싶다면 할 필요가 :

String[] moves = new String[7]; 

하지만 그렇게하더라도 배열의 요소는 여전히 될 것 null을 당신은 배열의 실제 문자열을 넣어해야합니다. useMoves()이 만들어진 것으로 보이지만 절대로 호출되지 않습니다. 이 방법은 어쨌든 이상합니다. 이동을 채우고 정렬하는 인수로 배열을 가졌지 만 동일한 배열을 반환하면 이미 매개 변수가 있습니다. Java는 한 메소드에서 다른 메소드로 전달하거나 메소드에서 리턴 할 때 배열을 복사하지 않으므로 매개 변수로 얻은 배열을 수정합니다. moves 필드를 항상 같은 동작으로 초기화하려면 클래스 생성자에서이 작업을 수행하는 것이 좋습니다. 정적 메서드는 클래스의 인스턴스 필드에 액세스 할 수 없습니다.

+0

... 그리고 결코 호출되지 않는'useMoves' 메쏘드에있는 초기화를 생성자에 넣으십시오. –

관련 문제