Bukkit - NullPointerException

时间:2015-11-08 21:48:02

标签: java nullpointerexception bukkit

执行命令“/ group”和“/ group example_player”时,我得到一个NullPointerException。 它没有给我任何更多的信息,问题是什么(或在哪里)。 我希望,StackOverflow可以提供帮助。

Stym.java:

initPicker(datePicker, resources.getStringArray(R.array.calendar_months));

GroupCommand.java:

import org.bukkit.plugin.java.JavaPlugin;

public class Stym extends JavaPlugin {

    @Override
    public void onEnable() {
        getConfig().options().copyDefaults(true);
        saveConfig();
        registerCommands();
        registerListeners();

    }

    public void registerCommands() {
        GroupCommand groupCommandClass = new GroupCommand(this);
        getCommand("GROUP").setExecutor(groupCommandClass);

    }

    public void registerListeners() {

    }

    public boolean isLeather(String playerName) {
        for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
            if(!ironPlayerName.equals(playerName)) {
                for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
                    return !diamondPlayerName.equals(playerName);

                }

            } else {
                return false;

            }

        }

        return false;

    }

    public boolean isIron(String playerName) {
        for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
            if(ironPlayerName.equals(playerName)) {
                for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
                    return !diamondPlayerName.equals(playerName);

                }

            } else {
                return false;

            }

        }

        return false;

    }

    public boolean isDiamond(String playerName) {
        for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
            if(!ironPlayerName.equals(playerName)) {
                for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
                    return diamondPlayerName.equals(playerName);

                }

            } else {
                return false;

            }

        }

        return false;

    }

}

plugin.yml:

import org.bukkit.entity.Player;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.CommandExecutor;

public class GroupCommand implements CommandExecutor {

    Stym stym;

    GroupCommand(Stym stymClass) {

    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] arguments) {
        try {
            if(arguments.length == 0) {
                if(sender instanceof Player) {
                    if(!sender.isOp()) {
                        if(stym.isLeather(sender.getName())) {
                            sender.sendMessage("You are made out of leather!");

                        } else if(stym.isIron(sender.getName())) {
                            sender.sendMessage("You are made out of iron!");

                        } else {
                            sender.sendMessage("You are made out of diamond!");

                        }

                    } else {
                        sender.sendMessage("You are an operator!");

                    }

                } else {
                    sender.sendMessage("You are a god!");

                }

            } else if(arguments.length == 1) {
                if(!arguments[0].equals("Krischon")) {
                    if(stym.isLeather(sender.getName())) {
                        sender.sendMessage(arguments[0] + " " + "is made out of leather!");

                    } else if(stym.isIron(sender.getName())) {
                        sender.sendMessage(arguments[0] + " " + "is made out of iron!");

                    } else {
                        sender.sendMessage(arguments[0] + " " + "is made out of diamond!");

                    }

                } else {
                    sender.sendMessage("Krischon is an operator!");

                }

            } else {
                sender.sendMessage("Not supported yet!");

            }

        } catch(Exception exception) {
            System.out.println(exception);

        }

        return true;

    }

}

config.yml:

name: Stym
main: Stym
version: 1.3.3.7
commands:
  group:

1 个答案:

答案 0 :(得分:1)

Stym stym;

GroupCommand(Stym stymClass) {

}

您从未在构造函数中初始化Stym stym。因此,当您尝试在方法中使用它时,它是null

Stym stym;

GroupCommand(Stym stymClass) {
    stym = stymClass;
}

For loop

for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
    return !diamondPlayerName.equals(playerName);

}

设置这些循环的方式,它们只会评估列表中的第一个名称并返回相应的布尔值。在return值的那一刻,该方法将终止。你最好使用这样的东西:

public boolean isLeather(String playerName) {
    for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
        if(ironPlayerName.equals(playerName)) {
            return false;
        }
    }
    for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
        if(diamondPlayerName.equals(playerName)) {
            return false;
        }
    }
    return true;
}

这样它将贯穿铁和钻石列表,如果在其中一个列表中找到了玩家名称,它将立即返回false。如果它通过两个列表而没有找到玩家名称,它将返回true。您还需要为isIronisDiamond执行类似的操作。

如果你还有"groups.leather",那么这可以更简单地完成。

public boolean isLeather(String playerName) {
    for(String leatherPlayerName : getConfig().getStringList("groups.leather")) {
        if(leatherPlayerName.equals(playerName)) {
            return true;
        }
    }
    return false;
}

public boolean isIron(String playerName) {
    for(String ironPlayerName : getConfig().getStringList("groups.iron")) {
        if(ironPlayerName.equals(playerName)) {
            return true;
        }
    }
    return false;
}

public boolean isDiamond(String playerName) {
    for(String diamondPlayerName : getConfig().getStringList("groups.diamond")) {
        if(diamondPlayerName.equals(playerName)) {
            return true;
        }
    }
    return false;
}

此方法仅搜索相应的皮革/铁/钻石列表。由于您无论如何都要调用Stym中的所有3种方法,因此这是理想的。