NullPointerException

Discussion in 'Plugin Development' started by henne90gen, Jun 26, 2012.

  1. Offline

    henne90gen

    Hello
    I tried to make a plugin that adds something on signs.
    My problem is that I cant enable the plugin because of a NullPointerException-Error.
    Here the errorcode:
    Code (Text):
    1. 2012-06-26 23:04:55 [INFO] [SignNews] Enabling SignNews v0.1
    2. 2012-06-26 23:04:55 [SEVERE] Error occurred while enabling SignNews v0.1 (Is it up to date?)
    3. java.lang.NullPointerException
    4. at com.github.henne90gen.SignNews.onEnable(SignNews.java:25)
    5. at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:215)
    6. at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337)
    7. at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
    8. at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:256)
    9. at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:238)
    10. at net.minecraft.server.MinecraftServer.t(MinecraftServer.java:381)
    11. at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:368)
    12. at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:197)
    13. at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:432)
    14. at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Here are the other files of my plugin:
    MainFile:
    Code (Text):
    1. package com.github.henne90gen;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.configuration.Configuration;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class SignNews extends JavaPlugin{
    14.  
    15. Logger log = Logger.getLogger("Minecraft");
    16.  
    17. public SignNewsCommands sncmd;
    18. Config config;
    19.  
    20. String px = ChatColor.AQUA+"[SignNews] "+ChatColor.WHITE;
    21.  
    22. public void onEnable(){
    23. //Befehle
    24. sncmd = new SignNewsCommands(this);
    25. getCommand("signnews").setExecutor(sncmd);
    26.  
    27. //Config
    28. config = new Config(this);
    29.  
    30. log.info("SignNews enabled!");
    31. }
    32.  
    33. public void onDisable(){
    34. this.saveConfig();
    35. log.info("SignNews disabled!");
    36. }
    37. }
    CommandFile:
    Code (Text):
    1. package com.github.henne90gen;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.configuration.Configuration;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.inventory.ItemStack;
    11.  
    12. public class SignNewsCommands implements CommandExecutor {
    13.  
    14. SignNews plg;
    15. String px;
    16.  
    17. public SignNewsCommands (SignNews plg) {
    18. this.plg = plg;
    19. this.px = plg.px;
    20. }
    21.  
    22. @Override
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    24. if (sender instanceof Player) {
    25. Player p = (Player) sender;
    26.  
    27. //Spielerbefehl "/setsignnews ohne Argumnte
    28. if (args.length < 1) {
    29. if (cmd.getName().equalsIgnoreCase("signnews")) {
    30. p.sendMessage(ChatColor.RED + "Diese Argumente kannst du verwenden:");
    31. p.sendMessage(ChatColor.RED + "create  Eine neue News-Wand erstellen");
    32. }
    33. } else {
    34. if (args.length == 1) return ExecuteCmd(p, args[0]);
    35. if (args.length == 2) return ExecuteCmd(p, args[0], args[1]);
    36. if (args.length == 3) return ExecuteCmd(p, args[0], args[1], args[2]);
    37. }
    38. }
    39. return false;
    40. }
    41.  
    42. public boolean ExecuteCmd(Player p, String cmd) {
    43. return false;
    44. }
    45.  
    46. public boolean ExecuteCmd(Player p, String cmd, String arg) {
    47. return false;
    48. }
    49.  
    50. public boolean ExecuteCmd(Player p, String cmd, String arg1, String arg2) {
    51. return false;
    52. }
    53.  
    54. }
    ConfigFile:
    Code (Text):
    1. package com.github.henne90gen;
    2.  
    3. import org.bukkit.configuration.Configuration;
    4. import org.bukkit.event.block.Action;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class Config {
    8. private SignNews plugin;
    9. private Configuration config;
    10. public String msg1, msg2, msg3;
    11. public boolean invertMouse;
    12. public Action clickAktion;
    13.  
    14. public Config(SignNews plugin) {
    15. this.plugin = plugin;
    16. config = plugin.getConfig().getRoot();
    17. config.options().copyDefaults(true);
    18. plugin.saveConfig();
    19. getOpts();
    20. }
    21. public void getOpts() {
    22. msg1 = "Config.messages.broadcast1";
    23. msg2 = "Config.messages.broadcast2";
    24. msg3 = "Config.messages.broadcast3";
    25.  
    26. }
    27. }
    plugin.yml
    Code (Text):
    1. name: SignNews
    2. version: 0.1
    3. description: Post something new on your sign!
    4. author: henne90gen
    5. website:  http://bukkit.org/
    6.  
    7. main: com.github.henne90gen.SignNews
    8.  
    9. commands:
    I did not added the commands so far.

    PS: Sorry for my bad english I'm german
     
  2. Offline

    Dreeass

    What line has number 25, and learn Java before you make a plugin.
     
  3. Offline

    henne90gen


    Thanks for your answer!
    line number 25 is:
    getCommand("signnews").setExecutor(sncmd);

     
  4. Offline

    Dreeass

    Don't do the sncmd but write it full and do you have a constructor in the class that has to execute the command?
     
  5. Offline

    henne90gen

    The code I posted above was all I have in my plugin.
    So can you look through it? Im a beginner so I dont know what you mean with "constructor" Im sorry.
    The first thing you said was something with write full so what is "full" for you?
     
  6. henne90gen
    Dreeass is asking whether you put a reference to your main class in your command executor. If so, you need to add the argument of (this) when registering your command.
     
  7. Offline

    henne90gen

    Like this:
    getCommand("signnews").setExecutor(this.sncmd);
     
  8. Offline

    Pandemoneus

    Nope, that's not what a constructor is.
    A constructor is a specific method that is called when you use
    Code:java
    1.  
    2. new MyClass(...)
    3.  


    More info: http://www.javabeginner.com/learn-java/java-constructors

    [edit]
    henne90gen
    The reason why you are getting a NullPointerException is because you want to register the command "signnews" which you never defined in your plugin.yml
     
    Codex Arcanum and henne90gen like this.
  9. Offline

    henne90gen

    mmh ;) thanks alot!
    I thought: "Oh lets do that later." and now I stand there dumb.
    Now everything is running correct!
    Thanks
     
  10. Pandemoneus
    Oh. I'm wrong then. I got it mixed up with parameter. :(
     

Share This Page