Solved NullPointerException Help?

Discussion in 'Plugin Development' started by serfma, Mar 10, 2013.

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

    serfma

    I am trying to split up my code between a couple classes which I'm having difficulty doing and I had thought everything was okay but when I use the plugin, whenever a player joins (onPlayerJoin) it throws a NullPointerException on the line;

    Code:
            if ((!playerSide.equals("living")) && (!playerSide.equals("undead")))
    
    Full Code, RedemptionEvents.java:
    Code:
    public class RedemptionEvents implements Listener {
     
        
        // ON PLAYER JOIN
        @EventHandler
        public void playerJoin(PlayerJoinEvent playerJoin)
        {
            Player player = playerJoin.getPlayer();
            String playerSide = Redemption.playerSideConfig.getString("PlayerSide." + player.getName() + ".side");
     
            if ((!playerSide.equals("living")) && (!playerSide.equals("undead")))
            {
                player.teleport(Redemption.livingSpawn);
                Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "Welcome " + player.getName() + ", to hell!");
                Redemption.playerSideConfig.set("PlayerSide." + player.getName() + ".side", "living");
                
     
                try
                {
                    Redemption.playerSideConfig.save(Redemption.configFile);
                }
                catch (Exception ex)
                {
                }
     
            }
     
            //Display join message for Living
            if (playerSide.equals("living"))
            {
                player.sendMessage(ChatColor.DARK_AQUA + "You are: " + ChatColor.AQUA + "Living");
            }
     
            //Display join message for Undead
            if (playerSide.equals("undead"))
            {
                player.sendMessage(ChatColor.DARK_PURPLE + "You are: " + ChatColor.LIGHT_PURPLE + "Undead");
            }
        }
    
    along with every other line that has "playerSide.equals()".

    I have player data stored into PlayerSide.yml and I'm needing to be able to read/write from a different class which is RedemptionEvents.

    The main Class File, Redemption.java;
    Code:
    public class Redemption extends JavaPlugin implements Listener {
     
        //Java Folder/File
        public static File pluginFolder;
        public static File configFile;
        //Bukkit File Config
        public static FileConfiguration playerSideConfig;
        public static Location undeadSpawn = new Location((Bukkit.getWorld("world")), 127.5, 71, 220.5);
        public static Location livingSpawn = new Location((Bukkit.getWorld("world")), 120.5, 71, 226.5);
     
        @Override
        public void onEnable()
        {
            System.out.println("[Redemption] Plugin Enabled!");
            getServer().getPluginManager().registerEvents(this, this);
            getServer().getPluginManager().registerEvents(new RedemptionEvents(), this);
     
            //Java Folder/File for playersides
            pluginFolder = getDataFolder();
            configFile = new File(pluginFolder, "PlayerSide.yml");
     
            //Bukkit new Config File
            playerSideConfig = new YamlConfiguration();
     
            //Create folder if it does not exist
            if (!pluginFolder.exists())
            {
                try
                {
                    pluginFolder.mkdir();
                }
                catch (Exception ex)
                {
                    //
                }
            }
     
            //If config file doesn't exist, try to create a blank file
            if (!configFile.exists())
            {
                try
                {
                    configFile.createNewFile();
                }
                catch (Exception ex)
                {
                    //
                }
            }
     
            loadPlayerData();
        }
     
        @Override
        public void onDisable()
        {
            System.out.println("[Redemption] Plugin Disabled!");
            try
            {
                playerSideConfig.save(configFile);
            }
            catch (Exception ex)
            {
                //
            }
        }
     
        public void loadPlayerData()
        {
            //Load file
            try
            {
                playerSideConfig.load(configFile);
            }
            catch (IOException | InvalidConfigurationException ex)
            {
            }
        }
    }
    
    Just to let you know, I'm a slight beginner and I'm still learning. I've fiddled around for an hour and can't seem to fix it myself.
     
  2. Offline

    sharp237

    I'm guessing playerSide is null then. You need to set playerSide to something or else it will return null, eg:

    Redemption.playerSideConfig.set("PlayerSide." + player.getName() + ".side", "living");

    So try this for RedemtionEvents.java :

    Code:
    public class RedemptionEvents implements Listener {
     
       
        // ON PLAYER JOIN
        @EventHandler
        public void playerJoin(PlayerJoinEvent playerJoin)
        {
            Player player = playerJoin.getPlayer();
            String playerSide = Redemption.playerSideConfig.getString("PlayerSide." + player.getName() + ".side");
            if(playerSide == null)
            {
            return;
            }
            if ((!playerSide.equals("living")) && (!playerSide.equals("undead")))
            {
                player.teleport(Redemption.livingSpawn);
                Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "Welcome " + player.getName() + ", to hell!");
                Redemption.playerSideConfig.set("PlayerSide." + player.getName() + ".side", "living");
               
     
                try
                {
                    Redemption.playerSideConfig.save(Redemption.configFile);
                }
                catch (Exception ex)
                {
                }
     
            }
     
            //Display join message for Living
            if (playerSide.equals("living"))
            {
                player.sendMessage(ChatColor.DARK_AQUA + "You are: " + ChatColor.AQUA + "Living");
            }
     
            //Display join message for Undead
            if (playerSide.equals("undead"))
            {
                player.sendMessage(ChatColor.DARK_PURPLE + "You are: " + ChatColor.LIGHT_PURPLE + "Undead");
            }
        }
     
  3. Offline

    Paper

    Try this:
    Code:
     if ((!playerSide.equalsIgnoreCase("living")) && (!playerSide.equalsIgnoreCase("undead")))
     
  4. Offline

    Darq

    If playerSide is null (which it has to be if that's indeed the line that NPE is being thrown on), it won't matter if you call equals, or equalsIgnoreCase, as you're still calling a method on a null object, thus throwing the NPE.

    However, you can do this:
    Code:
     if ((!"living".equals(playerSide)) && (!"undead".equals(playerSide)))
    which will not throw an NPE as methods are being called on literal String objects which are never null. String's equals methods simply return false if the object passed to them is null.

    Now this isn't really going to help the OP, playerSide being null is probably a completely separate problem, just wanted to point this out :)
     
  5. Offline

    serfma

    So if you see in Redemption.java, the "undeadSpawn" and "livingSpawn", how would I be able to use those variables in RedemptionEvents.java? I don't believe "Redemption.undeadSpawn"/"Redemption.livingSpawn" works lol

    Welp, I just need a way to pass the playerSideConfig and everything to the other class file so that it can read and write the config file/yml file.
     
  6. Offline

    serfma

    Could anyone explain to me what the difference is in these two pieces of code and why the 1st one throws a NullPointerException, yet the 2nd does not?

    Throws NPE @ Line 23:
    http://pastebin.com/aNvV7rgz

    Doesn't throw NPE @ Line 23:
    http://pastebin.com/Ndu3yKtc

    I set the string playerSide to be
    Code:
    Redemption.playerSideConfig.getString("PlayerSide." + player.getName() + ".side");
    
    So that when I use playerSide, shouldn't it be just as if I was using the above piece of code?
     
  7. Offline

    Rprrr

    serfma
    Well, in the case without the NPE, you first check if it's null, if it is, you're adding it to the file (so you're making sure it won't be null the next time you check), and then check again.

    In the case with the NPE, however, you first get the string, then you check if it's null, if it was null you add a string to the file, but you don't respecify the string you got before you did that, so it's still null (because you're working with the string you first got, before you added something to the file to make sure it won't be null if you check again).

    Sorry for the vague words but I can't think of a better way to explain it.
     
  8. Offline

    serfma

    Ooh yes, I understand now. When the player joins, the String playerSide would obviously be null since when they join, they won't have a side. Then once it gets to the 2nd if statement, it checks it and it's null since it doesn't check it again. Correct?
     
  9. Offline

    Rprrr

    serfma
    Yea, that's correct. So to avoid that, you need to re - get a string from the file (like you already did in the code that doesn't throw an NPE at line 23). :)
     
  10. Offline

    serfma

    So, I really wouldn't need to use playerSide at all, would I? I just figured I could set it as a String to make it easier. Hmm.
     
  11. Offline

    Rprrr

    serfma
    Well I'm not 100% sure of what you're trying to achieve.. but I would just check it in one line, like so:
    Code:
    if (Redemption.playerSideConfig.getString("PlayerSide." + player.getName() + ".side") == null){
     
    //...
     
    }
    else {
     
    String playerSide = Redemption.playerSideConfig.getString("PlayerSide." + player.getName() + ".side");
     
    }
    Oh, by the way, if you just want to check if a player has played on the server before (you might be, but I'm not sure because I don't exactly know what you're trying to do), you can also use player.hasPlayedBefore(). But if that's not what you want to do, just act like I didn't suggest this.
     
  12. Offline

    serfma

    I may actually use that for giving out "starter items". But the reason I use this other way is to make sure that they're set in stone as Living, if they're not already Undead or Living. And that even if they've played before, could be a chance they don't get set as Living and I just wanted to be prepared. :)

    I'm a complete beginner/noob to Java and I have an opportunity to learn Java, make a plugin, AND start a brand new gamemode. It's all about players being Living and Undead/Zombies that hunt the Living down. Random loot. Etc etc. So I have to figure out how to manipulate chat per Living/Undead players, prevent them from breaking/destroying certain blocks and everything! lol.

    The way I have it now is via Permissions and manipulating a bunch of other plugins to achieve what I want, but, there's a lot more I'd like and if I can create it myself, I know no one else did it. :p

    But hey, thank you a LOT for your help!
     
Thread Status:
Not open for further replies.

Share This Page