NPE and lore help

Discussion in 'Plugin Development' started by drpk, Aug 13, 2014.

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

    drpk

    I'm getting an NPE on this line even though I check if the lore is null... And before that the lore wouldn't add Speed 2
    NPE:
    Code:java
    1. for (String lore1 : p.getItemInHand().getItemMeta().getLore()) {
    2. if (lore1 != null) {
    3.  
    4. }
    5. }

    Full:
    Code:java
    1. Bukkit.broadcastMessage("1");
    2. if (lore1.contains(ChatColor.AQUA + "Speed!")) {
    3. Bukkit.broadcastMessage("2");
    4. lore.addAll(im.getLore());
    5. lore.remove(ChatColor.AQUA + "Speed!");
    6. lore.add(ChatColor.AQUA + "Speed 2");
    7. meta.setLore(lore);
    8. hand.setItemMeta(meta);
    9. item.getItemMeta().getLore();
    10.  
    11. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    12. p.updateInventory();
    13.  
    14.  
    15. return;
    16. }
    17.  
    18.  
    19. if (meta.hasLore()) {
    20. Bukkit.broadcastMessage("3");
    21. if (!lore1.contains(ChatColor.AQUA + "Speed!")) {
    22. if (!lore1.contains(ChatColor.AQUA + "Speed 5")) {
    23. if (!lore1.contains(ChatColor.AQUA + "Speed 4")) {
    24. if (!lore1.contains(ChatColor.AQUA + "Speed 3")) {
    25. if (!lore1.contains(ChatColor.AQUA + "Speed 2")) {
    26.  
    27. Bukkit.broadcastMessage("4");
    28. lore.addAll(im.getLore());
    29. lore.add(ChatColor.AQUA + "Speed!");
    30. meta.setLore(lore);
    31. hand.setItemMeta(meta);
    32.  
    33. Bukkit.broadcastMessage("5");
    34. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    35. p.updateInventory();
    36. return;
    37. }
    38. }
    39. }
    40. }
    41. }
    42. }
    43.  
    44.  
    45. if (!meta.hasLore()) {
    46.  
    47.  
    48. lore.add(ChatColor.AQUA + "Speed!");
    49. meta.setLore(lore);
    50. hand.setItemMeta(meta);
    51. item.getItemMeta().getLore();
    52. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    53. p.updateInventory();
    54.  
    55.  
    56. return;
    57. }
    58.  
    59. }

    stacktrace:
    http://pastebin.com/qm9Wmg18
     
  2. Offline

    mythbusterma

    drpk

    Because you're trying to iterate over null. getLore() can return null.
     
  3. Offline

    austinv11

    You need to check whether the item in the player's hand is null, if not, whether is has metadata, and finally, whether that metadata has lore.
    It would be something like this:
    Code:java
    1. if (p.getItemInHand() != null){
    2. if (p.getItemInHand().hasItemMeta()){
    3. if (p.getItemInHand().getItemMeta().hasLore()){
    4. //Your code here...
    5. }
    6. }
    7. }
     
  4. Offline

    drpk

    Strange, I did both, but I'm still getting the error even when I remove the
    Code:java
    1. lore != null
     
  5. Offline

    Necrodoom

    drpk paste edited code.
     
  6. Offline

    drpk

    Necrodoom
    Code:java
    1. ItemStack item = p.getItemInHand();
    2. ItemMeta im = item.getItemMeta();
    3. if (item != null) { // So you don't get errors in the console about NullErrorException
    4.  
    5. if (p.getInventory().contains(Material.BLAZE_ROD, 4)) {
    6.  
    7. ItemStack hand = p.getItemInHand();
    8. ItemMeta meta = hand.getItemMeta();
    9. ItemStack is = new ItemStack(Material.BLAZE_ROD, 4);
    10. ItemMeta isMeta = is.getItemMeta();
    11.  
    12. ArrayList<String> lore = new ArrayList<String>();
    13. for (String lore1 : p.getItemInHand().getItemMeta().getLore()) {
    14. if (lore1.contains(ChatColor.AQUA + "Speed!")) {
    15. lore.addAll(im.getLore());
    16. lore.remove(ChatColor.AQUA + "Speed!");
    17. lore.add(ChatColor.AQUA + "Speed 2");
    18. meta.setLore(lore);
    19. hand.setItemMeta(meta);
    20. item.getItemMeta().getLore();
    21.  
    22. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    23. p.updateInventory();
    24.  
    25.  
    26. return;
    27. }
    28.  
    29.  
    30. if (meta.hasLore()) {
    31. if (!lore1.contains(ChatColor.AQUA + "Speed!")) {
    32. if (!lore1.contains(ChatColor.AQUA + "Speed 5")) {
    33. if (!lore1.contains(ChatColor.AQUA + "Speed 4")) {
    34. if (!lore1.contains(ChatColor.AQUA + "Speed 3")) {
    35. if (!lore1.contains(ChatColor.AQUA + "Speed 2")) {
    36. lore.addAll(im.getLore());
    37. lore.add(ChatColor.AQUA + "Speed!");
    38. meta.setLore(lore);
    39. hand.setItemMeta(meta);
    40.  
    41.  
    42. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    43. p.updateInventory();
    44.  
    45. return;
    46. }
    47.  
    48.  
    49. if (!meta.hasLore()) {
    50.  
    51.  
    52. lore.add(ChatColor.AQUA + "Speed!");
    53. meta.setLore(lore);
    54. hand.setItemMeta(meta);
    55. item.getItemMeta().getLore();
    56. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    57. p.updateInventory();
    58.  
    59.  
    60. return;
    61. }
    62. }
    63. }
     
  7. Offline

    Necrodoom

    drpk your entire code paste executes only if iteminhand is null..
    Read your own code and fix the errors, you have a lot of logic errors there.
     
  8. Offline

    drpk

    Necrodoom doesn't
    Code:java
    1. lore ! = null

    make it so it can't return null?
     
  9. Offline

    Necrodoom

    drpk oh, darn, sorry, misread your code.
    However, you use the if check too late, as you already try to get the iteminhand meta, which would NPE if item is null.

    You also have duplicated the variables from lines 1-2 in lines 7-8, and you have an useless code line on line 20. Fix these errors, recheck your logic, and if you still get issues, repasts code.
     
  10. Offline

    drpk

    Necrodoom apparently the only time it works is when the item already has a lore, and even then, it just keeps adding speed :(
    Code:java
    1. ItemStack item = p.getItemInHand();
    2.  
    3.  
    4.  
    5. if (item != null) { // So you don't get errors in the console about NullErrorException
    6.  
    7. if (p.getInventory().contains(Material.BLAZE_ROD, 4)) {
    8. ItemMeta meta = item.getItemMeta();
    9. ItemStack is = new ItemStack(Material.BLAZE_ROD, 4);
    10. ItemMeta isMeta = is.getItemMeta();
    11.  
    12.  
    13. ArrayList<String> lore = new ArrayList<String>();
    14. for (String lore1 : p.getItemInHand().getItemMeta().getLore()) {
    15. if (lore1.contains(ChatColor.AQUA + "Speed!")) {
    16.  
    17. lore.addAll(meta.getLore());
    18. lore.remove(ChatColor.AQUA + "Speed!");
    19. lore.add(ChatColor.AQUA + "Speed 2");
    20. meta.setLore(lore);
    21. item.setItemMeta(meta);
    22.  
    23. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    24. p.updateInventory();
    25.  
    26.  
    27. return;
    28. }
    29.  
    30.  
    31. if (meta.hasLore()) {
    32.  
    33. if (!lore1.contains(ChatColor.AQUA + "Speed!")) {
    34. if (!lore1.contains(ChatColor.AQUA + "Speed 5")) {
    35. if (!lore1.contains(ChatColor.AQUA + "Speed 4")) {
    36. if (!lore1.contains(ChatColor.AQUA + "Speed 3")) {
    37. if (!lore1.contains(ChatColor.AQUA + "Speed 2")) {
    38.  
    39.  
    40. lore.addAll(meta.getLore());
    41. lore.add(ChatColor.AQUA + "Speed!");
    42. meta.setLore(lore);
    43. item.setItemMeta(meta);
    44.  
    45.  
    46. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    47. p.updateInventory();
    48.  
    49. return;
    50. }
    51.  
    52.  
    53. if (!meta.hasLore()) {
    54.  
    55.  
    56. lore.add(ChatColor.AQUA + "Speed!");
    57. meta.setLore(lore);
    58. item.setItemMeta(meta);
    59. item.getItemMeta().getLore();
    60. p.getInventory().removeItem(new ItemStack(Material.BLAZE_ROD, 4));
    61. p.updateInventory();
    62.  
    63.  
    64. return;
    65. }
    66.  
    67. }
    68. }
    69. }
     
  11. Offline

    drpk

    Help! bump.
     
  12. Offline

    drpk

  13. Offline

    AoH_Ruthless

    drpk
    Read other replies.
     
  14. Offline

    drpk

    okay, I fixed the NPE of those lines but those caused two more NPEs at these two lines. They are both the first in a series of if statements.
    Code:java
    1. if (!lore.isEmpty()) {
    2. if (lore.contains(ChatColor.GREEN + "NIght Vision!")) {
    3.  

    and
    Code:java
    1. if (lore1.contains(ChatColor.AQUA + "Speed 5")) {
    2. [syntax=java][/syntax]
     
Thread Status:
Not open for further replies.

Share This Page