gpt4 book ai didi

java - 启动我的 Bukkit 插件时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 11:30:25 25 4
gpt4 key购买 nike

当我的插件被启用时,我得到了以下 java.lang.NullPointerException。我真的看不出问题所在。

错误:

java.lang.NullPointerException
at tk.mypalsgaming.TARDIScraft.TARDIScraft.onDisable(TARDIScraft.java:31)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:219)
at org.bukkit.plugin.java.JavaPluginLoader.disablePlugin(JavaPluginLoader.java:481)
at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginManager.java:401)
at org.bukkit.plugin.SimplePluginManager.disablePlugins(SimplePluginManager.java:394)
at org.bukkit.craftbukkit.v1_6_R2.CraftServer.disablePlugins(CraftServer.java:281)
at net.minecraft.server.v1_6_R2.MinecraftServer.stop(MinecraftServer.java:349)
at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:445)
at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)

插件.yml:

name: TARDIScraft
main: tk.mypalsgaming.TARDIScraft.TARDIScraft
version: 0.0.1
depend: [Vault]
commands:
tardis:
description: TARDIS Command Block and Admin Command
usage: /<command> <TARDIS command> [parameters]
permission: TARDIScraft.admin
permission-message: You are not a TARDIS Admin, so you do not have access to this command.

TARDIScraft.java:

package tk.mypalsgaming.TARDIScraft;

import java.util.logging.Logger;

import net.milkbowl.vault.permission.Permission;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

public class TARDIScraft extends JavaPlugin {

Logger console = getLogger();

@Override
public void onEnable() {

console.info("Enabling the TARDIS plugin...");

// TODO: onEnable code

}

@Override
public void onDisable() {

console.info("Disabling the TARDIS plugin...");

// TODO: onDisable code

}

public static Permission permission = null;

@SuppressWarnings("unused")
private boolean setupPermissions()
{
RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
if (permissionProvider != null) {
permission = permissionProvider.getProvider();
}
return (permission != null);
}

public void onPlayerJoin(PlayerJoinEvent evt) {

Player player = evt.getPlayer();

if ( player.hasPermission("TARDIScraft.admin") ) {

console.info("Admin " + player.getName() + " has joined the game.");

}

}

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

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

boolean senderIsPlayer;

if ( sender instanceof Player ) {

senderIsPlayer = true;

} else senderIsPlayer = false;

// TODO: tardis Command

if ( args[0].equalsIgnoreCase("admin") ) {

Player playerToAdmin = Bukkit.getPlayer(args[1]);
if ( playerToAdmin != null ) {

permission.playerAdd(playerToAdmin, "TARDIScraft.admin");

}

}

return true;

} else {

return false;
}

}

}

最佳答案

NullPointerException 来自“getLogger()”调用。父类“JavaPlugin”必须有 initialize()在记录器存在之前调用。由于您是在实例化时而不是在调用 initialize() 之后获取记录器,因此父类返回 null。

documentation建议在 onEnable() 和 onDisable() 中简单地调用 getLogger(),可能是因为那时已经调用了 initialize()。

注意:源代码说不要自己调用initialize()!

参见:

  1. https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/plugin/java/JavaPlugin.java#L246

  2. http://wiki.bukkit.org/Plugin_Tutorial#Logging_a_message

关于java - 启动我的 Bukkit 插件时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17797981/

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