Solved NPE

Discussion in 'Plugin Development' started by ZodiacTheories, Jul 29, 2014.

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

    ZodiacTheories

    Hi, so when target is null, the code works, but when they are not null, then it doesnt work. Here is my code:

    Code:java
    1. Player p = (Player) sender;
    2.  
    3. if(cmd.getName().equalsIgnoreCase("inventory")) {
    4. if(args.length == 0) {
    5. p.sendMessage(PREFIX + "Usage = /inventory -o <player>");
    6. }
    7. else if(args.length == 1) {
    8. p.sendMessage(PREFIX + "Usage = /inventory -o <player>");
    9. }
    10. else if(args.length == 2) {
    11. if(p.hasPermission("inventorymenu.open")) {
    12. Player target = Bukkit.getServer().getPlayer(args[1]);
    13. if(target == null) {
    14. p.sendMessage(PREFIX + "Could not find player " + args[1]);
    15. return true;
    16. }
    17. else GUI.getGUIClass().openGadgetGUI(target, p); //LINE 52
    18. }
    19. }
    20. }
    21. return true;


    Stack trace:

    http://gyazo.com/3ebba219bd597b47892cac3394bcecdd

    GUI class:

    Code:java
    1. package org.zodiactheories.healthmenu;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.inventory.InventoryClickEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.scheduler.BukkitRunnable;
    17. public class GUI implements Listener {
    18.  
    19. private static GUI guiclass;
    20.  
    21. public static GUI getGUIClass() {
    22. return guiclass;
    23. }
    24.  
    25. public final List<Player> sender = new ArrayList<>();
    26. public final List<Player> target = new ArrayList<>();
    27.  
    28. public void openGadgetGUI(Player p, Player guisender) {
    29.  
    30. Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "Inventory 1");
    31.  
    32. ItemStack helmet = p.getInventory().getHelmet();
    33. ItemStack chestplate = p.getInventory().getChestplate();
    34. ItemStack leggings = p.getInventory().getLeggings();
    35. ItemStack boots = p.getInventory().getBoots();
    36. ItemStack potion = new ItemStack(Material.POTION);
    37. ItemMeta potionMeta = potion.getItemMeta();
    38.  
    39. potionMeta.setDisplayName(ChatColor.GOLD + p.getActivePotionEffects().toString());
    40. potion.setItemMeta(potionMeta);
    41. int health = (int) p.getHealth();
    42. int hunger = (int) p.getFoodLevel();
    43.  
    44. ItemStack hearts = new ItemStack(Material.GOLD_NUGGET, health);
    45. ItemStack food = new ItemStack(Material.RAW_FISH, hunger);
    46.  
    47. ItemStack nextgui = new ItemStack(Material.REDSTONE);
    48. ItemMeta meta = nextgui.getItemMeta();
    49. meta.setDisplayName(ChatColor.GREEN + "Click me to go to the Player's inventory!");
    50. nextgui.setItemMeta(meta);
    51.  
    52. inv.setItem(0, helmet);
    53. inv.setItem(1, chestplate);
    54. inv.setItem(2, leggings);
    55. inv.setItem(3, boots);
    56. inv.setItem(4, nextgui);
    57. inv.setItem(6, potion);
    58. inv.setItem(7, hearts);
    59. inv.setItem(8, food);
    60. guisender.openInventory(inv);
    61. target.add(p);
    62. sender.add(guisender);
    63. }


    Thanks
     
  2. Offline

    Rocoty

    Doesn't look like you are assigning a value to guiclass...side note: any particular reason you are using a singleton pattern here?
     
  3. Offline

    theguynextdoor

    Is your guiclass variable initialised somewhere?
     
  4. Offline

    Necrodoom

    You never define guiclass.

    EDIT: Oh god, double ninja'd.
     
    Rocoty likes this.
  5. Offline

    ZodiacTheories

Thread Status:
Not open for further replies.

Share This Page