Getting a name from a list in config is throwing a NullPointerException

Discussion in 'Plugin Development' started by Mxeaez, Mar 12, 2014.

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

    Mxeaez

    Ok so i'm trying to make a class plugin. When a player types /class (classname) it puts their name in a list and the list gets saved to config. In a command, if I put an if statement to check if the players name is in the config, it throws a NullPointerException when I do the command. Any idea why? I've been trying to get this to work for hours.

    Main class:
    Code:java
    1. package me.Mxeaez.Classes;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Classes extends JavaPlugin {
    15.  
    16. Logger myPluginLogger = Bukkit.getLogger();
    17. MyConfigManager manager;
    18. MyConfig classConfig;
    19. public Classes plugin;
    20.  
    21. @Override
    22. public void onEnable(){
    23. new Test(this);
    24. manager = new MyConfigManager(this);
    25. classConfig = manager.getNewConfig("Classes.yml", new String[] {"Class Config"});
    26. }
    27.  
    28. @Override
    29. public void onDisable()
    30. {
    31. }
    32.  
    33. List<String> Test = new ArrayList<String>();
    34.  
    35. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    36. {
    37. if(sender instanceof Player)
    38. {
    39. Player player = (Player) sender;
    40.  
    41. if(cmd.getName().equalsIgnoreCase("class"))
    42. {
    43. if(args.length == 0)
    44. {
    45. player.sendMessage(ChatColor.RED + "-----" + ChatColor.GOLD + "Classes Available" + ChatColor.RED + "-----");
    46. player.sendMessage(ChatColor.AQUA + "-Test");
    47. }
    48. else if(args.length == 1)
    49. {
    50. if(args[0].equalsIgnoreCase("test"))
    51. {
    52. if(plugin.classConfig.getString("Classes.Test").contains(player.getName()))
    53. {
    54. Test.add(player.getName());
    55. plugin.classConfig.set("Classes.Test", Test);
    56. plugin.classConfig.saveConfig();
    57. player.sendMessage(ChatColor.GOLD + "You are now a Test!");
    58. }
    59. }
    60. }
    61. }
    62. }
    63. return false;
    64. }
    65. }
    66.  


    The error lines that say NullPointerException and what line number (The line number is the if statement to check if the name is in the config)

    Code:
    Caused by: java.lang.NullPointerException
            at me.Mxeaez.Classes.Classes.onCommand(Classes.java:52)
    And lastly, the config file (I already did the command without the check so it put my name in there)

    Code:
    Classes:
      Test:
      - iTzMeLokKin
      - iTzMeLokKin
      - iTzMeLokKin
    
     
  2. Offline

    Mmarz11

    Mxeaez You never initialized the plugin variable therefore it is null.
     
Thread Status:
Not open for further replies.

Share This Page