gpt4 book ai didi

java - MySQL更新查询产生空指针异常

转载 作者:行者123 更新时间:2023-11-29 07:59:18 24 4
gpt4 key购买 nike

我有一个 MySQL 更新查询,当用户键入 /pardon 命令时运行,但我的 MySQL PreparedStatement try/catch 正在创建一个 NullPointerException.

这是更新的堆栈跟踪:

[19:28:19] [Server thread/WARN]: java.lang.NullPointerException
[19:28:19] [Server thread/WARN]: at net.coded.playerdata.cmds.Pardon.onCommand(Pardon.java:50)
[19:28:19] [Server thread/WARN]: at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
[19:28:19] [Server thread/WARN]: at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
[19:28:19] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:703)
[19:28:19] [Server thread/WARN]: at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchServerCommand(CraftServer.java:690)
[19:28:19] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.DedicatedServer.aB(DedicatedServer.java:296)
[19:28:19] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:261)
[19:28:19] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558)
[19:28:19] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469)
[19:28:19] [Server thread/WARN]: at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)

这是类(class):

public class Pardon implements CommandExecutor {

@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {

if (cmd.getName().equalsIgnoreCase("pardon")) {

if (!sender.hasPermission("bukkit.command.unban.player") || (!sender.isOp())) {
MessageManager.getInstance().severe(sender, ChatColor.RED + "You do not have permission to do that.");
return true;
}

if (args.length == 0) {
MessageManager.getInstance().severe(sender, "You must specify a player name!");
return true;
}

if (args.length >= 1) {
//Unban the player when they are offline
Bukkit.getOfflinePlayer(args[0]).setBanned(false);
Player toUnban = Bukkit.getPlayer(args[0]);

// Update database
try {
Class.forName(PlayerData.driver).newInstance();
Connection conn = DriverManager.getConnection(PlayerData.url + PlayerData.dbName, PlayerData.userName, PlayerData.password);

String query = "UPDATE playerdata SET banned = ?, ban_reason = ? WHERE uuid = ?";

// create the MySQL insert preparedstatement
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt(1, 0);
preparedStmt.setString(2, null);
preparedStmt.setString(3, toUnban.getUniqueId().toString());

// execute the preparedstatement
preparedStmt.executeUpdate();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}

Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Player " + ChatColor.WHITE + args[0] + ChatColor.GREEN + " has been unbanned.");
return true;
}
}
return true;
}
}

以下是导致问题的 if 语句:

if (args.length >= 1) {
//Unban the player when they are offline
Bukkit.getOfflinePlayer(args[0]).setBanned(false);
Player toUnban = Bukkit.getPlayer(args[0]);

// Update database
try {
Class.forName(PlayerData.driver).newInstance();
Connection conn = DriverManager.getConnection(PlayerData.url + PlayerData.dbName, PlayerData.userName, PlayerData.password);

String query = "UPDATE playerdata SET banned = ?, ban_reason = ? WHERE uuid = ?";

// create the MySQL insert preparedstatement
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt(1, 0);
preparedStmt.setString(2, null);
preparedStmt.setString(3, toUnban.getUniqueId().toString());

// execute the preparedstatement
preparedStmt.executeUpdate();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}

Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Player " + ChatColor.WHITE + args[0] + ChatColor.GREEN + " has been unbanned.");
return true;
}

我尝试为语句/查询创建一个单独的方法,添加一个finally子句来关闭连接,而不是在try中关闭它们。我还尝试在 try 之前声明 ConnectionPreparedStatement 和查询字符串。我相当确定这大部分是有效的代码,因为我使用 MySQL 和 Java 文档来编写此代码。我一定错过了一些东西。非常感谢任何帮助!

最佳答案

it is preparedStmt.setString(3, toUnban.getUniqueId().toString());

根据您的评论,toUnbantoUnban.getUniqueId() 似乎是 null 值。同样添加调试语句并再次运行检查:

preparedStmt.setString(2, null);
System.out.println("toUnban null? " + (toUnban==null));
System.out.println("toUnban.getUniqueId() null? " + (toUnban.getUniqueId()==null));
preparedStmt.setString(3, toUnban.getUniqueId().toString());

如果任一调试输出状态为 true,请调查相应变量为何为 null

关于java - MySQL更新查询产生空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24317713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com