Solved Java NullPointerException on location.getBlock().getType()

Discussion in 'Plugin Help/Development/Requests' started by Smootey, Jan 2, 2015.

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

    Smootey

    Hello!

    So, I am currently creating a bank plugin and it gets managed by signs.
    So, for now I've created 2 classes which are saving the sings in a .json file.
    Now the bank signs keep updating every second and I get an error...

    Here are my 3 classes:


    Signs Class (open)
    Code:
    package de.smoothie2.citybuild.signs;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    
    /**
    * Created by Marc on 01.01.2015.
    */
    public class Sign {
        private String name;
        private String world;
        private int x;
        private int y;
        private int z;
    
        public Sign(String name, Location loc) {
            this.name = name;
            this.world = loc.getWorld().toString();
            this.x = loc.getBlockX();
            this.y = loc.getBlockY();
            this.z = loc.getBlockZ();
        }
    
        public String getName() {
            return this.name;
        }
    
        public World getWorld() {
            return Bukkit.getWorld(this.world);
        }
    
        public int getX() {
            return this.x;
        }
    
        public int getY() {
            return this.y;
        }
    
        public int getZ() {
            return this.z;
        }
    
        public Location getLocation() {
            return new Location(getWorld(), getX(), getY(), getZ(), 0, 0);
        }
    
    
    }
    


    SignManager Class (open)
    Code:
    package de.smoothie2.citybuild.signs;
    
    import de.smoothie2.citybuild.Main;
    import me.michidk.DKLib.FileHelper;
    import net.minecraft.util.com.google.common.collect.Lists;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.libs.com.google.gson.Gson;
    import org.bukkit.craftbukkit.libs.com.google.gson.JsonSyntaxException;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.HashSet;
    import java.util.Set;
    
    /**
    * Created by Marc on 02.01.2015.
    */
    public class SignManager {
        private Gson gson = new Gson();
        private Set<Sign> signs = new HashSet();
    
        public SignManager() {
        }
    
        public void loadFromFile() {
            signs.clear();
            File file = new File(Main.getInstance().getDataFolder(), "signs.json");
            if (!file.exists()) {
                return;
            }
            try {
                signs.addAll(Lists.newArrayList(gson.fromJson(FileHelper.stringFromFile(file), Sign[].class)));
            } catch (JsonSyntaxException e) {
                Bukkit.getLogger().severe("Error while loding signs.json: " + e.getMessage());
            }
        }
    
        public void saveToFile() {
            File file = new File(Main.getInstance().getDataFolder(), "signs.json");
            if (!file.exists()) {
                try {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                } catch (IOException e) {
                    Bukkit.getLogger().severe("Error while creating signs.json: " + e.getMessage());
                    return;
                }
            }
            FileHelper.stringToFile(file, gson.toJson(signs.toArray(new Sign[signs.size()])));
        }
    
        public void addSign(String name, Location loc) {
            if (name == null) {
                return;
            }
            if (loc == null) {
                return;
            }
            name = name.toLowerCase();
            signs.add(new Sign(name, loc));
        }
    
        public Sign getSign(Location loc) {
            if(loc == null) return null;
            for(Sign s : signs) {
                if(loc.equals(s.getLocation())) return s;
            }
            return null;
        }
    
        public void removeSign(Sign s) {
            if(s == null) return;
            signs.remove(s);
        }
    
        public Set getSigns() {
            return signs;
        }
    }
    


    Main Class (open)
    Code:
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run() {
                    for (Player p : Bukkit.getOnlinePlayers()) {
                        for(Object o : getSignManager().getSigns()) {
                            de.smoothie2.citybuild.signs.Sign s = (de.smoothie2.citybuild.signs.Sign) o;
                            if(!s.getName().equals("bank")) return;
                            Location loc = s.getLocation();
                            if(!loc.getBlock().getType().equals(Material.SIGN) && !loc.getBlock().getType().equals(Material.WALL_SIGN) && !loc.getBlock().getType().equals(Material.SIGN_POST)) { // << Here is the error! 
                                getSignManager().removeSign(s);
                                return;
                            }
                            p.sendSignChange(loc, new String[]{"\u00a7a\u00a7lBank",
                            "\u00a7l" + p.getName(),
                            new DecimalFormat("###,###.##").format(Main.getPlayerManager().getPlayer(p.getUniqueId()).getBank()),
                            ((Sign) loc.getBlock().getState()).getLine(3)});
                        }
                    }
                }
            }, 20, 20);


    Error (open)
    Code:
    [13:51:49 WARN]: [CityBuild] Task #10 for CityBuild v0.1 generated an exception
    java.lang.NullPointerException
            at org.bukkit.Location.getBlock(Location.java:82) ~[spigot.jar:git-Spigo
    t-1.7.9-R0.2-207-g03373bb]
            at de.smoothie2.citybuild.Main$1.run(Main.java:123) ~[?:?]
            at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftTask.run(CraftTask.java
    :71) ~[spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftScheduler.mainThreadHea
    rtbeat(CraftScheduler.java:350) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:6
    41) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    89) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    84) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :490) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]


    I hope you can help me.

    Greets, Smootey
     
  2. Offline

    bwfcwalshy Retired Staff

    Moved to Bukkit Alternates.
     
  3. Offline

    coldandtired

    World.toString() probably doesn't return just the name. Try using World.getName() in your sign constructor instead.
     
  4. Offline

    Smootey

    Oh well, thanks!
     
Thread Status:
Not open for further replies.

Share This Page