2017-06-05 5 views
0

java.lang.StackOverflowError: nullというエラーが表示されています。私のプログラムは、宝くじのようなもので、乱数が選択され、数字に基づいて、あなたのアイテムは配列から選択されます。今、これは便利で素敵ですが、在庫を在庫に入れようとしたときに動作します。私はそのエラーを取得します。私はかなり間違ったクラスを設定していますが、別のクラスからデータを受け取り、同じクラスにデータを戻す別のクラスに配列を作成する方法がわかりません。チャットをするだけで、私のコードはこれまでのように見えます。あなたはinventorymain()マルチクラスプロジェクトの空の配列は、呼び出し時にデータを受け取った後にnullを返します

public class inventory { 

    private int inventorymain; 

    public String[] inventorymain() { 
     String[] inventorymain; 
     return inventorymain(); // recursive call here without escape condition 
    } 

} 

を呼び出す無限再帰ループに入っている

public class inventory { 
    private int inventorymain; 
    public String[] inventorymain() { 
     String[] inventorymain; 
     return inventorymain(); 
    } 
} 


    import java.util.Scanner; 
    import java.util.Random; 

    public class glue { 

     public static void main(String [] args) { 
      inventory inv = new inventory(); 
      allskins a = new allskins(); 
      Scanner s = new Scanner(System.in); 
      int selection = 0; 
      int invcount = -1; 
      Random rand = new Random(); 
      do { 
      System.out.println(d.menue()); 
      selection = s.nextInt(); 
      if (selection == 1) { 
       invcount++; 
       int random = rand.nextInt(208); 
       System.out.println("You opend a: "); 
       System.out.println(a.allskins()[random]); 
       System.out.println("Your item has been put in your inventory, select inventroy from the menue to view all items."); 
       System.out.println(""); 
       inv.inventorymain()[invcount] = (a.allskins()[random]); 
       } 
      }while (selection != 6); 
     } 
} 

+0

inventarymain()は同じメソッドをもう一度呼び出すだけです。代わりに 'new String []' –

+0

を返すべきです。配列のサイズは、プレーヤーが受け取るアイテムの数に依存します(ランダムです) –

答えて

1

ありがとうそれは維持する方が良いでしょう(これは私のコードの切り取られた部分であるとして、余分な変数を気にいけないしてください)そのような場合を避けるために、メソッド名と変数名が異なります。

関連する問題