Why doesn't this work?

Discussion in 'Plugin Development' started by puyttre, Sep 6, 2012.

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

    puyttre

    Hello.

    I am still trying to get familiar with Java, but this just stumps me.

    This code gives me an error:
    Code:java
    1. public class TestPlugin extends JavaPlugin {
    2. public static TestPlugin plugin;
    3. public final ChatListener chat = new ChatListener();
    4. public final GameJumpListener jump = new GameJumpListener();
    5. private static final Logger log = Logger.getLogger("Minecraft");
    6. public PluginManager pm = getServer().getPluginManager();
    7.  
    8. @Override
    9. public void onEnable(){
    10. log.info( "TestPlugin enabled." );
    11.  
    12. pm.registerEvents(this.chat, this);
    13. pm.registerEvents(this.jump, this);
    14. }
    15. @Override
    16. public void onDisable(){
    17. log.info( "TestPlugin disabled." );
    18. }
    19. }


    In Netbeans, it shows no errors. But when I put the plugin into my server,
    Code:
    2012-09-06 19:25:04 [SEVERE] Could not load 'plugins\TestPlugin.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:153)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:222)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:198)
        at net.minecraft.server.ServerConfigurationManagerAbstract.<init>(ServerConfigurationManagerAbstract.java:50)
        at net.minecraft.server.ServerConfigurationManager.<init>(SourceFile:11)
        at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:105)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:378)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.NullPointerException
        at com.TyphoonMC.Puyttre.TestPlugin.TestPlugin.<init>(TestPlugin.java:14)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:149)
        ... 9 more
    The listeners don't have anything to do with it. I already tried removing both listeners and it had no effect. If anyone has any information that could help, I would appreciate it.

    Thanks!
    Also, in advance, I know, I probably sound like a total noob.
     
  2. Offline

    makskay

    "InvalidPluginException" usually means you forgot to package a valid plugin.yml file with the jar.
     
  3. Offline

    puyttre

    makskay
    When I open my TestPlugin.jar with WinRar, it shows plugin.yml along with com and META-INF in the main folder. Should plugin.yml be somewhere else?

    EDIT: Also, in my plugin.yml I include the name, the version, and the main method. I believe that is all that is required.
     
  4. Offline

    makskay

    You sound like you have plugin.yml in the right place but the fact that you say "main method" makes me think there's an error in plugin.yml itself. Could you post your plugin.yml here?
     
  5. Offline

    puyttre

    Sorry for the late reply.

    Code:
    name: TestPlugin
    version: 1.0
    author: Puyttre
    description: A test plugin.
     
    main: com.TyphoonMC.Puyttre.TestPlugin.TestPlugin
     
    website: http://www.typhoonmc.com/
     
  6. Offline

    makskay

    puyttre :: Hrm, that's really odd - the error doesn't seem to have anything to do with plugin.yml. You could maybe try removing the "@Override" annotations on the onEnable() and onDisable() methods but it's a long shot that the issue has anything to do with that and I'm out of ideas otherwise.
     
  7. Offline

    puyttre

    Alright thanks. Removing the @Override doesn't work. I think it may have something to do while creating a new instance of something because of this line in the error
    Code:
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    Is there anything wrong with my Logger or PluginManager constructors?
     
  8. Offline

    makskay

    I don't think so - both of them look to be fine. Based on some quick research this particular version of this error usually crops up when someone overrides the default JavaPlugin constructor itself, which you don't appear to be doing.
     
  9. Offline

    puyttre

    makskay
    Alright thanks for your help anyways. I'll do some experimenting and see if I can get it to work. :)
     
  10. Offline

    Tirelessly

    Give us the entire class including imports, please.

    Actually, you never implemented listener. Right after extends JavaPlugin put implements Listener
    puyttre
     
  11. Offline

    Chiller

    Try putting pm = getServer().getPluginManager(); Right before you register the events.
     
  12. Offline

    Sleaker

    Really need you to post the entire source including the package of the class. It's failing during the constructor, so it shouldn't have anything to do with the onEnable or onDisable
     
  13. Offline

    StevasaurousREX

    @puyttre I did this very thing in a test using the code you provided and the plugin.yml remaking the listener class (just blank) got the same error till I the above. Luckily I read other posts or I would have suggested the same thing

    Code:Java
    1. public class TestPlugin extends JavaPlugin {
    2. public static TestPlugin plugin;
    3. public final ChatListener chat = new ChatListener();
    4. public final GameJumpListener jump = new GameJumpListener();
    5. private static final Logger log = Logger.getLogger("Minecraft");
    6.  
    7. @Override
    8. public void onEnable() {
    9. log.info("TestPlugin enabled.");
    10.  
    11. PluginManager pm = getServer().getPluginManager();
    12. pm.registerEvents(this.chat, this);
    13. pm.registerEvents(this.jump, this);
    14. }
    15.  
    16. @Override
    17. public void onDisable() {
    18. log.info("TestPlugin disabled.");
    19. }
    20. }
    21.  
     
  14. Offline

    Sushi

    Can you post the entire class as it appears in your text editor? (including the correct line numbers)
     
  15. Offline

    Chiller

    getServer() returns null when you use it outside of your onEnable constructor.
     
  16. Offline

    puyttre

    Thanks for the help guys but I completely restarted with my code. I don't know what it was, but my code works now. All the imports were correct and like I said, there were no errors.

    Sorry to people coming here to look for a fix. I don't know the answer.
     
  17. Offline

    Tirelessly

    Jesus, I gave you the answer dude. You never implemented Listener so when you tried to call events it came up with errors. puyttre
     
  18. Offline

    puyttre

    Right, but you replied a day after I restarted.

    I said thanks, either say you're welcome or just leave.

    p.s. I don't think your solution was right, either. I don't see why I would need to Implement Listener...
     
  19. Offline

    Tirelessly

    Right, but I responded an hour and a half after you posted this. And you're stupid for not thinking you need to implement Listener, that's what listens for events idiot. You need to implement it or else you can't register events (Like you did in your onEnable, which was what caused the error) or access EventHandler.
     
  20. Offline

    puyttre

    Weird... My new code doesn't implement Listener but it works fine!
     
  21. Offline

    Tirelessly

    code me.
     
  22. Offline

    puyttre

    Code:java
    1. public class TestPlugin extends JavaPlugin {
    2. private static FileConfiguration config;
    3. private ChatListener chat = new ChatListener();
    4. private GameJumpListener jump = new GameJumpListener();
    5.  
    6. @Override
    7. public void onEnable(){
    8. File config = new File(this.getDataFolder(), "config.yml");
    9. PluginManager pm = getServer().getPluginManager();
    10.  
    11. log("[TestPlugin] TestPlugin enabled.");
    12. pm.registerEvents(this.chat, this);
    13. pm.registerEvents(this.jump, this);
    14.  
    15. if(config.exists()){
    16. log("[TestPlugin] Found config. Using current config.");
    17. }else{
    18. warn("[TestPlugin] Confing not found. Generating a new one.");
    19. this.saveDefaultConfig();
    20. }
    21. }
    22. @Override
    23. public void onDisable(){
    24. log("[TestPlugin] TestPlugin disabled.");
    25. }
    26.  
    27. public void log(String info){
    28. getServer().getLogger().info(info);
    29. }
    30. public void warn(String warning){
    31. getServer().getLogger().warning(warning);
    32. }
    33. }
     
  23. Offline

    Tirelessly

    Not even going to respond to you..
     
  24. Offline

    StevasaurousREX

    Dude you are wrong, it was Chiller that originally post the fix. What you said was complete unrelated to this error.

    Notice line 9 on that, that is what you had wrong at first.

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

    Chiller

    Finally someone that knows what was wrong unlike the other guy that was talking about implementing Listener.

    Tirelessly please learn java before trying to help other people with java...
     
  26. Offline

    Tirelessly

    Hahahahaaha good one.
     
Thread Status:
Not open for further replies.

Share This Page