java 中错误 attempt to invoke virtual method on a null object reference-ag捕鱼王app官网

java 中错误 attempt to invoke virtual method on a null object reference

作者:迹忆客 最近更新:2023/07/16 浏览次数:

本篇文章介绍如何解决 java 中的 attempt to invoke virtual method on a null object reference 错误。


java 中 attempt to invoke virtual method on a null object reference 错误

在处理 android 应用程序时,可能会出现错误“attempt to invoke virtual method on a null object reference”,这是一种 nullpointerexception 类型。 每当我们尝试使用 null 对象调用方法时,它都会抛出此错误。

这是一个基于android平台的系统,当尝试在空对象引用描述上调用虚拟方法时,会抛出nullpointerexception。

此示例具有三个不同的 java 类。

main_player.java:

package jiyik;
import javax.naming.context;
public class main_player {
    private context appcontext;
    private sharedpreferences sharedpreferencessettingsu;
    private sharedpreferences.editor preferenceseditoru;
    private static final int preference_mode = 0;
    private static final string unique_preferences_file = "demoapp";
    public player(context appcontext, string player_name) {
        this.appcontext = appcontext;
        save_name(player_name);
    }
    public void save_name(string nvalue) {
        sharedpreferencessettingsu = appcontext.getsharedpreferences(unique_preferences_file, preference_mode);
        preferenceseditoru = sharedpreferencessettingsu.edit();
        preferenceseditoru.putstring("keyname", nvalue);
        preferenceseditoru.commit();
    }
    public string get_name(context democontext) {
        sharedpreferencessettingsu = democontext.getsharedpreferences(unique_preferences_file, preference_mode);
        string playername = sharedpreferencessettingsu.getstring("keyname", "anonymous");
        return playername;
    }
}

player_name.java:

package jiyik;
import javax.naming.context;
public class player_name extends activity {
private edittext player_name;
private player m_player;
public static context appcontext;
@override
protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.player_name);
    appcontext = this;
    player_name = (edittext) findviewbyid (r.id.etname);
}
public void onc_confirm(view btnclick) {
    m_player = new player(appcontext, string.valueof(player_name.gettext()));
    //m_player.savename();
    intent intent = new intent(player_name.this, game_play.class);
    startactivity(intent);
}
public void onc_testshpref(view btnclick) {
    intent intent = new intent(player_name.this, game_play.class);
    startactivity(intent);
}

game_play.java:

public class game_play extends activity {
    private textview welcome_player;
    private listview games_created;
    private player m_player;
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.play_game);
        welcome_player = (textview) findviewbyid (r.id.tvplayer_name);
        welcome_player.settext("welcome back player, "   string.valueof(m_player.getname(this))   " !");
        games_created = (listview) findviewbyid (r.id.listcreatedgames);
        games_created.setemptyview(findviewbyid (r.id.tvnogames));
    }
}

我们在android平台上运行上述系统,会抛出java.lang.nullpointerexception: attempt to invoke virtual method导致的错误。

caused by: java.lang.nullpointerexception: attempt to invoke virtual method 'java.lang.string plp.cs4b.thesis.drawitapp.main_player.getname()' on a null object reference
    at plp.cs4b.thesis.drawitapp.game_play.oncreate(game_play.java:20)
    at android.app.activity.performcreate(activity.java:5933)
    at android.app.instrumentation.callactivityoncreate(instrumentation.java:1105)
    at android.app.activitythread.performlaunchactivity(activitythread.java:2251)
    ... 10 more

该错误发生在 game_play.java 类中的以下代码行中。

welcome_player.settext("welcome back player, "   string.valueof(m_player.getname(this))   " !");

出现此错误是因为 m_player 为 null。 毕竟还没有初始化。 通过初始化,我们可以解决这个问题。

查看ag捕鱼王app官网的解决方案:

game_play.java:

package jiyik;
public class game_play extends activity {
    private textview welcome_player;
    private listview games_created;
    //initialize the m_player
    private player m_player = new main_player(appcontext,"");
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.play_game);
        welcome_player = (textview) findviewbyid (r.id.tvplayer_name);
        welcome_player.settext("welcome back player, "   string.valueof(m_player.getname(this))   " !");
        games_created = (listview) findviewbyid (r.id.listcreatedgames);
        games_created.setemptyview(findviewbyid (r.id.tvnogames));
    }
}

因此,每当在这样的系统上工作时,您必须确保方法没有调用任何空值; 否则,会抛出此异常。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

java 错误 java.net.socketexception: network is unreachable

发布时间:2023/07/16 浏览次数:963 分类:java

今天我们就来讨论一下java编程时出现java.net.socketexception: network is unreachable异常的可能原因及解决方法。java中出现java.net.socketexception: network is unreachable的可能原因及ag捕鱼王app官网的解决方案

java 错误 java.net.connectexception: connection timed out

发布时间:2023/07/16 浏览次数:235 分类:java

本篇文章将重点介绍如何使用此包进行基本的网络调用以及可能面临和解决的错误。在 java 中使用 java.net 进行网络调用 进行网络调用是 java 开发人员每天面临的最重要的事情之一。

java 错误 java.security.invalidkeyexception: illegal key size

发布时间:2023/07/15 浏览次数:644 分类:java

本篇文章介绍包含 java.security.invalidkeyexception: illegal key size 的 java 代码。 然后,我们将了解其可能的原因。最后,它通过消除指定的错误来引导我们找到ag捕鱼王app官网的解决方案。

发布时间:2023/07/15 浏览次数:165 分类:java

本篇文章介绍如何解决 java 中的 java.sql.sqlexception: access denied for user 'root'@'localhost' 错误。修复 java 中的 java.sql.sqlexception: access denied for user 'root'@'localhost' (using password: yes)

发布时间:2023/07/15 浏览次数:885 分类:java

本篇文章介绍了 java 中 java gateway process exited before sending the driver its port number 错误 java gateway process exited before sending the driver its port number 错误

java 中错误 unsupported major minor version

发布时间:2023/07/14 浏览次数:133 分类:java

出现 unsupported major.minor version 错误或 java.lang.unsupportedclassversionerror 的原因是运行时 jdk 较低,编译时 jdk 较高。 本篇文章介绍如何解决java中不支持的major.minor版本。

发布时间:2023/07/14 浏览次数:868 分类:java

当安装了运行时环境时,即 java 找不到主 java.dll 文件时,可能会出现“could not find java se runtime environment”错误。 本篇文章介绍如何解决 java 中的“could not find java se runtime environment”错误。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图