Java 中错误 Attempt to Invoke Virtual Method on a Null Object Reference
本篇文章介绍如何解决 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。 毕竟还没有初始化。 通过初始化,我们可以解决这个问题。
查看解决方案:
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));
}
}
因此,每当在这样的系统上工作时,您必须确保方法没有调用任何空值; 否则,会抛出此异常。
相关文章
Java 错误 Java.Net.SocketException: Network Is Unreachable
发布时间:2023/07/16 浏览次数:102 分类:Java
-
今天我们就来讨论一下Java编程时出现java.net.SocketException: Network is unreachable异常的可能原因及解决方法。Java中出现java.net.SocketException: Network is unreachable的可能原因及解决方案
Java 错误 Java.Net.ConnectException: Connection Timed Out
发布时间:2023/07/16 浏览次数:101 分类:Java
-
本篇文章将重点介绍如何使用此包进行基本的网络调用以及可能面临和解决的错误。在 Java 中使用 java.net 进行网络调用 进行网络调用是 Java 开发人员每天面临的最重要的事情之一。
Java 错误 Java.Security.InvalidKeyException: Illegal Key Size
发布时间:2023/07/15 浏览次数:98 分类:Java
-
本篇文章介绍包含 java.security.InvalidKeyException: Illegal key size 的 Java 代码。 然后,我们将了解其可能的原因。最后,它通过消除指定的错误来引导我们找到解决方案。
Java 错误 Java.SQL.SQLException: Access Denied for User Root@Localhost
发布时间:2023/07/15 浏览次数:72 分类:Java
-
本篇文章介绍如何解决 Java 中的 java.sql.SQLException: Access Denied for user 'root'@'localhost' 错误。修复 Java 中的 java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
Java 错误 Gateway Process Exited Before Sending Its Port Number
发布时间:2023/07/15 浏览次数:104 分类: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 中 Java.Net.BindException: Address Already in Use: Bind 错误
发布时间:2023/07/15 浏览次数:77 分类:Java
-
本篇文章介绍了 Java 中的 java.net.BindException:Address already in use: Bind 错误。修复Java 中的 java.net.BindException:Address already in use: Bind
Java 中错误 Unsupported Major Minor Version
发布时间:2023/07/14 浏览次数:68 分类:Java
-
出现 Unsupported Major.minor version 错误或 Java.lang.UnsupportedClassVersionError 的原因是运行时 JDK 较低,编译时 JDK 较高。 本篇文章介绍如何解决Java中不支持的major.minor版本。
Java 错误 Error:Java: Javactask: Source Release 1.8 Requires Target Release 1.8
发布时间:2023/07/14 浏览次数:201 分类:Java
-
在使用IntelliJ for Java时,无法编译Java程序是一个常见的问题。 本教程提供了此错误的解决方案。Error:Java: Javactask: Source Release 1.8 Requires Target Release 1.8 错误
Java 中 Could Not Find Java SE Runtime Environment 错误
发布时间:2023/07/14 浏览次数:149 分类:Java
-
当安装了运行时环境时,即 Java 找不到主 java.dll 文件时,可能会出现“Could not find Java SE Runtime Environment”错误。 本篇文章介绍如何解决 Java 中的“Could not find Java SE Runtime Environment”错误。