来自bukkit插件(公民2和派系)的令人沮丧的NPE / NME

时间:2018-05-29 16:59:35

标签: minecraft bukkit

所以我写了一个公民2特性,这个特质应该让NPC的派系拥有,但是我遇到了一些我无法解决的问题。任何帮助,将不胜感激! 当我启动服务器时会发生这种情况:

[19:32:26] [Server thread/WARN]: [Citizens] Task #67 for Citizens v2.0.22- 
SNAPSHOT (build 1487) generated an exception
java.lang.NullPointerException
at redcrafter.factioncontrol.FactionTrait.onAttach(FactionTrait.java:45) ~[?:?]
at net.citizensnpcs.api.trait.Trait.linkToNPC(Trait.java:46) ~[?:?]
at net.citizensnpcs.api.npc.AbstractNPC.addTrait(AbstractNPC.java:100) ~[?:?]
at net.citizensnpcs.api.npc.AbstractNPC.load(AbstractNPC.java:300) ~[?:?]
at net.citizensnpcs.npc.CitizensNPC.load(CitizensNPC.java:134) ~[?:?]
at net.citizensnpcs.api.npc.SimpleNPCDataStore.loadInto(SimpleNPCDataStore.java:59) ~[?:?]
at net.citizensnpcs.Citizens$2.run(Citizens.java:305) ~[?:?]
at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[minecraft_server.jar:git-Spigot-de459a2-51263e9]
at org.bukkit.craftbukkit.v1_10_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:353) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:723) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_171]

当我右键点击NPC时会发生这种情况

[19:35:28] [Server thread/ERROR]: Could not pass event NPCRightClickEvent to 
Citizens v2.0.22-SNAPSHOT (build 1487)
org.bukkit.event.EventException
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[minecraft_server.jar:git-Spigot-de459a2-51263e9]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[minecraft_server.jar:git-Spigot-de459a2-51263e9]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.citizensnpcs.EventListen.onPlayerInteractEntity(EventListen.java:414) [Citizens(4).jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_171]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_171]
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1540) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.PacketPlayInUseEntity.a(SourceFile:68) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.PacketPlayInUseEntity.a(SourceFile:12) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_171]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_171]
at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:732) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:400) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:668) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:567) [minecraft_server.jar:git-Spigot-de459a2-51263e9]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_171]
Caused by: java.lang.NoSuchMethodError: com.massivecraft.factions.entity.FactionColl.get(Ljava/lang/Object;)Lcom/massivecraft/factions/entity/Faction;
at redcrafter.factioncontrol.FactionTrait.getFaction(FactionTrait.java:54) ~[?:?]
at redcrafter.factioncontrol.FactionTrait.onClick(FactionTrait.java:37) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_171]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_171]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_171]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_171]
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[minecraft_server.jar:git-Spigot-de459a2-51263e9]
... 24 more

这是我的代码:

package redcrafter.factioncontrol;

import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.event.EventHandler;

import com.massivecraft.factions.Rel;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.MPlayer;

import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.trait.trait.Owner;

@TraitName("faction")
public class FactionTrait extends Trait {

    @Persist
    private String originalOwner;

    @Persist
    private String faction;

    public FactionTrait() {
        super("faction");
    }

    @EventHandler
    public void onClick(NPCRightClickEvent e) {
        if(e.getNPC() != getNPC()) return;
        Faction f = MPlayer.get(e.getClicker()).getFaction();
        if((f.getId().equals(getFaction().getId()) && (MPlayer.get(e.getClicker()).getRelationTo(f) == Rel.OFFICER || MPlayer.get(e.getClicker()).getRelationTo(f) == Rel.LEADER))
        || e.getClicker().getUniqueId().toString().equals(originalOwner)) {
            getNPC().getTrait(Owner.class).setOwner(e.getClicker());
            e.getClicker().sendMessage("Revokable ownership of the NPC transferred to you!");
        }
    }

    public void onAttach() {
        originalOwner = getNPC().getTrait(Owner.class).getOwnerId().toString();
        faction = FactionColl.get().getNone().getId();
    }

    public void setFaction(Faction f) {
        faction = f.getId();
    }

    public Faction getFaction() {
        if(FactionColl.get().containsId(faction)) return FactionColl.get().get(faction);
        setFaction(FactionColl.get().getNone());
        return FactionColl.get().getNone();
    }

    public OfflinePlayer getOriginalOwner() {
        return Bukkit.getOfflinePlayer(UUID.fromString(originalOwner));
    }

    public void setOriginalOwner(OfflinePlayer p) {
        originalOwner = p.getUniqueId().toString();
    }
}

似乎FactionsColl.get()方法不存在,因为代码编译得很好而没有意义。至于NPE,我不知道是什么导致它。

1 个答案:

答案 0 :(得分:0)

您获得的错误在线

originalOwner = getNPC().getTrait(Owner.class).getOwnerId().toString();

当您对返回null的内容调用方法时,可能会出现Null Pointer异常。

由于行的长度,很难从错误所在的轨迹进行调试。

您需要将其拆分为多行,或使用调试器查找null的位置。

可能.getNPC返回null但我们无法告诉您,因为您没有为getNPC提供代码。

也可能.getTrait返回null。