Plugin is null

Discussion in 'Plugin Development' started by stuntguy3000, Feb 21, 2013.

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

    stuntguy3000

    I love the amount of documentation on how to use other classes >_<

    When i place a block i receive a lovely NPE which points to plugin, being null.

    Main
    Code:
    package net.stuntguy3000;
     
    import java.util.logging.Logger;
     
    import net.stuntguy3000.event.*;
     
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class TSPlugin extends JavaPlugin {
       
        public final TSUtil TSUtil = new TSUtil(this);
        public final evt_build evt_build = new evt_build(this);
       
        public void onEnable() {
            Logger log = Bukkit.getLogger();
           
            getConfig().options().copyDefaults(true);
            saveConfig();
           
            this.getServer().getPluginManager().registerEvents(this.evt_build, this);
        } 
       
        public void onDisable() {
           
        }
    }
    
    TSUtil
    Code:
    package net.stuntguy3000;
     
    import org.bukkit.entity.Player;
     
    public class TSUtil {
        public TSPlugin plugin;
       
        public TSUtil(TSPlugin instance) {
            this.plugin = instance;
        }
       
        public String test(Player p)
        {
            String value = plugin.getServer().getName();
           
            return value;
        }
    }
    
    evt_build
    Code:
    package net.stuntguy3000.event;
     
    import net.stuntguy3000.TSPlugin;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.hanging.HangingBreakByEntityEvent;
    import org.bukkit.event.hanging.HangingPlaceEvent;
     
    public class evt_build implements Listener {
        public TSPlugin plugin;
       
        public evt_build(TSPlugin instance) {
            this.plugin = instance;
        }
       
        public net.stuntguy3000.TSUtil TSUtil = new net.stuntguy3000.TSUtil(plugin);
       
        @EventHandler
        public void BlockPlaceEvent(BlockPlaceEvent event)
        {
            Player p = event.getPlayer();
           
            if (!p.hasPermission("theship.editblocks"))
            {
                event.setCancelled(true);
            }
           
            TSUtil.test(p);
        }
    }
     
  2. Offline

    RainoBoy97

    Make a variable for the classes, and initialize them in the onEnable
     
  3. Offline

    stuntguy3000

    Could you provide a code example? Thanks :D
     
  4. Offline

    RainoBoy97

    Code:
    public final TSUtil tsutil;
    public final evt_build evt_build;
    
    and in onEnable do
    Code:
    tsutil = new TSUtil(this);
    evt_build = new evt_build(this);
    
     
  5. Offline

    stuntguy3000

    Strange errors,

    "tsutil = new TSUtil(this);
    evt_build = new evt_build(this);"

    Eclipse wants to remove the "final" modifiers, and the classname has a error under it about tsutil not being defined. I removed the final modifiers and the error still exists.
     
  6. Offline

    RainoBoy97

    Try rename the variable, since its the same name as the class it maybe be causing the error
     
  7. Offline

    Deckerz

    That's probably the reason ^^

    also don't use underscores it doesn't look good :D im vert petty about things like that
     
  8. Offline

    stuntguy3000

    public TSUtil tsutilTest;
    public evt_build evt_buildTest;

    public void onEnable() {
    Logger log = Bukkit.getLogger();

    getConfig().options().copyDefaults(true);
    saveConfig();

    tsutilTest = new TSUtil(this);
    evt_buildTest = new evt_build(this);

    this.getServer().getPluginManager().registerEvents(this.evt_buildTest, this);
    }

    No fix :/

    TnT md_5 Looking for help :)

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

    mastermustard

    .......... for one u bumped after 2 minutes -_-. secondly... you don't ask them for plugin help questions. read bukkit staff profile pages and rules before you go tagging staff
     
  10. Offline

    stuntguy3000

    Jeez i was just asking help from the 'professional's -.-

    ...Since this thread is going nowhere...
     
  11. Offline

    macguy8

    We don't have be be professionals to help. In evt_build, you create a new TsUtil class with the plugin value, which is null at the time. When you do TSUtil, you'd want to use plugin.tsutil, instead of the version you created.
     
  12. Offline

    stuntguy3000

    I don't get what you mean ...?
     
  13. Offline

    md_5

    1) TnT isn't even a plugin developer
    2) No I won't help you, since tagging me does not entitle you to priority support.
     
    kittenchunks and ZeusAllMighty11 like this.
  14. Offline

    ZeusAllMighty11 Retired Staff

    I had this issue before - I fixed it by creating a new class and COPYING + PASTING into it.
     
  15. Offline

    lycano

    stuntguy3000 to give you a head start here is a modified version of your code

    Since this seems to be a test-plugin transporting the plugin instance everytime seems ok to me but be aware that you should only pass what its needed to a class to avoid overhead.

    E.g. when you want to use the Server instance then only pass getServer() to the other class instead of the whole plugin instance. Or if you only need the name just transport the name as a String like new MyClass(this.getServer().getName()) and the constructor would be public MyClass(String serverName);

    Code hidden in spoiler tags
    Show Spoiler

    Main
    Code:
    package net.stuntguy3000;
     
    import java.util.logging.Logger;
     
    import net.stuntguy3000.event.*;
     
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class TSPlugin extends JavaPlugin {   
        public Logger log;
     
        public void onEnable() {
            this.log = Bukkit.getLogger();
         
            getConfig().options().copyDefaults(true);
            saveConfig();
     
            this.getServer().getPluginManager().registerEvents(new EVT_Build(this), this);
        }
     
        public void onDisable() {
         
        }
    }
    
    EVT_Build
    Code:
    package net.stuntguy3000.event;
     
    import net.stuntguy3000.TSPlugin;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.hanging.HangingBreakByEntityEvent;
    import org.bukkit.event.hanging.HangingPlaceEvent;
     
    public class EVT_Build implements Listener {
        protected TSPlugin plugin;
        protected TSUtil tsUtil;
     
        public EVT_Build(TSPlugin instance) {
            this.plugin = instance;
            this.tsUtil = new TSUtil(this.plugin);
        }
       
        @EventHandler()
        public void BlockPlaceEvent(BlockPlaceEvent event)
        {
            Player p = event.getPlayer();
         
            if (!p.hasPermission("theship.editblocks")) {
                event.setCancelled(true);
            }
         
            player.sendMessage("Test returned: " + tsUtil.test(p));
        }
    }
    
    TSUtil
    Code:
    package net.stuntguy3000;
     
    import org.bukkit.entity.Player;
     
    public class TSUtil {
        protected TSPlugin plugin;
     
        public TSUtil(TSPlugin instance) {
            this.plugin = instance;
        }
     
        public String test(Player p) {
            String value = plugin.getServer().getName();
           
            return value;
        }
    }
    
     
  16. Offline

    stuntguy3000

    Its not actually going to be a test plugin though...what is another method.

    I want to know, how this fixes it...

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

    ZeusAllMighty11 Retired Staff

  18. Offline

    Deckerz

    Code:java
    1.  
    Code:java
    1.  
    2. [FONT=arial]package net.stuntguy3000.event;[/FONT]
    3.  
    4. [FONT=arial]import net.stuntguy3000.TSPlugin;[/FONT]
    5. [FONT=arial]import net.stuntguy3000.TSUtil;[/FONT]
    6. [FONT=arial]import org.bukkit.entity.Player;[/FONT]
    7. [FONT=arial]import org.bukkit.event.EventHandler;[/FONT]
    8. [FONT=arial]import org.bukkit.event.Listener;[/FONT]
    9. [FONT=arial]import org.bukkit.event.block.BlockBreakEvent;[/FONT]
    10. [FONT=arial]import org.bukkit.event.block.BlockPlaceEvent;[/FONT]
    11. [FONT=arial]import org.bukkit.event.hanging.HangingBreakByEntityEvent;[/FONT]
    12. [FONT=arial]import org.bukkit.event.hanging.HangingPlaceEvent;[/FONT]
    13.  
    14. [FONT=arial]public class evt_build implements Listener {[/FONT]
    15. [FONT=arial] public TSPlugin plugin;[/FONT]
    16.  
    17. [FONT=arial] public evt_build(TSPlugin instance) {[/FONT]
    18. [FONT=arial] this.plugin = instance;[/FONT]
    19. [FONT=arial] }[/FONT]
    20.  
    21.  
    22. [FONT=arial] @EventHandler[/FONT]
    23. [FONT=arial] public void BlockPlaceEvent(BlockPlaceEvent event)[/FONT]
    24. [FONT=arial] {[/FONT]
    25. [FONT=arial] Player p = event.getPlayer();[/FONT]
    26.  
    27. [FONT=arial] if (!p.hasPermission("theship.editblocks"))[/FONT]
    28. [FONT=arial] {[/FONT]
    29. [FONT=arial] event.setCancelled(true);[/FONT]
    30. [FONT=arial] }[/FONT]
    31.  
    32. [FONT=arial] TSUtil.test(p);[/FONT]
    33. [FONT=arial] }[/FONT]
    34. [FONT=arial]}[/FONT]

    There that SHOULD work WTF with the [font.] thing?
     
  19. Offline

    Digi

    Bukkit documentation covers its own stuff, it doesn't have to teach you Java, the Java documentation does that.

    And are you dragging and dropping the text ? Or if you're not, then use [code] instead, [syntax] is always screwing with stuff.
     
Thread Status:
Not open for further replies.

Share This Page