2011-03-27 19 views
0

我的問題就是這樣。我試圖將字符串聲明爲特定的大小,我將其排序和所有內容,但唯一有用的是將字符串放在我使用的主類中的main方法中。這很令人沮喪,因爲我嘗試了所有我能想到的東西。爲什麼當我從其他課程導入元素時,這些元素會變得無效?我在移動類中添加了一個主要方法,並將它移動到那裏來嘗試,但無濟於事。 這裏的第一個程序:空指針異常調用另一個類的字符串[]

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 2011-03-27 21:31:54

回答

3

您從不初始化movesClass中的moves字段。它是空的,參考字段的默認值。如果你想並初始化它,你需要做的:

String[] moves = new String[7]; 

但是,即使你做到這一點,該數組的元素仍然會null,你必須把實際字符串在數組中。看起來你的useMoves()是爲了這個,但它永遠不會被調用。無論如何,這種方法有點奇怪。你有一個數組作爲參數,你填充這些移動然後對它進行排序,但是然後你返回相同的數組,你已經得到了這個參數。 Java在將數組從一種方法傳遞到另一種方法或從方法返回時不會複製數組,因此您可以修改作爲參數獲取的數組。如果你總是想用相同的動作初始化moves字段,那麼最好在類的構造函數中這樣做。靜態方法無法訪問類實例的字段。

+0

...然後將現在在從未調用過的'useMoves'方法中的初始化添加到構造函數中。 – 2011-03-27 21:54:48