NullPointerException (Java) [duplicate]

自古美人都是妖i 提交于 2019-11-28 02:11:11

You shouldn't give your instance field the same name as the class because that causes Variable shadowing - Wikipedia says (in part) variable shadowing occurs when a variable declared within a certain scope (decision block, method, or inner class) has the same name as a variable declared in an outer scope. At the level of identifiers (names, rather than variables), this is known as name masking. And you could define the reference at declaration like

private SongDatabase songDatabase = new SongDatabase();

Then something like

private void run() {
    switch (userInput) {
    case 1: 
        songDatabase.addNewSong();
        break;
    case 2:
        songDatabase.removeSong();
        break;
    case 3:
        songDatabase.sortSongs();
        break;
    default:
        System.out.println("Please enter a valid number.");
        break;
    }
}

It is null because it is never being instantiated. Create SongDatabase in your main method to get around your original problem:

public static void main(String[] args) {
    Interface intFace = new Interface();
    SongDatabase = new SongDatabase();
    SongDatabase.addNewSong();
    intFace.run();
}

To avoid confusion name your variables starting with a lower case letter.

 private SongDatabase songDatabase;

This way it is clear that when you write songDatabase you mean the instance and when you write SongDatabase you are referring to the class.

You need to instantiate the instance of the class before you can use it. It seems that you are already aware of this from the question but it is just a matter of where to do it. For a quick fix you can instantiate it at the point where you declare the variable. Later you can look into a better design. Therefore:

 private SongDatabase songDatabase = new SongDatabase();

You were getting the NullPointerException as the class variable for SongDatabase have never been instantiated. To initialize the class variables we can use constructor, please see the code details below. Also to avoid the naming confusion we can use this keyword to have better code readability.

import java.util.Scanner;

public class Interface
{
    private Scanner console;
    private SongDatabase songDatabase;

    public Interface()
    {
        this.songDatabase = new SongDatabase(); // Initialize the SongDatabase class and .
        this.console = new Scanner(System.in); // Initialize the console reference with scanner class.
    }

    public static void main(String[] args)
    {
        Interface intFace = new Interface();
        intFace.run();
    }

    private void run()
    {
        System.out.println("1. Add Song");
        System.out.println("2. Remove Song");
        System.out.println("3. Sort Song");
        System.out.print("Please Enter Choice: ");

        int userInput = console.nextInt(); // Get the data from user input

        switch (userInput)
        {
            case 1:
                this.songDatabase.addNewSong();
                break;

            case 2:
                this.songDatabase.removeSong();
                break;

            case 3:
                this.songDatabase.sortSongs();
                break;

            default:
                System.out.println("Please enter a valid number.");
                break;
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!