2017-02-04 3 views
0

Scoreboardクラスの関数がオブジェクトゲームエントリの値を表示できません。Javaのクラスオブジェクト値を印刷できません

値はadd関数によって取得され、オブジェクト配列にも割り当てられます。

値が変更されている場合、エラーが表示されます。スコアボードクラスのDisplayブロック - スレッド "メイン" Scoreboard.displayでjava.lang.NullPointerExceptionが

(Scoreboard.java:73)

73行に

例外。

あなたはちょうど彼らが何をしたいのかをユーザーに尋ねる前Scoreboardの新しいインスタンスを作成しますので、それがある表示ブロックから

import java.util.*; 
class Game_entry 
{ 
    String name; 
    int score=0; 
    Game_entry(String n,int s){ 
     name=n; 
     score=s; 
     System.out.println(name); 
     System.out.println(s); 

    } 
    String getname() 
    { 
     return name; 
    } 
    int getscore() 
    { 
     return score; 
    } 
    void display() 
    { 
     System.out.println("name:"+name+" score:"+score); 
    } 

} 


public class Scoreboard { 
    static int num_entries; 

    Game_entry[] board; 
    Scoreboard(int capacity) 
    { 
     board=new Game_entry[capacity]; 
    } 
    void add(Game_entry e) 
    { 
     int newscore=e.getscore(); 
     if(num_entries<board.length || newscore>board[num_entries-1].getscore()) 
     { 
      if(num_entries<board.length){ 
       ++num_entries; 
       System.out.println("hello"); 
       System.out.println(num_entries); 

      } 
      int j=num_entries-1; 
      while(j>0 && board[j-1].getscore()<newscore) 
      { 
       System.out.println("hello"); 

       board[j]=board[j-1]; 
       j--; 
      } 

      board[j]=e; 
      System.out.println(board[0].getname()); 
      System.out.println(board[0].getscore()); 
     } 
     System.out.println(num_entries); 
    } 

    void display() 
    { 
     System.out.println(num_entries); 
     System.out.println("Total no. of enteries"+num_entries); 
     if(num_entries==0) 
     { 
      System.out.println("no entries"); 
     } 
     else 
     { 

      for(int i=0;i<num_entries;i++) 
      System.out.println(board[i].getname());    
      System.out.println(board[i].getscore()); 
     } 
    } 

    public static void main(String[] args) { 
     // TODO code application logic here 
     int c; 
     boolean a=true; 
     Scanner sc=new Scanner(System.in); 
     while(a==true) 
     { 
      System.out.println("\n\n\n MENU "); 
      System.out.println("1.Insertion"); 
      System.out.println("2.Deletion"); 
      System.out.println("3.Display"); 
      System.out.println("4.Exit"); 
      System.out.println("Enter your choice"); 
      c=sc.nextInt(); 
      Scoreboard sco=new Scoreboard(100); 

      switch(c) 
      { 
      case 1: 
       String m; 
       int n; 
       System.out.println("Enter name"); 
       m=sc.next(); 
       System.out.println("enter score"); 
       n=sc.nextInt(); 
       Game_entry ge=new Game_entry(m,n); 
       sco.add(ge); 

       break; 
      case 2: 
       // int x; 
       // System.out.println("enter the index from where element must b deleted"); 
       // x=sc.nextInt(); 
       // sco.remove(x); 
       // break; 
      case 3: 
       sco.display(); 
       break; 
      case 4: 
       a=false; 
       break; 
      default: 
       System.out.println("enter correct choice"); 

      } 
     } 
    } 

} 
+3

[NullPointerExceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix -it) –

+0

あなたの 'Scoreboard.java'ファイルで#** 73 **行を確認してください。 –

+0

Scoreboardクラスの表示ブロックelse部分 –

答えて

0

を値を印刷することができません。あなたのユーザーは新しいスコアを追加してから、もう一度それを放棄します。

num_entriesフィールドがstaticでない場合、これは問題ではありません。 staticは、Scoreboardのすべてのインスタンスで1つだけが共有されていることを意味します。したがって、毎回新しい配列を作成している間は、前の配列の要素の数を覚えています。

関連する問題