Solved Don't really know what is wrong... :?

Discussion in 'Plugin Development' started by Disturbed_Creator, Oct 13, 2013.

Thread Status:
Not open for further replies.
  1. Trouble Code:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command,
    2. String label, String[] args) {
    3.  
    4. if (command.getName().equalsIgnoreCase("queue")) {
    5.  
    6. if (!(sender instanceof Player))
    7. return false;
    8.  
    9. Player p = (Player) sender;
    10.  
    11. if (args.length < 1)
    12. return false;
    13.  
    14. World w = Bukkit.getWorld(args[0]);
    15.  
    16. if (w == null)
    17. return false;
    18.  
    19. addToQueue(args[0], p.getName());
    20. return true;
    21.  
    22. }
    23.  
    24. return true;
    25.  
    26. }
    27.  
    28. public static void addToQueue(String MapName, String PlayerName) {
    29.  
    30. if (!plugin.queueMap.containsKey(MapName)) {
    31.  
    32. List<String> pList = new ArrayList<String>();
    33. pList.add(PlayerName);
    34.  
    35. plugin.queueMap.put(MapName, pList);
    36. return;
    37.  
    38. }
    39.  
    40. if (plugin.queueMap.containsKey(MapName)) {
    41.  
    42. for (Map.Entry<String, List<String>> map : plugin.queueMap
    43. .entrySet()) {
    44.  
    45. if (map.getKey().equalsIgnoreCase(MapName)) {
    46.  
    47. if (!map.getValue().contains(PlayerName)) {
    48.  
    49. map.getValue().add(PlayerName);
    50. QueueScoreboard.update(MapName);
    51.  
    52. }
    53. return;
    54.  
    55. }
    56.  
    57. }
    58.  
    59. }
    60.  
    61. }


    Lines that are having issues:
    "addToQueue(args[0], p.getName());"
    "if (!plugin.queueMap.containsKey(MapName))"

    I am getting a Null Pointer Exception :/

    Please help :>
     
  2. Offline

    MineDoubleSpace

    Pezah and Ultimate_n00b like this.
  3. Define your plugin as:

    MainClass plugin;

    public currentclassname(Mainclass i) {

    plugin = i

    }
     
  4. MineDoubleSpace DreTaX

    That is not my entire class file :p
    I am asking what is wrong with just that bit of code. I have defined plugin, and implemented this class as a CommandExecutor. What is wrong in the code I showed you?
     
  5. Offline

    MineDoubleSpace

    Disturbed_Creator still seems like plugin is null, how did you define the plugin?
     
  6. MineDoubleSpace
    It was defined

    static League plugin;
    public QueueCommand(League plugin){
    QueueCommand.plugin = plugin;
    }

    Also there are no error messages in eclipse.
     
  7. Offline

    BungeeTheCookie

    Disturbed_Creator

    Ok, so your constructor is:
    Code:java
    1. static League plugin;
    2.  
    3. public QueueCommand(League plugin){
    4. QueueCommand.plugin = plugin;
    5. }


    I don't know how you got that error, probably because the constructor is set up incorrectly and Eclipse doesn't know it... If your plugin is getting a null pointer there is probably something wrong in your constructor. This is how I use constructors in all of my classes that need them, and it works every time. CREDIT GOES TO spoljo666 FOR THIS METHOD.

    Try this as your constructor:

    Code:java
    1. public final PVPEssentials plugin;
    2.  
    3. public ExplosionListener(final PVPEssentials plugin) {
    4. this.plugin = plugin;
    5. }


    Also, in your main class make sure it is defined

    as public static League plugin;

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
    spoljo666 likes this.
  8. BungeeTheCookie
    With this code I am getting the same problems :/
    Code:java
    1. package me.disturbed.league.commands;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.Map;
    6.  
    7. import me.disturbed.league.League;
    8. import me.disturbed.league.scoreboard.QueueScoreboard;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.World;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandExecutor;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.entity.Player;
    16.  
    17. public class QueueCommand implements CommandExecutor {
    18.  
    19. public final League plugin;
    20.  
    21. public QueueCommand(final League plugin) {
    22.  
    23. this.plugin = plugin;
    24.  
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command command,
    28. String label, String[] args) {
    29.  
    30. if (command.getName().equalsIgnoreCase("queue")) {
    31.  
    32. if (!(sender instanceof Player))
    33. return false;
    34.  
    35. Player p = (Player) sender;
    36.  
    37. if (args.length < 1)
    38. return false;
    39.  
    40. World w = Bukkit.getWorld(args[0]);
    41.  
    42. if (w == null)
    43. return false;
    44.  
    45. addToQueue(args[0], p.getName());
    46. return true;
    47.  
    48. }
    49.  
    50. return true;
    51.  
    52. }
    53.  
    54. public void addToQueue(String MapName, String PlayerName) {
    55.  
    56. if (!plugin.queueMap.containsKey(MapName)) {
    57.  
    58. List<String> pList = new ArrayList<String>();
    59. pList.add(PlayerName);
    60.  
    61. plugin.queueMap.put(MapName, pList);
    62. return;
    63.  
    64. }
    65.  
    66. if (plugin.queueMap.containsKey(MapName)) {
    67.  
    68. for (Map.Entry<String, List<String>> map : plugin.queueMap
    69. .entrySet()) {
    70.  
    71. if (map.getKey().equalsIgnoreCase(MapName)) {
    72.  
    73. if (!map.getValue().contains(PlayerName)) {
    74.  
    75. map.getValue().add(PlayerName);
    76. QueueScoreboard.update(MapName);
    77.  
    78. }
    79. return;
    80.  
    81. }
    82.  
    83. }
    84.  
    85. }
    86.  
    87. }
    88.  
    89. }
    90.  


    BungeeTheCookie MineDoubleSpace DreTaX
    I did some testing and the problem doesn't lie with the plugin variable.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  9. Offline

    Rocoty

    queueMap may be null
     
  10. Rocoty
    queueMap is defined in the Main Class as:
    Code:
    public HashMap<String, List<String>> queueMap;
     
  11. Offline

    xCyanide

    Disturbed_Creator
    You need to do.(You need to do this unless you are defining the hashmap in the onenable or something along the lines of that)
    public HashMap<String, List<String>> queueMap = new HashMap<String, List<String>>();
     
  12. xCyanide
    Thank you so much.... I have spent an ungodly amount of time on this.. only to find out it was a simple fix... I hate the universe.
     
  13. Offline

    BungeeTheCookie

Thread Status:
Not open for further replies.

Share This Page