Solved java.lang.Long cannot be cast to java.lang.Integer

Discussion in 'Plugin Development' started by Panda_Crafter, Jul 29, 2016.

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

    Panda_Crafter

    How do I fix this error? I tried to change every Integer and int into Long but that doesn't work

    The Code:
    Code:
        public static void baltop(Player sender, int top) {
          
            sender.sendMessage(ChatColor.GREEN + "================== TOP " + top + " ==================");
          
            class ValueComparator implements Comparator<String> {
    
                Map<String, Integer> base;
                public ValueComparator(HashMap<String, Integer> map) {
                    this.base = map;
                }
    
                public int compare(String a, String b) {
                    if (base.get(a) >= base.get(b)) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
    
    
            ValueComparator bvc =  new ValueComparator(getValues());
            TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(bvc);
             sorted_map.putAll(getValues());
    
            for(int i = 1; i < top+1; i ++){
                Entry<String, Integer> e = sorted_map.pollFirstEntry();
                String pname = e.getKey();
                int score = e.getValue();
                sender.sendMessage(ChatColor.YELLOW + "" + i + ". " + Bukkit.getOfflinePlayer(UUID.fromString(pname)).getName() + ": " + score + " SC");
            }
    
        }
      
        public static HashMap<String, Integer> getValues() {
            Map<String, Object> map = Main.config.getConfigurationSection("Market").getValues(true);
            HashMap<String, Integer> val = new HashMap<String, Integer>();
          
            for (Entry<String, Object> e : map.entrySet()) {
    //            Bukkit.getConsoleSender().sendMessage(e.getKey() + e.getValue());
                val.put(e.getKey(),  (Integer) e.getValue());
            }
          
            return val;
        }
    

    The error:
    Code:
    [00:10:46 ERROR]: Could not pass event AsyncPlayerChatEvent to NonsenseCraft v1.
    0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:484) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.chat(PlayerConnection.
    java:1273) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.jav
    a:1211) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat$1.run(PacketPlayInChat
    .java:39) [spigot.jar:git-Spigot-9797151-301db84]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_66]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_66]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:
    1.8.0_66]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?
    :1.8.0_66]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to java.l
    ang.Integer
            at me.pandacrafter1.nonsensecraft.market.MarketManager.getValues(MarketM
    anager.java:169) ~[?:?]
            at me.pandacrafter1.nonsensecraft.market.MarketManager.baltop(MarketMana
    ger.java:150) ~[?:?]
            at me.pandacrafter1.nonsensecraft.events.player.ChatRecieve.onChatReciev
    e(ChatRecieve.java:277) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _66]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _66]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_66]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_66]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot.jar:git-Spigot-9797151-301db84]
            ... 11 more
    
     
  2. Offline

    Zombie_Striker

    @Panda_Crafter
    Use Integer.parseInt(long) to turn a long into an integer.
     
  3. Offline

    Panda_Crafter

    Integer.parseInt() is only supported for strings and not long
     
  4. Offline

    Zombie_Striker

    @Panda_Crafter
    Then either use "Integer.valueOf()" or convert the long to a string using String.valueOf()
     
  5. Offline

    Panda_Crafter

    @Zombie_Striker
    New error:
    Code:
    [00:38:07 ERROR]: Could not pass event AsyncPlayerChatEvent to NonsenseCraft v1.
    0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:484) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.chat(PlayerConnection.
    java:1273) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.jav
    a:1211) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat$1.run(PacketPlayInChat
    .java:39) [spigot.jar:git-Spigot-9797151-301db84]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_66]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_66]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:
    1.8.0_66]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?
    :1.8.0_66]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to jav
    a.lang.String
            at me.pandacrafter1.nonsensecraft.market.MarketManager.getValues(MarketM
    anager.java:169) ~[?:?]
            at me.pandacrafter1.nonsensecraft.market.MarketManager.baltop(MarketMana
    ger.java:150) ~[?:?]
            at me.pandacrafter1.nonsensecraft.events.player.ChatRecieve.onChatReciev
    e(ChatRecieve.java:277) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _66]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _66]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_66]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_66]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot.jar:git-Spigot-9797151-301db84]
            ... 11 more
    
     
  6. Offline

    Oxyorum

    @Panda_Crafter

    Use Long.valueOf(x).intValue() to convert primitive long values to int.
     
    Last edited: Jul 29, 2016
  7. Offline

    Panda_Crafter

    When I do, I get this error
    Code:
    [00:45:52 ERROR]: Could not pass event AsyncPlayerChatEvent to NonsenseCraft v1.
    0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:484) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.chat(PlayerConnection.
    java:1273) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.jav
    a:1211) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat$1.run(PacketPlayInChat
    .java:39) [spigot.jar:git-Spigot-9797151-301db84]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_66]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_66]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:
    1.8.0_66]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?
    :1.8.0_66]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to jav
    a.lang.String
            at me.pandacrafter1.nonsensecraft.market.MarketManager.getValues(MarketM
    anager.java:169) ~[?:?]
            at me.pandacrafter1.nonsensecraft.market.MarketManager.baltop(MarketMana
    ger.java:150) ~[?:?]
            at me.pandacrafter1.nonsensecraft.events.player.ChatRecieve.onChatReciev
    e(ChatRecieve.java:277) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _66]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _66]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_66]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_66]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot.jar:git-Spigot-9797151-301db84]
            ... 11 more
    
    EDIT:
    I pasted the wrong error
     
  8. Offline

    Oxyorum

    @Panda_Crafter EDIT: I am blind. Never mind. xD

    I am not sure why you are trying to cast Long to String. Update your code.
     
    Last edited: Jul 29, 2016
  9. Offline

    Panda_Crafter

    Because Long.valueOf(x) requires the x to be a String
     
  10. Offline

    FisheyLP

    Long -> Integer:
    Code:
    .intValue();
    Integer -> Long:
    Code:
    .longValue();
    Really simple actually :)
     
  11. Offline

    Oxyorum

  12. Offline

    Panda_Crafter

    When I convert Long to Integer, I get this error.

    Code:
    [01:01:04 ERROR]: Could not pass event AsyncPlayerChatEvent to NonsenseCraft v1.
    0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-9797151-301db84]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:484) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.chat(PlayerConnection.
    java:1273) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.jav
    a:1211) [spigot.jar:git-Spigot-9797151-301db84]
            at net.minecraft.server.v1_10_R1.PacketPlayInChat$1.run(PacketPlayInChat
    .java:39) [spigot.jar:git-Spigot-9797151-301db84]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_66]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_66]
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:
    1.8.0_66]
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?
    :1.8.0_66]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_66]
    Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to jav
    a.lang.Long
            at me.pandacrafter1.nonsensecraft.market.MarketManager.getValues(MarketM
    anager.java:169) ~[?:?]
            at me.pandacrafter1.nonsensecraft.market.MarketManager.baltop(MarketMana
    ger.java:150) ~[?:?]
            at me.pandacrafter1.nonsensecraft.events.player.ChatRecieve.onChatReciev
    e(ChatRecieve.java:277) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _66]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _66]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_66]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_66]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot.jar:git-Spigot-9797151-301db84]
            ... 11 more
    
     
  13. Offline

    FisheyLP

    Show your updated code. I'm sure you are casting somewhere instead of using those methods.
     
  14. Offline

    Oxyorum

    @Panda_Crafter Care to give us an update of your code so we can see what the issue is? @FisheyLP beat me to the punch. :)
     
  15. Offline

    Panda_Crafter

    @Oxyorum @FisheyLP
    Code:
        public static void baltop(Player sender, int top) {
           
            sender.sendMessage(ChatColor.GREEN + "================== TOP " + top + " ==================");
           
            class ValueComparator implements Comparator<String> {
    
                Map<String, Integer> base;
                public ValueComparator(HashMap<String, Integer> map) {
                    this.base = map;
                }
    
                public int compare(String a, String b) {
                    if (base.get(a) >= base.get(b)) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
    
    
            ValueComparator bvc =  new ValueComparator(getValues());
            TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(bvc);
             sorted_map.putAll(getValues());
    
            for(int i = 1; i < top+1; i ++){
                Entry<String, Integer> e = sorted_map.pollFirstEntry();
                String pname = e.getKey();
                int score = e.getValue();
                sender.sendMessage(ChatColor.YELLOW + "" + i + ". " + Bukkit.getOfflinePlayer(UUID.fromString(pname)).getName() + ": " + score + " SC");
            }
    
        }
       
        public static HashMap<String, Integer> getValues() {
            Map<String, Object> map = Main.config.getConfigurationSection("Market").getValues(true);
            HashMap<String, Integer> val = new HashMap<String, Integer>();
           
            for (Entry<String, Object> e : map.entrySet()) {
    //            Bukkit.getConsoleSender().sendMessage(e.getKey() + e.getValue());
                val.put(e.getKey(),  ((Long) e.getValue()).intValue());
            }
           
            return val;
        }
     
  16. Offline

    bwfcwalshy Retired Staff

    @Panda_Crafter Change the entry from Object to Integer and remove all the casting and intValud
     
  17. Offline

    Panda_Crafter

    Well, a friend of me helped me out with the problem. Here is my updated code that solved my problem.

    Code:
        public static void baltop(Player sender, int top) {
           
            sender.sendMessage(ChatColor.GREEN + "================== TOP " + top + " ==================");
           
            class ValueComparator implements Comparator<String> {
    
                Map<String, Long> base;
                public ValueComparator(HashMap<String, Long> map) {
                    this.base = map;
                }
    
                public int compare(String a, String b) {
                    if (base.get(a) >= base.get(b)) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
    
    
            ValueComparator bvc =  new ValueComparator(getValues());
            TreeMap<String, Long> sorted_map = new TreeMap<String, Long>(bvc);
             sorted_map.putAll(getValues());
    
            for(int i = 1; i < top+1; i ++){
                Entry<String, Long> e = sorted_map.pollFirstEntry();
                String pname = e.getKey();
                Long score = e.getValue();
                sender.sendMessage(ChatColor.YELLOW + "" + i + ". " + Bukkit.getOfflinePlayer(UUID.fromString(pname)).getName() + ": " + score + " SC");
            }
    
        }
       
        public static HashMap<String, Long> getValues() {
            HashMap<String, Long> val = new HashMap<String, Long>();
            for(String s : Main.config.getConfigurationSection("Market").getKeys(false)){
                val.put(s, Main.config.getLong("Market." + s));
            }
           
            return val;
        }
    
     
Thread Status:
Not open for further replies.

Share This Page