File returns a null pointer

Discussion in 'Plugin Development' started by Creeoer, Dec 1, 2014.

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

    Creeoer

    So I get a stacktrace, telling me that at line 41 there is a nullpointer expection when I place my block. I think, "Ok so file could be null, I'll add a null check." Still nothing, so I think "is it possible that my file variable is returning null?" And I can't seem to find the issue as the file is written in the correct directory. Any help?


    StackTrace if it helps:
    Code:
    [17:58:39 ERROR]: Could not pass event BlockPlaceEvent to BBBlocks v0.0.3
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callBlockPlace
    Event(CraftEventFactory.java:138) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g868
    8bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.ItemStack.placeItem(ItemStack.java:142)
    [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.PlayerInteractManager.interact(PlayerInt
    eractManager.java:390) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092j
    nks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :629) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.a(SourceFile:60)
    [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInBlockPlace.handle(SourceFile
    :9) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.NullPointerException
            at me.creeoer.bb.handlers.LandChecker.canPlayerPlaceSchematic(LandChecke
    r.java:41) ~[?:?]
            at me.creeoer.bb.main.PlayerListener.buildlingPlace(PlayerListener.java:
    166) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _67]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _67]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_67]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_67]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            ... 16 more
    >
    Line where it tells me to look:
    Code:java
    1. public boolean canPlayerPlaceSchematic(String schematicname, Player player) throws DataException, IOException {
    2.  
    3. File schematicsave = new File(main.getDataFolder() + File.separator + "schematics" + File.separator + schematicname + ".schematic");
    4.  
    5. //Just checks if any protection and returns true or false if player has access or not accordingly
    6.  


    How the name of the file is determined (Note that BB Block is green, when i replace the string must i use ChatColor.GREEN + the string?)
    Code:java
    1. public void buildlingPlace(BlockPlaceEvent e) throws MaxChangedBlocksException, DataException, IOException{
    2. Player player = e.getPlayer();
    3. ItemStack sas = e.getItemInHand();
    4. ItemMeta im = sas.getItemMeta();
    5.  
    6. if(im.getDisplayName() != null) {
    7. //Just debugging
    8. Bukkit.broadcastMessage("debuggs");
    9. if(im.getDisplayName().contains("BB Block")){
    10. //This prints
    11. Bukkit.broadcastMessage("yooo i contain bb block");
    12. String building = im.getDisplayName().replace("BB Block", "");
    13.  
    14.  
    15.  
    16.  
    17.  
    18.  
    19. if (land.canPlayerPlaceSchematic(building, player) == true) {
     
  2. Offline

    PreFiXAUT

    Did you try to debug the Code? Normaly you don't get a NPE by creating a new Object (Only when Java has a BIG Prob), so I'ld guess it's a problem with your "main.getDataFolder()". Just save every step into a variable and print it out and send the output.
     
  3. Offline

    Skionz

    Creeoer Post your entire class and show us where you initialize 'main.'
     
  4. Offline

    Creeoer

    Skionz
    Here:
    Code:
    package me.creeoer.bb.handlers;
     
    import java.io.File;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.List;
     
    import me.creeoer.bb.main.BB;
    import me.ryanhamshire.GriefPrevention.Claim;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
     
    import org.bukkit.util.Vector;
     
    import com.massivecraft.factions.entity.BoardColls;
    import com.massivecraft.factions.entity.Faction;
    import com.massivecraft.factions.entity.UPlayer;
    import com.sk89q.worldedit.CuboidClipboard;
     
    import com.sk89q.worldedit.data.DataException;
    import com.sk89q.worldguard.protection.ApplicableRegionSet;
    import com.sk89q.worldguard.protection.managers.RegionManager;
     
    public class LandChecker{
    BB main = BB.getInstance();
       
     
                     
       
         
     
       
       
       
     
        public boolean canPlayerPlaceSchematic(String schematicname, Player player) throws DataException, IOException {   
           
              File schematicsave = new File(main.getDataFolder() + File.separator + "schematics" + File.separator + schematicname + ".schematic");
             
             
             
            CuboidClipboard cc = CuboidClipboard.loadSchematic(schematicsave);
            int x = cc.getLength();
            int y = cc.getHeight();
            int z = cc.getWidth();
            int xx = x + player.getLocation().getBlockX();
            int yy = y + player.getLocation().getBlockY();
            int zz = z + player.getLocation().getBlockZ();
            Location schematic = new Location(player.getWorld(),xx,yy,zz);
           
     
           
     
           
           
           
     
           
            //good
            if(main.getFactions() != null) {
                Faction faction = BoardColls.get().getFactionAt(com.massivecraft.massivecore.ps.PS.valueOf(schematic));       
                if (faction !=null) {
                List<UPlayer> fplayers = faction.getUPlayers();
                  Player playername = Bukkit.getPlayerExact(player.getName());
                  UPlayer uplayer = UPlayer.get(player);
                 
                if(fplayers.contains(uplayer)) {
                    return true;
                }
                return false;
                }
            }
       
           
           
           
            //Good
            if(main.getGriefPrevention() != null) {
                Claim claim = main.getGriefPrevention().dataStore.getClaimAt(schematic, true, null);
                String owner = claim.getOwnerName();
                if(claim != null) {
                      if (owner == player.getName()) {
                          return true;
                      }
                      return false;
                }
               
            }
           
               
               
               
               
               
               
               
           
           
           
           
           
            //Good
            if (main.getWorldGuard() != null) {
                com.sk89q.worldguard.LocalPlayer p = main.getWorldGuard().wrapPlayer(player);
                RegionManager regionManager = main.getWorldGuard().getRegionManager(player.getWorld());
         
                if(regionManager !=null) {
                    ApplicableRegionSet set = regionManager.getApplicableRegions(schematic);
                    //Returns a true or false boolean based on whether or not this player has access to the region
                    return set.canBuild(p);
                   
                }
            }
           
           
           
           
            //Good
                if (main.getGriefPrevention() == null && main.getFactions() == null && main.getWorldGuard() == null) {
                return true;
            }
            return false;
           
           
           
           
     
        }
        public Vector turntoVector(Location location) {
              double pitch = ((location.getPitch() + 90) * Math.PI) / 180;
              double yaw  = ((location.getYaw() + 90)  * Math.PI) / 180;
              double xxx = Math.sin(pitch) * Math.cos(yaw);
              double yyy = Math.sin(pitch) * Math.sin(yaw);
              double zzz = Math.cos(pitch);
              Vector origin = new Vector(xxx, zzz, yyy);
             
     
              return origin;
        }
     
     
     
     
     
     
    public boolean doesFileExist(File file){
        File folder = new File(BB.getInstance().getDataFolder() + File.separator + "schematics");
        List<File> files = Arrays.asList(folder);
          if(files.contains(file)){
             
              return true;
          }
          return false;
    }
    }
     
  5. Offline

    Skionz

    Creeoer Try initializing 'main' in the method.
     
  6. Offline

    Creeoer

    Skionz
    Well it partially worked, now I'm gettinga file not found exepction. Anyways I'll fix this myself from now on, thanks. And why does initalizng the variable inside the method make a difference anyways?
     
Thread Status:
Not open for further replies.

Share This Page