Solved Name Cannot Be Null Error

Discussion in 'Plugin Development' started by DefaultSyntax, Aug 4, 2014.

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

    DefaultSyntax

    Hi, I've had this problem for a while now and I have no clue how to fix it. So what I'm doing is I'm trying to spawn a wolf on a player's location using wolf.class . However when I run the plugin my console throws me an error saying "Name cannot be null". Can someone HELP me fix this, and by help I mean not giving me the code, but actually walking me through my mistakes so I don't make them again. Thank you :)

    Here's my code:
    Pets Class:
    Code:
    public class Pets extends JavaPlugin{
        String playerName;
        Player player = Bukkit.getPlayerExact(playerName);
        Location loc = player.getLocation();
     
        public void spawnWolf(){
            Wolf wolf = loc.getWorld().spawn(loc, Wolf.class);
           
        }
    Main Class:
    Code:
    public class Core extends JavaPlugin {
     
        Pets pet = new Pets();
     
        @Override
        public void onEnable() {
     
     
        }
     
        @Override
        public void onDisable() {
     
     
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender.hasPermission("girithiumpets.wolf")){
                if(cmd.getLabel().equalsIgnoreCase("spawnwolf")){
                    pet.spawnWolf();
     
                }
     
            }
     
     
            return false;
        }
    Error log:
    Code:
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: Name cannot be null
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.loadPlugins(CraftServer.java:241)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.reload(CraftServer.java:613)
        at org.bukkit.Bukkit.reload(Bukkit.java:277)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:528)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchServerCommand(CraftServer.java:515)
        at net.minecraft.server.v1_6_R3.DedicatedServer.as(DedicatedServer.java:263)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:228)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.IllegalArgumentException: Name cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.getPlayerExact(CraftServer.java:346)
        at org.bukkit.Bukkit.getPlayerExact(Bukkit.java:417)
        at me.JulesLabador.GirithiumPets.Pets.<init>(Pets.java:20)
        at me.JulesLabador.GirithiumPets.Core.<init>(Core.java:18)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178)
        ... 14 more
     
  2. Offline

    _LB

    Code:java
    1.  
    2. String playerName;
    3. Player player = Bukkit.getPlayerExact(playerName);
    This code is in the class body and so runs before any constructor. How could playerName ever not be null?
     
  3. Offline

    artish1

    DefaultSyntax
    Are these 2 seperate plugins? If not, why are you extending JavaPlugin to 2 different classes? Only your main class should be extending JavaPlugin, failure to do so may cause "Problems"
     
  4. Offline

    DefaultSyntax

    artish1 It's all from one plugin, just from 2 different classes. I removed the "extends JavaPlugin" part and it still gave me the same error.

    _LB
    Code:java
    1. String playerName = "";
    2. Player player = Bukkit.getPlayerExact(playerName);
    3. Location loc = player.getLocation();

    I gave playerName a value, now it's giving me a NullPointerException error. I want to get the Player's name and location outside of the method so it's not repetitive. If I wanted to do this, what would you recommend doing so that I get the information I need, because in order to do the "Bukkit.getPlayer() method, I'm going to need a String for it.
     
  5. Offline

    _LB

    Put the code in the constructor.
     
  6. Offline

    artish1

    DefaultSyntax
    Why do you never declare the String playerName? I do not see ANYWHERE that you have changed playerName to an actual name besides ""; (and that is null?), atleast create a Constructor with a 'String ownerName' Parameter (or however you want to name it), and then declare that playerName = ownerName; inside the constructor.
     
  7. Offline

    Skye

    Null is null, the absence of value. "" is an empty String, not null. ;)
     
  8. Offline

    artish1

    Skye
    But Bukkit.getPlayerExact(""); will always return null because there is no player named "".
     
  9. Offline

    DefaultSyntax

    Thank you guys so much! I was able to figure out the problem! :)
     
Thread Status:
Not open for further replies.

Share This Page