Random Item Giving null pointer

Discussion in 'Spigot Plugin Development' started by Nutowen, May 11, 2017.

  1. https://hastebin.com/uvogegatac.apache
    Code (Text):

    Random rand = new Random();
                            rand.nextInt(100);
                            int determined_chance = rand.nextInt(100);
                            // 1 because it starts from 0
                            ItemStack[] armorArray = InvCLickAdmin.getInstance().invC.getContents();
                            int id = 0;
                            for(ItemStack item : armorArray){
                                int i = Main.inst().getConfig().getInt("Common.Items." + id + ".percentage", AddItem.getInstance().items.get(item));
                                p.sendMessage("works");
                                if(determined_chance <= i){
                                    p.getInventory().addItem(InvCLickAdmin.getInstance().invC.getItem(0));
                                   
                                }
                            }
     
     
  2. I'm really not sure what's going on here. Could you please provide more context, ie: what you're trying to do. Also which line is 87?
     
  3. Yeah show us line 87
    And avoid using new Random()

    Instead use
    chance = ThreadLocalRandom().current().nextInt(1,100);

    Since minecraft is single threaded creating new instances of random often can create contingency, extra overhead and repetition of numbers so use the single threaded solution which is ThreadLocalRandom


    Sent from my iPhone using Tapatalk
     
  4. Line 87: int i = Main.inst().getConfig().getInt("Common.Items." + id + ".percentage", AddItem.getInstance().items.get(item));
     
  5. Check for errors in your config, and check to see if id is ever empty


    Sent from my iPhone using Tapatalk
     
  6. There are no errors in config
     
  7. Post it please. Also, are you sure you're fetching the correct config file?
     
  8.  
    #8 Nutowen, May 11, 2017
    Last edited: May 11, 2017
  9. And if that item passed to the Id does not exist you'll end up with an error as well. Perhaps the player is wearing an item that does not exist in the config?

    Previous poster means post the config.


    Sent from my iPhone using Tapatalk
     
  10. Common:
    Items:
    '0':
    item:
    ==: org.bukkit.inventory.ItemStack
    type: DIAMOND_SWORD
    percentage: 100
    '1': {}
    '2': {}
    '3': {}
    '4': {}
    '5': {}
    '6': {}
    '7': {}
    '8': {}
     
  11. That's the only item in the config? If so having any item other in the inventory other than a diamond_sword would not be findable in the config. If you don't handle that possibility you're going to get errors searching for the config for items that don't exist. Ie IRON_SWORD


    Sent from my iPhone using Tapatalk
     
    #11 dNiym, May 11, 2017
    Last edited: May 11, 2017