Solved Buying Items with Custom Economy

Discussion in 'Plugin Development' started by Creeper674, Oct 1, 2014.

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

    Creeper674

    Hello all!

    I have a small economy which uses a sign to allow users to purchase items using the custom points currency I have created. However, when the specified item ID is a item, and not a block it returns a null pointer exception. Stack trace is here:

    Show Spoiler


    Here is the piece of code which is relevant.

    Code:java
    1. @EventHandler
    2. public void onSign(PlayerInteractEvent event) {
    3. Player p = event.getPlayer();
    4. Block clicked = event.getClickedBlock();
    5. if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    6. if (clicked.getState() instanceof Sign) {
    7. Sign s = (Sign) event.getClickedBlock().getState();
    8.  
    9. if (s.getLine(0).equals("<Buy>")) {
    10. if (!(s.getLine(1).equals(null))) {
    11. if (!(s.getLine(2).equals(null))) {
    12. if (!(s.getLine(3).equals(null))) {
    13. int id = Integer.parseInt(s.getLine(1));
    14. Material item = Material.getMaterial(id);
    15. int amount = Integer.parseInt(s.getLine(2));
    16. int cost = Integer.parseInt(s.getLine(3));
    17.  
    18. if (StatsManager.getInstance().getPoints(p) < cost) {
    19. p.sendMessage("§8[§aStore§8] §2You do not have enough points to buy this!");
    20. } else {
    21. p.sendMessage("§8[§aStore§8] §2You have purchased some §aID" + id + " §2for §a" + cost + " §2points!");
    22. StatsManager.getInstance().changePoints(p, -cost);
    23. p.getInventory().addItem(new ItemStack(item, amount));
    24. }
    25. }
    26. }
    27. }
    28. }
    29. }
    30. }


    According to the stack trace, the null pointer is caused by line 32 on the class I have provided which is the following line:

    Code:java
    1. if (StatsManager.getInstance().getPoints(p) < cost) {


    I don't understand how this line has anything to do with a problem which seems to only effect item IDs which correspond to items.

    All suggestions welcome!
     
  2. Offline

    ZeusAllMighty11

    Your StatsManager instance must be null.
     
  3. Offline

    fireblast709

    Creeper674 your code doesn't match your stacktrace. The NPE occurs because you try to create an ItemStack of a null Material.
     
  4. Offline

    Creeper674

    fireblast709 Thanks, but my overall question is why does this error only happen when the item ID on the sign is an item, and not a block. Do items not count as materials, and if not, how can I get an item which has a specific ID?
     
  5. Offline

    fireblast709

  6. Offline

    Creeper674

    fireblast709 Line 1 is the item ID which I then create a new material from here:

    Code:java
    1. int id = Integer.parseInt(s.getLine(1));
    2. Material item = Material.getMaterial(id);
     
  7. Offline

    fireblast709

    Creeper674 can you post the actual sign entry? If the ID is actually an item ID, then it should work fine.
     
  8. Offline

    Creeper674

    fireblast709 Oh my I feel stupid. I was unaware that there is a huge gap in the numbers of ID's between blocks and items, and that that stack trace was because I tried to purchase item 222, which is nothing. My apologies for the waste of time!
     
Thread Status:
Not open for further replies.

Share This Page