Development Assistance NPE.

Discussion in 'Plugin Help/Development/Requests' started by CraftCreeper6, Jan 25, 2015.

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

    CraftCreeper6

    Hello, I am in need of a little help.
    Code:
    public void addWarning(InetSocketAddress inet)
    {
            if(!bannedIPs.contains(inet + ".Warnings"))
    {
            bannedIPs.set(inet + ".Warnings", 1);
            }
     else 
            {
            for(Player player : Bukkit.getOnlinePlayers())
            {
                if(player.getAddress() == inet)
                {
                player.setBanned(true);
                }
            }
            }
            save();
        }
    Line 2 is where I get the NPE.

    Any help is appreciated.
     
  2. Offline

    WesJD

    @CraftCreeper6
    Code:
    InetSocketAddress.getAddress().getHostAddress();
     
  3. Offline

    CraftCreeper6

    @WesJD
    There is no getHostAddress() method.
     
  4. Offline

    Nateb1121

    Could I see the stack trace please?
     
  5. Offline

    CraftCreeper6

    @Nateb1121
    Code:
    [22:47:19 ERROR]: Could not pass event PlayerChatEvent to Security v0.0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:302) ~[spigot.jar:git-Spigot-1647]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-1647]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:514) [spigot.jar:git-Spigot-1647]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:499) [spigot.jar:git-Spigot-1647]
            at net.minecraft.server.v1_7_R4.PlayerConnection$4.evaluate(PlayerConnec
    tion.java:974) [spigot.jar:git-Spigot-1647]
            at org.bukkit.craftbukkit.v1_7_R4.util.Waitable.run(Waitable.java:24) [s
    pigot.jar:git-Spigot-1647]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:6
    47) [spigot.jar:git-Spigot-1647]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [spigot.jar:git-Spigot-1647]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot.jar:git-Spigot-1647]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot.jar:git-Spigot-1647]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1647]
    Caused by: java.lang.NullPointerException
            at com.lemoncube.managers.BlockedIPList.addWarning(BlockedIPList.java:78
    ) ~[?:?]
            at com.lemoncube.listeners.ChatManager.onChat(ChatManager.java:47) ~[?:?
    ]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _25]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _25]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_25]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:298) ~[spigot.jar:git-Spigot-1647]
            ... 10 more
     
  6. Offline

    Nateb1121

    Where do you instantiate bannedIPs? I'm guessing it's not instantiated.
     
  7. Offline

    CraftCreeper6

    @Nateb1121
    In the ChatManager class.
    BlockedIPList blockedIP = new BlockedIPList(main);
     
  8. Offline

    Nateb1121

    I can't 100% say without seeing the whole control flow but I'm pretty sure it's not instantiated when you're calling the addWarning() method.
     
  9. Offline

    CraftCreeper6

    @Nateb1121
    Code:
    import java.net.InetSocketAddress;
    import java.util.HashMap;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.lemoncube.Main;
    import com.lemoncube.managers.BlockedIPList;
    
    @SuppressWarnings("deprecation")
    public class ChatManager
        implements Listener
    {
    
        private Main main;
        public ChatManager(Main ins){
            this.main = ins;
        }
       
        BlockedIPList blockedIP = new BlockedIPList(main);
       
        private HashMap<InetSocketAddress, Integer> chat = new HashMap<>();
       
        @EventHandler
        public void onChat(PlayerChatEvent e)
        {
           
            Player player = e.getPlayer();
           
            final InetSocketAddress ipAddress = player.getAddress();
               
            if(chat.containsKey(ipAddress))
            {   
                if(chat.get(ipAddress) == 3)
                {
                    player.sendMessage(ChatColor.RED + "If you continue to spam you will be banned!");
                    e.setCancelled(true);
                }
                else if(chat.get(ipAddress) > 3)
                {
                    blockedIP.addWarning(ipAddress);
                    player.kickPlayer(ChatColor.RED + "Stop spamming!");
                    e.setCancelled(true);
                }
            }
            else
            {
            chat.put(ipAddress, 1);
            }
               
                chat.put(ipAddress, chat.get(ipAddress)+1);
               
                new BukkitRunnable()
                {
                @Override
                public void run()
                {
                if(chat.containsKey(ipAddress))
                {
                    chat.remove(ipAddress);
                }
                }       
                }.runTaskLater(main, 20*8);
        }
       
    }
     
  10. Offline

    Nateb1121

    Can I see your BlockedIPList class, please.
     
  11. Offline

    CraftCreeper6

    @Nateb1121
    Code:
    package com.lemoncube.managers;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    
    public class BlockedIPList
    {
    
        public File ban;
        public FileConfiguration bannedIPs;
       
        /* List */
        private List<InetSocketAddress> bannedIP = new ArrayList<InetSocketAddress>();
       
        public void add(InetSocketAddress inet)
        {bannedIP.add(inet);}
       
        public void remove(InetSocketAddress inet)
        {if(bannedIP.contains(inet)){bannedIP.remove(inet);}}
       
        public boolean contains(InetSocketAddress inet){return bannedIP.contains(inet);}
       
        /* Configuration */   
        @SuppressWarnings("unchecked")
        public void input()
        {
            List<InetSocketAddress> list = new LinkedList<InetSocketAddress>();
            list.addAll((List<InetSocketAddress>) bannedIPs.getList("Banned.List"));
            for(InetSocketAddress inet : bannedIP)
            {
                if(list.contains(inet)) continue;
                list.add(inet);
            }
            bannedIPs.set("Banned.List", list);
            save();
        }
       
        private void save()
        {
            try
            {
            bannedIPs.save(ban);
            }
            catch (IOException e)
            {
            System.out.append("Unable so save configuration. "
                + "This could be a bad thing."
                + " Please re-install Security.jar immediately!");
            }
        }
       
        @SuppressWarnings("deprecation")
        public void addWarning(InetSocketAddress inet)
            {
            if(!bannedIPs.contains(inet + ".Warnings"))
            {
            bannedIPs.set(inet + ".Warnings", 1);
            }
                else
                {
            for(Player player : Bukkit.getOnlinePlayers())
                {
                if(player.getAddress() == inet){
                player.setBanned(true);
                }
            }
            }
            save();
        }
       
    }
     
  12. Offline

    Nateb1121

    Yep, I'm correct. Line 18, you make 'bannedIPs' but you never say what it is.
     
  13. Offline

    CraftCreeper6

    Code:
    public void loadConfiguration()
        {
            BlockedIPList ipList = new BlockedIPList();
            ipList.ban = new File(getDataFolder(), "BannedIPs.yml");
            ipList.bannedIPs = YamlConfiguration.loadConfiguration(ipList.ban);
        }
    @Nateb1121
    Main class ;p
     
  14. Offline

    Nateb1121

    Well now you've got me. Dang. I'm gonna keep looking. Haha
     
  15. Offline

    CraftCreeper6

    @Nateb1121
    xD I really don't know, maybe I don't have an IP xD
     
    Nateb1121 likes this.
  16. Offline

    Nateb1121

    Hm, my only guess is you don't call loadConfiguration method? Try adding a log statement in it to see if it's actually called.


    Why was this thread moved?
     
  17. Offline

    CraftCreeper6

    @Nateb1121
    It was moved because it's Spigot ;p It's for my server so I have to use Spigot. (BungeeCoord).

    EDIT: It doesn't generate the config.
     
  18. Offline

    WesJD

    @Nateb1121 Because he's using spigot.

    edit: ninja'd
     
    CraftCreeper6 likes this.
  19. Offline

    Nateb1121

    Ah! Duh, should of seen that in the stack trace. Haha. Man, this one baffles me. Loadconfiguration() is the only thing I'm thinking about right now...

    My favorite line:
     
  20. Offline

    WesJD

  21. Offline

    CraftCreeper6

  22. Offline

    WesJD

    @CraftCreeper6
    Change InetSocketAddress to a string and use the toString method?
     
  23. Offline

    Nateb1121

    The error isn't in getting inet, it's something with the reference to bannedIPs.
     
  24. Offline

    CraftCreeper6

    @WesJD
    Okay, I managed to get it to work. Although, when using getAddress().getHostAddress(); it seems to return my IPv4 address, except, I do not want this. I want the players actual address, otherwise, lots of people are going to get banned. ;p

    @Nateb1121
     
Thread Status:
Not open for further replies.

Share This Page