NullPointerException(Java) [英] NullPointerException (Java)

查看:113
本文介绍了NullPointerException(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试查看有关此问题的帖子,但我的代码中仍然遇到此错误的问题。所以在第四行中,我创建了一个名为 SongDatabase 的实例变量来访问 SongDatabase 类。但是当我到达 SongDatabase.addNewSong(); case 1 时,我得到了一个java。 lang.NullPointerException:null error。

I've tried looking at posts on this issue but am still having some trouble with this error in my code. So in the fourth line, I create an instance variable called SongDatabase to access the SongDatabase class. But when I get down to the line, SongDatabase.addNewSong(); under case 1, I get a java.lang.NullPointerException: null error.

接口类:

public class Interface 
{
    Scanner console = new Scanner(System.in);
    private SongDatabase SongDatabase;

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

    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;

    }
}

SongDatabase类:

SongDatabase class:

public class SongDatabase {
    Scanner console = new Scanner(System.in);  
    private Song song1, song2, song3, song4;

public void addNewSong() {        
        if (song1 == null) {
            song1 = getFromUser();
        }
        else if (song2 == null) {
            song2 = getFromUser();
        }    
        else if (song3 == null) {
            song3 = getFromUser();
        }
        else if (song4 == null) {
            song4 = getFromUser();
        }
        else {
        System.out.println("The database is currently full. Please delete a song before adding a new one.");
       }    
    }

我已经通过调试器,我知道实例变量,SongDatabase = null,这可能导致错误?我之前有一行

I've stepped through the debugger and I know that the instance variable, SongDatabase = null, which is probably causing the error? I previously had a line

SongDatabase SongDatabase = new SongDatabase();
SongDatabase.addNewSong();

相反,但我意识到这是每次创建一个新的SongDatabase对象并擦除我在那里存储的内容所以我不得不改变它。我真的很感激解决方案,因为我不知道,谢谢!

instead, but I realised this was creating a new SongDatabase object everytime and wiping what I had stored in there so I had to change it. I'd really appreciate a solution because I have no clue, thanks!

推荐答案

你不应该给你的实例字段一样将该名称命名为类,因为这会导致变量阴影 - 维基百科说(部分) 变量阴影在特定范围内声明的变量(决策块,方法或内部类)与在外部范围内声明的变量具有相同的名称时发生。在标识符(名称而不是变量)级别,这称为名称屏蔽 。您可以在声明中定义引用,如

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();

然后像

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;
    }
}

这篇关于NullPointerException(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆