Annoying NPE and I can not figure it out..

Discussion in 'Plugin Development' started by nala3, Dec 10, 2011.

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

    nala3

    I keep getting this NPE for BLOCK_BREAK whenever a piece of bedrock (the materiel specified) is broken.
    So here is the part of the code in question:
    PHP:
       public void onBlockBreak(BlockBreakEvent event)
       {
           
    Player player event.getPlayer();
           
    int y event.getBlock().getY();
            if(
    event.getBlock().getType() == Material.BEDROCK && <= this.config.Level() && this.permissions.canBreak(player) == true//line 28
            
    {
                
    event.setCancelled(true);
                if(
    config.showMsg() == true)
                {
                
    player.sendMessage(ChatColor.RED "[ABB] " this.plugin.getConfig().getString("Message.String"));
                }
            }
            if(
    player.hasPermission("abb.break"))
            {
                
    event.setCancelled(false);
            }
        }
    and here is the error:
    I think it may have something to do with how I am checking for permission (maybe returning a null? :p )
    PHP:
    public boolean canBreak(Player player)
        {
            
    boolean canBreak;
            if(
    player.isOp())
            {
                return 
    canBreak true;
            }
            else if(
    player.hasPermission("abb.break"))
            {
                return 
    canBreak true;
            }
            else
            {
                return 
    canBreak false;
            }
     
  2. Offline

    nisovin

    Is this.config or this.permissions null?
     
  3. Offline

    nala3

    well I posted what the this.permissions points to, so that should not be null either way. It should be true or false.
    This is what the this.config points to:
    PHP:
        public boolean showMsg()
        {
            
    boolean showMsg;
            if(
    this.plugin.getConfig().get("Message.Show") == "true")
            {
                return 
    showMsg true;
            }
            if(
    this.plugin.getConfig().get("Message.Show") == "false")
            {
                return 
    showMsg false;
            }
            else
            {
                
    log.info("[WARNING][ABB] The configuration file has an incorrect value for \"Show:\", should be \"true\" or \"false\"");
                return 
    showMsg true;
            }
        }
        
    //Level
        
    public int Level()
        {
            
    int level;
            
    level this.plugin.getConfig().getInt("LowestBreakLayer") - 1;
            return 
    level;
        }
     
  4. Offline

    nisovin

    Yes, but where are those variables defined? Are they instantiated? Or did you leave them null?
     
  5. Offline

    nala3

    Ok here is the complete source, I was just removing some stuff to make it a little smaller but here you go.
    AntiBedrockBreak.java (open)

    PHP:
    package antibedrockbreak;

    import java.util.logging.Logger;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;


    /**
     *
     * @author Alan Litz
     *
     */
    public class AntiBedrockBreak extends JavaPlugin{
        private final 
    AntiBedrockBreakBlockListener blockListener = new AntiBedrockBreakBlockListener(this);
        static final 
    Logger log Logger.getLogger("Minecraft");

        
    //Load Config method
        
    public void loadConfig(){
            
    getConfig().options().copyDefaults(true);
            
    saveConfig();
        }

        @
    Override
        
    public void onEnable(){
            
    loadConfig();
            
    log.info("[ABB]Anti-Bedrock Break v0.1 Enabled");
            
    PluginManager pm this.getServer().getPluginManager();
            
    pm.registerEvent(Event.Type.BLOCK_BREAKblockListenerEvent.Priority.Normalthis);
    }
        @
    Override
        
    public void onDisable(){
            
    log.info("[ABB]Anti-Bedrock Break v0.1 Disabled");
        }

        public static 
    void main(String[] args) {
        }
    }

    AntiBedrockBreakBlockListener.java (open)

    PHP:
    package antibedrockbreak;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.ChatColor;

    /**
     *
     * @author Alan Litz
     *
     */
    class AntiBedrockBreakBlockListener extends BlockListener
    {
        public 
    AntiBedrockBreak plugin;
        public 
    AntiBedrockBreakPermissions permissions;
        public 
    AntiBedrockBreakConfig config;

         public 
    AntiBedrockBreakBlockListener(AntiBedrockBreak instance)
         {
            
    plugin instance;
         }
       public 
    void onBlockBreak(BlockBreakEvent event)
       {
           
    Player player event.getPlayer();
           
    int y event.getBlock().getY();
            if(
    event.getBlock().getType() == Material.BEDROCK && <= this.config.Level() && this.permissions.canBreak(player) == true)
            {
                
    event.setCancelled(true);
                if(
    config.showMsg() == true)
                {
                
    player.sendMessage(ChatColor.RED "[ABB] " this.plugin.getConfig().getString("Message.String"));
                }
            }
            if(
    player.hasPermission("abb.break"))
            {
                
    event.setCancelled(false);
            }
        }

    }

    AntiBedrockBreakPermissions.java (open)

    PHP:
    package antibedrockbreak;

    import org.bukkit.entity.Player;

    /**
     *
     * @author Alan Litz
     */
    public class AntiBedrockBreakPermissions
    {
        public 
    boolean canBreak(Player player)
        {
            
    boolean canBreak;
            if(
    player.isOp())
            {
                return 
    canBreak true;
            }
            else if(
    player.hasPermission("abb.break"))
            {
                return 
    canBreak true;
            }
            else
            {
                return 
    canBreak false;
            }
        }

    }

    AntiBedrockBreakConfig.java (open)

    PHP:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package antibedrockbreak;

    import java.util.logging.Logger;

    /**
     *
     * @author Alan
     */
    public class AntiBedrockBreakConfig
    {
        static final 
    Logger log Logger.getLogger("Minecraft");
        
    AntiBedrockBreak plugin;

       
    //Show Messaga
        
    public boolean showMsg()
        {
            
    boolean showMsg;
            if(
    this.plugin.getConfig().get("Message.Show") == "true")
            {
                return 
    showMsg true;
            }
            if(
    this.plugin.getConfig().get("Message.Show") == "false")
            {
                return 
    showMsg false;
            }
            else
            {
                
    log.info("[WARNING][ABB] The configuration file has an incorrect value for \"Show:\", should be \"true\" or \"false\"");
                return 
    showMsg true;
            }
        }
        
    //Level
        
    public int Level()
        {
            
    int level;
            
    level this.plugin.getConfig().getInt("LowestBreakLayer") - 1;
            return 
    level;
        }
    }

    config.yml (open)

    PHP:
    ##############################################
    #------Anti-Bedrock Break Configuration------#
    ##############################################
    #1 = the very bottom layer of bedrock
    LowestBreakLayer1
    Message
    :
      
    Showtrue
      String
    You are not allowed to break through the map!
     
  6. Offline

    nisovin

    As I thought, you never set those variables to anything, so they're null. You need to set them to something. You could do something like this:

    public AntiBedrockBreakPermissions permissions = new AntiBedrockBreakPermissions();
     
    nala3 likes this.
  7. Offline

    nala3

    I've done that, the error is on line 41 of the Config.java
    PHP:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package antibedrockbreak;

    import java.util.logging.Logger;

    /**
     *
     * @author Alan
     */
    public class AntiBedrockBreakConfig
    {
        static final 
    Logger log Logger.getLogger("Minecraft");
        
    AntiBedrockBreak plugin;

       
    //Show Message
        
    public boolean showMsg()
        {
            
    boolean showMsg;
            if(
    this.plugin.getConfig().get("Message.Show") == "true")
            {
                
    showMsg true;
            }
            else if(
    this.plugin.getConfig().get("Message.Show") == "false")
            {
                
    showMsg false;
            }
            else
            {
                
    log.info("[WARNING][ABB] The configuration file has an incorrect value for \"Show:\", should be \"true\" or \"false\"");
                
    showMsg true;
            }
            return 
    showMsg;
        }
        
    //Level
        
    public int Level()
        {
            
    int level 0;
            
    level plugin.getConfig().getInt("LowestBreakLayer"); //line 41
            
    return level;
        }
    }
    Ok, so I sorted those problems but now I get one in the AntiBlockBreakConfig.java (I changed it from the one above)
    Now its this:
    Show Spoiler

    PHP:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package antibedrockbreak;

    import java.util.logging.Logger;

    /**
     *
     * @author Alan
     */
    public class AntiBedrockBreakConfig
    {
        static final 
    Logger log Logger.getLogger("Minecraft");
        
    AntiBedrockBreak plugin;

       
    //Show Message

        
    public boolean show()
        {
            
    boolean msg;
            
    msg plugin.getConfig().getBoolean("Message.Show");
            return 
    msg;
        }

        public 
    boolean showMsg()
        {
            
    boolean showMsg;
            if(
    show() == true)
            {
                
    showMsg true;
            }
            else if(
    show() == false)
            {
                
    showMsg false;
            }
            else
            {
                
    log.info("[WARNING][ABB] The configuration file has an incorrect value for \"Show:\", should be \"true\" or \"false\"");
                
    showMsg true;
            }
            return 
    showMsg;
        }
        
    //Level
        
    public int Level()
        {
            
    int level 0;
            
    level plugin.getConfig().getInt("LowestBreakLayer");
            return 
    level;
        }
    }

    The error is definitely in show() ( I think) >.<

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  8. Offline

    Abrupt

    we need the updated stack trace
     
  9. Offline

    nala3

    Sorry, about that :p, but its ok I got santa to help me. And by santa I mean the guy on the ventrillo I was on :p I was actually way over thinking it after he explained what my error was I realized that a lot of it was redundant.
    http://dev.bukkit.org/server-mods/abb/
     
Thread Status:
Not open for further replies.

Share This Page