Solved How to get int value in HashMap

Discussion in 'Spigot Plugin Development' started by SudanoDev, Oct 12, 2017.

  1. Hello everybody!

    Well, i want to know how can i get the integer value of a hashmap.

    Code (Text):
    public static HashMap<Player, Integer> moedas = new HashMap<>();
    Code (Text):
        public static int getMoedas(Player p) {
            return Main.moedas.get(p).intValue();
        }
    Error:
    upload_2017-10-12_12-45-7.png
    Async:
    Code (Text):
                ServerListeners.addMoedas(p, 1);
    'addMoedas'​
    Code (Text):
        public static void addMoedas(Player p, int quantia) {
            Main.moedas.put(p, getMoedas(p) + quantia);
        }
    OBS: Sorry for my bad english, i'm brazilian.
     
  2. Code (Text):
    Main.moedas
    Might be null.

    It's a NullPointerException, just log each reference to see what returns null.
     
  3. bro, i'm so noob in bukkit, what i need to do? =(
     
  4. Code (Text):
    if(Main.moedas == null) {
      System.out.println("OMG IT'S NULL");
    }
     
  5. thanks, but where i put?
     
  6. Before you do anything with it. In your case, before you get the player's integer value

    Sent from my SM-G903F using Tapatalk
     
  7. To my point of view what happens is that you can not use the put if you have already established the player, then what I recommend you do is this: (I have something similar with hashmaps and I work this way and it works me perfect)
    Code (Text):

        public static HashMap<Player, Integer> coins = new HashMap<Player, Integer>();
     
        public static int getCoins(Player p) {
            int integer;
            if (coins.containsKey(p)) {
                integer = coins.get(p);
            } else {
                integer = 0;
            }
            return integer;
        }

     
        public static void setCoins(Player p, int amount) {
         
            if (coins.containsKey(p)) {
                int a = getCoins(p);
                coins.remove(p);
                coins.put(p, amount + a);
            } else {
                coins.put(p, amount);
    }
     
     
  8. and, how i make the 'addCoins' & 'removeCoins'
     
  9. Learn Java before learning how to make Minecraft Plugins.

    There are many reasons why you should do this, too many to name in fact. Just find a Java tutorial and learn the language - it will make your plugins better in general and also make it easier for you to make them.

    Learn Java BEFORE Making Plugins!
     
    • Agree Agree x 1
  10. I recommend https://sololearn.com

    Sent from my SM-G903F using Tapatalk
     
    • Like Like x 1
  11. This would be a remove coins, and the addCoins is the same as the setCoins, look how the method works
    Code (Text):
        public static void removeCoins(Player p, int amount) {
           
            if (coins.containsKey(p)) {
                int integer = getCoins(p) - amount;
                coins.remove(p);
                coins.put(p, integer);
            }
        }
     
    • Optimistic Optimistic x 1
    • Don't abuse static to create accessible fields. Create an instance of whatever managed the Map and pass it around.
    • Public fields should be rare (and if so, the field should be final and the object immutable - or a primitive which doesn't contain state anyway)
    • Use abstractions for fields and parameter types, rather than concrete classes (Map rather than HashMap, List rather than ArrayList).
      • The only reason to ever use a more explicit type, is to expose some additional methods which you need access to. In this case, HashMap doesn't even add methods.
    • To provide a default value for when a key doesn't have a value associated with it, use getOrDefault (surprise ;D!).
      • Want to insert it as well? computeIfAbsent is your function.
     
    • Agree Agree x 1
  12. You need to handle the initial state of there not being money in your hashmap, and return 0 there. Otherwise addMoedas will fail, because it calls getMoedas to get the previous value, which would throw an NPE.

    Code (Text):

            public static int getMoedas(Player p) {
                Integer intMoedas = Main.moedas.get(p);
                return (intMoedas == null) ? 0 : intMoedas.intValue();
            }
     
    The ? is a conditional operator. Its the same as if (intMoedas == null) return 0 else return intMoedas.intValue(). It is handy to switch values you return.

    People learn java while making plugins, you have to start somewhere. I know its painful but please don't tell people to stop programming because they can't program. Thats dumb advice. Passive-aggressively linking the java introduction manual would be more productive.
     
    #13 TeamBergerhealer, Oct 13, 2017
    Last edited: Oct 13, 2017
  13. @TeamBergerhealer you recommend static abuse.
    Second your method getMoedas will never use real values. Instead insert an default value. When using Java 8 use the method @DarkSeraphim recommended.


    Do what @DarkSeraphim recommended.
     
  14. Telling people to stop spamming "dont use static" in every thread is not recommending it. It adds nothing to the conversation to keep bringing it up. I find this self-righteous crusade to point out static in threads it has no value to bring it up petty.

    Answer people's questions, don't comment on the stank of their code. His problems are not resolved by dependency injection or other meme term.

    Save your 'dont use static' for the "how do I access x in class y" threads, for which there already is an endless supply.
     
    #15 TeamBergerhealer, Oct 13, 2017
    Last edited: Oct 13, 2017

Share This Page