Solved NullPointerException?

Discussion in 'Plugin Development' started by Cerlurs, Nov 22, 2015.

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

    Cerlurs

    Basically, I'm trying to load a plugin I made. But whenever I try to use a command I created, it says "An Internal Error Occured". It says there's something null on line 34. However I'm using the variables I've created on that line.
    Code:
    package me.cerlurs.potioncommands.main;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class main extends JavaPlugin {
      
        public final Logger logger = Logger.getLogger("Minecraft");
      
        @Override
        public void onEnable() {
            Bukkit.getServer().getLogger().info("PotionCommands v1.0 has been enabled!");
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
      
        @Override
        public void onDisable() {
            Bukkit.getServer().getLogger().info("PotionCommands v1.0 has been disabled!");
          
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
            Player p = (Player) sender;
            String noperm = getConfig().getString("noperm");
            String nopermission = ChatColor.translateAlternateColorCodes('&', noperm);
            String nv = getConfig().getString("nightvision");
            String nightvision = ChatColor.translateAlternateColorCodes('&', nv);
          
            if(cmd.getName().equalsIgnoreCase("nv")) {
                if(p.hasPermission("potioncommands.nightvision")) {
                    p.sendMessage(nightvision);
                    p.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 1728000, 1));
                  
                }
                if(!(p.hasPermission("potioncommands.nightvision"))) {
                    p.sendMessage(nopermission);
                }
            }
          
          
          
          
            return false;
          
        }
    
    }
    
    I'm not too experienced when it comes to coding, this is my first time trying to add a config file. I've probably made an obvious mistake but I just don't realize it.

    Error Log (open)

    22.11 13:21:04 [Server] INFO ... 13 more
    22.11 13:21:04 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at me.cerlurs.potioncommands.main.main.onCommand(main.java:34) ~[?:?]
    22.11 13:21:04 [Server] INFO at org.bukkit.ChatColor.translateAlternateColorCodes(ChatColor.java:206) ~[spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO Caused by: java.lang.NullPointerException
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_1649.jar:git-Spigot-1649]
    22.11 13:21:04 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'nv' in plugin PotionCommands v1.0
    22.11 13:21:04 [Server] ERROR null
    22.11 13:21:02 [Server] INFO Cerlurs >> Hey cutie
    22.11 13:19:56 [Server] INFO ... 13 more
    22.11 13:19:56 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at me.cerlurs.potioncommands.main.main.onCommand(main.java:34) ~[?:?]
    22.11 13:19:56 [Server] INFO at org.bukkit.ChatColor.translateAlternateColorCodes(ChatColor.java:206) ~[spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO Caused by: java.lang.NullPointerException
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_1649.jar:git-Spigot-1649]
    22.11 13:19:56 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'nv' in plugin PotionCommands v1.0
     
    Last edited: Nov 22, 2015
  2. Offline

    Scimiguy

    Use [code][/code] tags instead of using spoilers, far easier to read
     
  3. Offline

    Zombie_Striker

    Read this: http://www.oracle.com/technetwork/java/codeconventions-135099.html
    STOP STEALING MINECRAFT'S LOGGER!
    Bukkit already logs your plugins. You don't need this.
    Because of Above, You can completely remove this.

    DONT BLIND CAST
    You're creating this instence for EACH AND EVERY command sent. This will take up memory space. Please create these instance once you know you're actually going to use them.
     
  4. Offline

    mcdorli

    Can you please show us your config.yml?
     
  5. Offline

    Cerlurs

    @mcdorli
    noperm: '&c&lPotion Commands &8> &7&lYou do not have permission to do that!'
    nightvision: '&c&lPotion Commands &8> &7&lYou have enabled Night Vision!'
     
  6. Offline

    Zombie_Striker

    Instead of doing another useless check, just use "ELSE"

    22.11 13:19:56 [Server] INFO at me.cerlurs.potioncommands.main.main.onCommand(main.java:34) ~[?:?]
    This is the line that is causing the error. What is this line?
     
  7. Offline

    Cerlurs

    @Zombie_Striker String nopermission = ChatColor.translateAlternateColorCodes('&', noperm);
     
  8. Offline

    mcdorli

    noperm is somehow null
     
  9. Offline

    Cerlurs

    @mcdorli I know this, but I'm using it. So I can't figure out how it's null.
     
  10. Offline

    mcdorli

    make a picture of the config.yml file and post it here
     
  11. Offline

    Cerlurs

  12. Offline

    mcdorli

    Did you enabled, so you use the messages.yml file instead of the basic config.yml file?
     
  13. Offline

    Cerlurs

    @mcdorli I don't believe so. How do I do that?
     
  14. Offline

    Xerox262

    If you do not know how to do it you should just stick to the default config.yml until you figure it out.
     
  15. Offline

    mcdorli

    I reccomend just renaming the config to config.yml if you don' have it already. But if you don't want to, then simply FileConfiguration config = YamlConfiguration.loadConfiguration(<your file here>);
    then use config.set()
     
  16. Offline

    Cerlurs

    @Xerox262 Fair enough
    @mcdorli I'll just rename it

    @mcdorli Thanks for all your help, changing the name ended up fixing the issue

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 22, 2015
Thread Status:
Not open for further replies.

Share This Page