NEP On Main.class?

Discussion in 'Plugin Development' started by PolarCraft, Dec 4, 2013.

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

    PolarCraft

    Okay so I should not be getting a nullpointerexception on my main class file.

    Error line:
    Code:java
    1. getCommand("Am").setExecutor(new Am());


    Main Class:
    Code:java
    1. package net.yougold.org;
    2.  
    3. import net.yougold.org.Commands.Am;
    4. import net.yougold.org.Commands.Ip;
    5. import net.yougold.org.Commands.PInfo;
    6.  
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. public void onEnable(){
    12. getCommand("Am").setExecutor(new Am());
    13. getCommand("Ip").setExecutor(new Ip());
    14. getCommand("PInfo").setExecutor(new PInfo());
    15. getConfig().options().copyDefaults(true);
    16. saveDefaultConfig();
    17. }
    18.  
    19. public void onDisable(){
    20.  
    21. }
    22. }
    23.  
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    PolarCraft We can't tell if you should or should not be getting an NPE in your main class. Can you post your plugin description file?
    Although my guess is that you did not declare the commands.
     
  3. Offline

    PolarCraft

    Sagacious_Zed
    Code:java
    1. name: AdministrationTools
    2. main: net.yougold.org.Main
    3. version: 12.4.13
    4. commands:
    5. am:
    6. description: Allows you to change the motd!
    7. ip:
    8. description: Show you the players ip address!
    9. pinfo:
    10. description: Shows you the Players information!
    11. permissions:
    12. at.*:
    13. description: Gives access to all am commands
    14. children:
    15. at.am: true
    16. at.ip: true
    17. at.pinfo: true
    18. at.am:
    19. description: Allows you to change the motd
    20. default: op
    21. at.pinfo:
    22. description: Shows you the Players information
    23. default: op
    24. at.ip:
    25. description: Show you the players ip address
    26. default: true
    27.  
     
  4. Offline

    masons123456

    The command in your main class is"Am" but in your plugin.yml it is "am"
     
  5. Offline

    PolarCraft

    masons123456 It should not matter if it is capitalized or not. In the command class itself has ignorecase. Which will do it if you type /am or /Am or /AM.
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    PolarCraft In this case please post the stacktrace
     
  7. Offline

    The_Doctor_123

    Do you get a startup error? It looks like your command descriptions are improperly spaced.
     
  8. Offline

    PolarCraft

    The_Doctor_123
    Code:java
    1. at net.yougold.org.Main.onEnable(Main.java:12)
    2. at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    3. at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    4. .java:457)
    5. at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    6. r.java:381)
    7. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.jav
    8. a:282)
    9. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.
    10. java:264)
    11. at net.minecraft.server.v1_6_R3.MinecraftServer.l(MinecraftServer.java:3
    12. 15)
    13. at net.minecraft.server.v1_6_R3.MinecraftServer.f(MinecraftServer.java:2
    14. 92)
    15. at net.minecraft.server.v1_6_R3.MinecraftServer.a(MinecraftServer.java:2
    16. 52)
    17. at net.minecraft.server.v1_6_R3.DedicatedServer.init(DedicatedServer.jav
    18. a:152)
    19. at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    20. :393)
    21. at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    22. 83)
    23. 11:49:47 [INFO] Server permissions file permissions.yml is empty, ignoring it
    24. 11:49:47 [INFO] Done (6.956s)! For help, type "help" or "?"
    25. >
     
  9. Offline

    RROD

    Yeah it would be down to the "Am" and "am" capitalisation. Try registering it without capitals, it's case-sensitive.
     
  10. Offline

    PolarCraft

    RROD Already did that...
     
  11. Offline

    RROD

    Try this as your plugin.yml file. If it fails, the chances are there is something wrong inside your "Am" class file.
     
  12. Offline

    PolarCraft

    RROD It is all of my classes -_-

    AM:
    Code:java
    1. package net.yougold.org.Commands;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Am extends JavaPlugin {
    9.  
    10. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    11. if (cmd.getName().equalsIgnoreCase("am") || ((sender.isOp()))) {
    12. if (sender.hasPermission("at.am")) {
    13. sender.sendMessage(ChatColor.RED + "You are not permitted to do this!");
    14. return true;
    15. }
    16. if (args.length == 0) {
    17. sender.sendMessage(ChatColor.RED + "Please specify a message!");
    18. return true;
    19. }
    20. StringBuilder str = new StringBuilder();
    21. for (int i = 0; i < args.length; i++) {
    22. str.append(args[i] + " ");
    23. }
    24. String motd = str.toString();
    25. getConfig().set("motd", motd);
    26. saveConfig();
    27. String motd1 = getConfig().getString("motd");
    28. motd1 = motd1.replaceAll("&", "§");
    29. sender.sendMessage(ChatColor.GREEN + "MOTD set to: " + motd1);
    30. }
    31. return false;
    32. }
    33. }
    34. [/i]
     
  13. Offline

    moose517

    your am class should implement commandExecutor, not extend JavaPlugin. thats your problem.
     
  14. Offline

    PolarCraft

    moose517 It will give too many errors if i do extends JavaPlugin. And also most of my plugins run off of extends JavaPlugin.
     
  15. PolarCraft
    Big error. What moose517 said is correct. Only your main class should extend JavaPlugin. Instead, implement CommandExecutor.
     
  16. Offline

    PolarCraft

    The Gaming Grunts Okay so I implemented CommandExecutor and fixed the errors for am. But i still get npe.
     
  17. PolarCraft
    Try creating a constructor for the class. For example:

    Code:java
    1. public class Am implements CommandExecutor{
    2.  
    3. public Am(){
    4. }
    5. //your code
    6. }
     
  18. Offline

    PolarCraft

    The Gaming Grunts Already have :D
    Code:java
    1. public class Am implements CommandExecutor {
    2.  
    3. private Main plugin;
    4.  
    5. public Am(Main plugin) {
    6. this.plugin = plugin;
    7. }
    8.  
    9. public Am() {
    10. // TODO Auto-generated constructor stub
    11. }
     
  19. Offline

    PolarCraft

    The Gaming Grunts If i remove one it will give me the error from the main.class.

    Main:
    Code:java
    1. package net.yougold.org;
    2.  
    3. import net.yougold.org.Commands.*;
    4.  
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class Main extends JavaPlugin {
    8.  
    9. public void onEnable(){
    10. getCommand("am").setExecutor(new Am());
    11. getCommand("ip").setExecutor(new Ip());
    12. getCommand("pinfo").setExecutor(new PInfo());
    13. getConfig().options().copyDefaults(true);
    14. saveDefaultConfig();
    15. new Am(this);
    16. }
    17.  
    18. public void onDisable(){
    19.  
    20. }
    21. }
    22.  
     
  20. Offline

    Conarnar

    Code:
    getCommand("am").setExecutor(new Am(this));
    And then you can remove the second constructor.
     
    The Gaming Grunts likes this.
  21. PolarCraft
    Get rid of "new Am(this)" at line 15
     
  22. Offline

    PolarCraft

  23. Offline

    Conarnar

    Do
    Code:java
    1. System.out.println(getCommand("am"));
    2.  

    and see what happens.
     
  24. PolarCraft
    Post all your code for your 2 classes and the full error. Maybe we missed something.
     
  25. Offline

    PolarCraft

    The Gaming Grunts Conarnar
    Main Class:
    Code:java
    1. package net.yougold.org;
    2.  
    3. import net.yougold.org.Commands.*;
    4.  
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class Main extends JavaPlugin {
    8.  
    9. public void onEnable(){
    10. getCommand("am").setExecutor(new Am(this));
    11. getCommand("ip").setExecutor(new Ip(this));
    12. getCommand("pinfo").setExecutor(new PInfo(this));
    13. getConfig().options().copyDefaults(true);
    14. saveDefaultConfig();
    15. }
    16.  
    17. public void onDisable(){
    18.  
    19. }
    20. }
    21.  

    Am:
    Code:java
    1. package net.yougold.org.Commands;
    2.  
    3. import net.yougold.org.Main;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9.  
    10. public class Am implements CommandExecutor {
    11.  
    12. private Main plugin;
    13.  
    14. public Am(Main plugin) {
    15. this.plugin = plugin;
    16. }
    17.  
    18. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    19. if (cmd.getName().equalsIgnoreCase("am") || ((sender.isOp()))) {
    20. if (sender.hasPermission("at.am")) {
    21. sender.sendMessage(ChatColor.RED + "You are not permitted to do this!");
    22. return true;
    23. }
    24. if (args.length == 0) {
    25. sender.sendMessage(ChatColor.RED + "Please specify a message!");
    26. return true;
    27. }
    28. StringBuilder str = new StringBuilder();
    29. for (int i = 0; i < args.length; i++) {
    30. str.append(args[i] + " ");
    31. }
    32. String motd = str.toString();
    33. plugin.getConfig().set("motd", motd);
    34. plugin.saveConfig();
    35. String motd1 = plugin.getConfig().getString("motd");
    36. motd1 = motd1.replaceAll("&", "§");
    37. sender.sendMessage(ChatColor.GREEN + "MOTD set to: " + motd1);
    38. }
    39. return false;
    40. }
    41. }
    42. [/i]
     
  26. Offline

    zeeveener

    PolarCraft

    Please post an updated error log. The old one is for old code, numbers may have changed since then.
     
  27. Offline

    Wingzzz

    With the updates done to the classes, can you now show the stack trace you're getting?
     
  28. Offline

    PolarCraft

    zeeveener Wingzzz It was another plugin causing this sorry. But now the coding is:

    Main.class:
    Code:java
    1. package net.yougold.org;
    2.  
    3. import net.yougold.org.DiamondListener;
    4.  
    5. import org.bukkit.plugin.java.JavaPlugin;
    6.  
    7. public class Main extends JavaPlugin {
    8.  
    9. public void onEnable(){
    10. getServer().getPluginManager().registerEvents(new DiamondListener(), this);
    11. getConfig().options().copyDefaults(true);
    12. saveDefaultConfig();
    13. }
    14. }
    15.  

    DiamondListener.class:
    Code:java
    1. package net.yougold.org;
    2.  
    3. import java.util.List;
    4. import java.util.logging.Level;
    5.  
    6. import net.yougold.org.Main;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.Material;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.BlockBreakEvent;
    13. import org.bukkit.inventory.ItemStack;
    14.  
    15. public class DiamondListener implements Listener {
    16.  
    17. private Main plugin;
    18.  
    19. public DiamondListener(Main plugin) {
    20. this.plugin = plugin;
    21. }
    22.  
    23. public DiamondListener() {
    24. // TODO Auto-generated constructor stub
    25. }
    26.  
    27. @EventHandler
    28. public void onBreak(BlockBreakEvent e){
    29. if(e.getBlock().getType().equals(Material.DIAMOND_ORE)) {
    30. String item = plugin.getConfig().getString("DiamondReward");
    31.  
    32. String s[] = item.split(":");
    33.  
    34. String i = s[0];
    35. String d = s[1];
    36. String a = s[2];
    37.  
    38. Material m = Material.getMaterial(i);
    39.  
    40. if(m == null) { Bukkit.getLogger().log(Level.SEVERE, "The item is null!"); }
    41.  
    42. else {
    43.  
    44. int dv = Integer.getInteger(d);
    45. int amt = Integer.getInteger(a);
    46.  
    47. ItemStack is = new ItemStack(m, amt, (short) dv);
    48.  
    49. e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), is);
    50. }
    51. }
    52. }
    53. }


    Stack-Trace:
    Code:java
    1. 00:26:10 [SEVERE] Error occurred while enabling MiningRewards v12.1.13 (Is it up
    2. to date?)
    3. at net.yougold.org.Main.onEnable(Main.java:10)
    4. at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
    5. at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    6. .java:457)
    7. at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    8. r.java:381)
    9. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugin(CraftServer.jav
    10. a:282)
    11. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.enablePlugins(CraftServer.
    12. java:264)
    13. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:60
    14. 9)
    15. at org.bukkit.Bukkit.reload(Bukkit.java:277)
    16. at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    17. 23)
    18. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
    19. 2)
    20. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServe
    21. r.java:523)
    22. at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchServerCommand(Craf
    23. tServer.java:512)
    24. at net.minecraft.server.v1_6_R3.DedicatedServer.as(DedicatedServer.java:
    25. 263)
    26. at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2
    27. 28)
    28. at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4
    29. 88)
    30. at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    31. :421)
    32. at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    33. 83)
    34. 00:26:10 [INFO] Server permissions file permissions.yml is empty, ignoring it
    35. 00:26:10 [INFO] CONSOLE: Reload complete.
    36. >
     
  29. Offline

    Wingzzz

    Alright, in your listener remove the empty param constructor- it is literally doing nothing.

    Next, in your onEnable() when you initialize the DiamondListener, fill the params like so:
    Code:java
    1. getServer().getPluginManager().registerEvents(new DiamondListener(this), this);

    You need to do that because your DiamondListener class takes a constructor with the params of an object of type Plugin- so in order for the DiamondListener class to use the field named "plugin" it must properly have it initialized (which is done so when you fill the constructor's parameter with an object of type Plugin, which you then have your "plugin" field point to ie: this.plugin = plugin;).

    Note: The reason your empty param / empty body constructor is doing nothing is because objects by default are initialized like so: MyObject object = new MyObject(); Meaning they don't require any parameters. They only do if you create alternate constructors. Albeit, keep in mind you can have multiple constructors that a class can choose from just by specifying multiple like you have. Though, in your case it requires an object of type Plugin to be supplied in order for the class to function properly (otherwise throwing NPE's). So, it is unwise to create a constructor that will not meet requirements of the class unless your provide proper counter measures- which isn't really necessary considering the complexity of your class and how unnecessary it would be to allow the initialization of it without just simply supplying a plugin instance. Times that you would create a constructor with empty params would be when you need to do things such as logic during initialization or create a private constructor in order to hinder initialization (examples). Although, you can do the same thing via an instance block.

    tl;dr (for the note)
    Remove unnecessary constructors and properly fill out the ones that provide essential functionality to your class.

    My odd explanation above aside... I'd recommend reading through how constructors work more thoroughly here.
     
    DrkMatr1984 likes this.
Thread Status:
Not open for further replies.

Share This Page