空指针异常 (Java) [英] NullPointerException (Java)

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

问题描述

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

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

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

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