Solved NullPointerException

Discussion in 'Plugin Development' started by TheMinecraftKnight, Jan 26, 2017.

Thread Status:
Not open for further replies.
  1. I've just split my plugin into different classes (before all the commands were all in one), and now I'm getting a NullPointerException.
    All my class files (except the main one) have this at the start:
    Code:
        private ClassName plugin;
        public ClassName(KnightzMCBasics knightzMCBasics) {
        }
        public void MyPluginCommandExecutor(ClassName plugin) {
            this.plugin = plugin;
        }
    They don't actually say ClassName, it's for easier viewing.
    My main class has this in the onEnable():
    Code:
            this.getCommand("knightzmc").setExecutor(new KnightzMCLinks(this));
            this.getCommand("knightzmc").setExecutor(new KnightzMCBurn(this));
            this.getCommand("knightzmc").setExecutor(new KnightzMCProfits(this));
            this.getCommand("knightzmc").setExecutor(new KnightzMCHints(this));
            this.getCommand("knightzmc").setExecutor(new KnightzMCHeals(this));
            this.getCommand("knightzmc").setExecutor(new KnightzMCRandomTP(this));
    and also vault implementation.
    Before you ask, no I haven't extended JavaPlugin in all of the class files, however I'm really new to java and any help would be nice!
    Thanks!
     
  2. Offline

    Zombie_Striker

    @TheMinecraftKnight
    Don't. You can only have one class that extends Java plugin.

    Please read this, and then tell us which line is throwing the NPE
    https://bukkit.org/threads/how-to-r...ubleshoot-your-own-plugins-by-yourself.32457/

    Then you're going to have a bad time. Without propper knowledge of Java by itself, you will run into problems like these very often. I have been on this forum for almost four years. Most of the problems here are caused from a lack of knowledge in Java. Please, pick a tutorial from the following list, learn Java, and then come back to bukkit. It should not take more than a month to finish them.
    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/
     
  3. Followed the stack trace post, there was something wrong with Vault's setupChat
    I just commented it all and it's fixed now, not really sure why though - I got the code from Vault's github page
    EDIT: The commands all work in the console, but not as a player...
     
  4. Online

    timtower Administrator Administrator Moderator

    @TheMinecraftKnight 1. Don't comment it out, it throws an error with a reason.
    2. Post the code for the commands please.
    3. Please post your main class.
     
  5. Online

    timtower Administrator Administrator Moderator

    @TheMinecraftKnight
    Code:
    public void MyPluginCommandExecutor(KnightzMCCommandExecutor plugin) {
            this.plugin = plugin; // Store the plugin in situations where you need it.
        }
    Please just put the middle line in the constructor, made for that pretty much.
    If you still have errors: please post your new server log
     
  6. By constructor do you mean the main class?
     
  7. Online

    timtower Administrator Administrator Moderator

    @TheMinecraftKnight
    Code:
    public KnightzMCCommandExecutor(KnightzMCBasics knightzMCBasics) {     }
    That is your constructor, yet it does nothing.
    Your MyPluginCommandExecutor does something it shouldn't be doing: setting a field to itself, never gets called anyways.
     
  8. I've added that, however it comes up with an error: plugin cannot be resolved to a variable
    http://pastebin.com/uXFyp0aC
     
  9. Online

    timtower Administrator Administrator Moderator

  10. Would just the default field that was created by Eclipse work? It doesn't seem to be right now, do I have to add something else to private Object plugin; ?
     
  11. Online

    timtower Administrator Administrator Moderator

    Yeah, it needs to be the type of the main class.
     
  12. I know this is probably a noob question, I'm currently in the middle of learning java, but how would I do that?
     
  13. Offline

    Drkmaster83

    Types are defined more or less by the class names (only if they're object-oriented). Your main class in the Bukkit plugin context would be whichever class has your onEnable() (there should only be one, else you're doing this all wrong). String is of type String, int is an int, Boolean is a Boolean, and if my main class is called DrkPlugin and has my onEnable(), my main class's type would be of DrkPlugin type.
    @TheMinecraftKnight if you were only to make it an Object, well, a lot of things are objects. You want to make sure the program knows you mean this specific object with all the data associated with it (so not Object, but "DrkPlugin" in my example).

    A quick google search yielded this, which will only help with the basic data types (int, char, long, short, byte, boolean, double, NOT stuff like String, JavaPlugin, Boolean (capitalization matters!))
     
Thread Status:
Not open for further replies.

Share This Page