NullPointerException Error On Start

Discussion in 'Plugin Development' started by Snwspeckle, Oct 23, 2011.

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

    Snwspeckle

    Okay I would like to address that this is my last post for the night, I just am having some issues. Anyway my issue is that I keep getting a "NullPointerException" when I run my server. Here is the error followed by the plugin.

    Error:
    Code:
    161 recipes
    17 achievements
    23:02:35 [INFO] Starting minecraft server version Beta 1.8.1
    23:02:35 [INFO] Loading properties
    23:02:35 [INFO] Starting Minecraft server on 127.0.0.1:25565
    23:02:35 [WARNING] **** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!
    23:02:35 [WARNING] The server will make no attempt to authenticate usernames. Be
    ware.
    23:02:35 [WARNING] While this makes the game possible to play without internet a
    ccess, it also opens up the ability for hackers to connect with any username the
    y choose.
    23:02:35 [WARNING] To change this, set "online-mode" to "true" in the server.set
    tings file.
    23:02:36 [INFO] This server is running Craftbukkit version git-Bukkit-0.0.0-1131
    -g86b7fa8-b1337jnks (MC: 1.8.1)
    23:02:36 [SEVERE] Could not load 'plugins\LinkAccount.jar' in folder 'plugins':
    
    java.lang.reflect.InvocationTargetException
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce)
            at java.lang.reflect.Constructor.newInstance(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:175)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:215)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:136)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:136)
            at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:112)
            at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    ationManager.java:52)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:136)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:348)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    Caused by: java.lang.NullPointerException
            at lotc.anthony.linkaccount.Link.<init>(Link.java:17)
            ... 13 more
    23:02:36 [INFO] Preparing level "world"
    23:02:36 [INFO] Default game type: 0
    23:02:36 [INFO] Preparing start region for level 0 (Seed: 1807061556657223266)
    23:02:37 [INFO] Preparing start region for level 1 (Seed: 1807061556657223266)
    23:02:37 [INFO] Preparing spawn area: 3%
    23:02:37 [INFO] Server permissions file permissions.yml is empty, ignoring it
    23:02:37 [INFO] Done (0.182s)! For help, type "help" or "?"
    Plugin:
    Code:
    package lotc.anthony.linkaccount;
    
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import java.util.logging.Logger;
     
    public class Link extends JavaPlugin {
    
        String playerName = "adonzo";
        String pinCode = "Code";
    
        Logger log = Logger.getLogger("Minecraft");
        Player player = this.getServer().getPlayer(playerName);
    
        public void onEnable() {
            log.info("Link Account Plugin 0.1 has been enabled");
        }
    
        public void onDisable() {
               log.info("Link Account Plugin 0.1 has been disabled");
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("link")){
                if(this.getServer().getPlayer(playerName) != null){
                    log.info("Player is Online");
                    player.sendMessage(ChatColor.BLUE + pinCode);
                } else {
                    log.info("Player is offline");
                }
                return true;
            }
            return false;
        }
    }
    
     
  2. EDIT: Fail, one sec
    Post your plugin.yml
     
  3. Offline

    Snwspeckle

    plugin.yml:

    Code:
    name: Link Account
    main: lotc.anthony.linkaccount.Link
    version: 0.1
    authors: [Adonzo]
    commands:
       link:
          description: Links the account
          permission: linkaccount.link
          usage: /link
     
  4. Wierdo
    I've seen this error before, but don't remember for what..
    EDIT:
    is this line 17?
    Player player = this.getServer().getPlayer(playerName);
     
  5. Offline

    Snwspeckle

    Darn, I have gotten so far writing it this far tonight. Lol I need to have this part done by the end of the night :)

    Edit: Yes thats line 17, is something wrong with it because based off the code, it seems its all correct.
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    @Snwspeckle you can't call getServer() until your plugins initialize method is called. But this getServer() is called on object construction so it is throwing a InvocationTargetException

    and I don't actually see where it is throwing a NullPointerException.
     
  7. Offline

    desht

    It is throwing a NullPointerException, which is the cause of the InvocationTargetException (InvocationTargetException is thrown if a reflected method fails, and Bukkit uses reflection to load plugin classes). Look for the "Caused by" line in the error text.
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    Nonetheless try moving the getServer call
     
  9. Offline

    desht

    Yeah, I wasn't disputing that part - that is indeed the root cause here ;)
     
  10. Offline

    Snwspeckle

    Where though are you referring to the getServer() call?
    Code:
        Player player = this.getServer().getPlayer(playerName);
    
    Is this line that needs to be changed? If I do not include getServer(), I cannot call the player variable.
     
  11. Offline

    desht

    The problem is that you're calling this.getServer() in your (implicit) constructor, which is too soon. At that point, the plugin's server field hasn't been initialised, and getServer() just returns null.

    Move that call into the onEnabled() method, and you should be fine.
     
  12. Offline

    Snwspeckle

    Yea I fixed that, but now Bukkit keeps returning that its "sad". I know this regards permissions and this is my current setup for permissions:
    Code:
    if (player.hasPermission("testLink")) {
    // Code Here
    {
    
    My plugin.yml File:
    Code:
    name: Link Account
    main: lotc.anthony.linkaccount.Link
    version: 0.1
    authors: [Adonzo]
    commands:
       link:
          description: Links the account
          permission: linkaccount.link
          usage: /link
    permissions:
        testLink:
            description: Gives test linking permissions
            default: op
    Any idea why it keeps returning sad?
     
  13. Offline

    arnie231

     
  14. Offline

    Snwspeckle

    That cannot be right at all because its declared as testLink in my plugin.yml file.
     
  15. System.out.println(player.hasPermission)
    Try oping yourself. Also paste your permissions file
     
  16. Offline

    Snwspeckle

    I have tried to OP myself. Also note that the player is being called from a variable. In this case I set it to my username "adonzo" so it should still be displayed on my screen.
     
  17. System.out.println(player);
    Make sure it isn't null
     
Thread Status:
Not open for further replies.

Share This Page