0

Display function in Scoreboard class in unable to print the value of the object game entry.

Value is fetched by the add function and also begin assign to object array.

When value is been it show the error.

Exception in thread "main" java.lang.NullPointerException

at Scoreboard.display(Scoreboard.java:73)

73 line - Display block in Scoreboard class.

Unable to print the value from display block

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");
                
            }
        }
    }
    
}
Community
  • 1
  • 1

1 Answers1

0

It is because you create a new instance of Scoreboard just before asking the user what they want to do. Your user adds a new score, then you throw it away again.

This would not be so much of a problem if your num_entries field was not static. static means there is only one of them shared between all instances of Scoreboard. So while you're creating a fresh array each time, you are remembering the number of elements that were in the previous one.

Stik
  • 519
  • 4
  • 17