Constructor Trouble

Discussion in 'Plugin Development' started by hawkfalcon, Aug 9, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    hawkfalcon

  2. Offline

    HamOmlet

    You could use getter methods.

    Also, looking at your source code:

    Code:
    public Listener Events = new Events(this);
    public Listener TheMethods = new Events(this);
    public Listener Tag = new Tag(this);
    What are you trying to achieve here? I can't see why you're declaring Listener instead of the class.
     
  3. Offline

    Taco

    Yeah.. It should be more like:
    public Events events = new Events(this);

    Then events would be the object you use to register that class.
     
  4. Offline

    hawkfalcon

    That is me registering my events, Not where I am making a class constructor.
     
  5. Offline

    Taco

    But... That bit of code would give you three instances of the Listener class, rather than your classes that implement Listener. Also, posting the error and the line that the error points to really helps people determine what your issue is.
     
  6. Offline

    hawkfalcon

    Each one of them implements listener. :3
    /me is not used to multiple classes ;p
    Error:
    Code:
    18:39:49 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tag' in plugin MCTag v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:281)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:583)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:476)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
        at me.hawkfalcon.mctag.TheMethods.tagPlayer(TheMethods.java:73)
        at me.hawkfalcon.mctag.Commands.onCommand(Commands.java:68)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 15 more
    >
    I was basing my code off of travja 's hungerarena
     
  7. Offline

    Taco

    Well, looking at your code, it appears that the player variable you're passing into tagPlayer() may be null.
     
  8. Offline

    hawkfalcon

    I tried it on another command and got
    Code:
    18:56:54 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tag' in plugin MCTag v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:168)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:492)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:878)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:825)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:281)
        at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:109)
        at net.minecraft.server.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:583)
        at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:212)
        at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:476)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:408)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
        at me.hawkfalcon.mctag.TheMethods.selectPlayer(TheMethods.java:24)
        at me.hawkfalcon.mctag.Commands.onCommand(Commands.java:53)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 15 more
    >
    
    It is a method that doesn't require a player. So thats not it.

    Whats wrong with
    Code:
        private MCTag plugin;
        private TheMethods method;
        public Tag(MCTag m) {
        this.plugin = m;
        this.method = m.method;
        }
    ?

    Or can somebody tell me a better way to do constructors?:) thanks. !!!!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  9. Offline

    HamOmlet

    In your main class:
    Code:
    private Tag tag;
     
    @Override
    public void onEnable()
    {
        tag = new Tag(this);
    }
    
    In your Tag class:
    Code:
    private MCTag plugin;
     
    public Tag(MCTag plugin)
    {
      this.plugin = plugin;
    }
    
     
    hawkfalcon likes this.
  10. Offline

    travja



    I have that, gives somewhat of how to register events. Chat listener is out of date though as that was 1.2.5 not 1.3
     
    hawkfalcon likes this.
  11. Offline

    azazad

    No it wouldn't. The three objects created would all be referenced as Listeners, but their classes would be different. And Listener isn't even a class :D

    Declaring the listener references to be of the interface (Listener) is a perfectly legitimate technique. I do it all the time in my plugins.
     
    hawkfalcon and nisovin like this.
  12. Offline

    Taco

    I've honestly never seen this used before, and I'm not quite sure I follow what you're saying, but I'll look into it so that I don't make that mistake again.
     
Thread Status:
Not open for further replies.

Share This Page