java.lang.NullPointerException

Discussion in 'Plugin Development' started by beatcomet, Jul 11, 2011.

  1. Offline

    beatcomet

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I'm getting the following error after launching the server:
    Code:
    [SEVERE] Could not load 'plugins\Freeze.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 Source)
    	at java.lang.reflect.Constructor.newInstance(Unknown Source)
    	at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:173)
    	at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:199)
    	at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:122)
    	at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:118)
    	at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:89)
    	at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:51)
    	at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:132)
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:335)
    	at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    Caused by: java.lang.NullPointerException
    	at me.beatcomet.Freeze.Frz.<init>(Frz.java:19)
    	... 13 more
    
    Here is the plugin's main class :

    Code:java
    1. package me.beatcomet.Freeze;
    2.  
    3. import java.io.File;
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.event.Event;
    7. import org.bukkit.plugin.PluginDescriptionFile;
    8. import org.bukkit.plugin.PluginManager;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10. import org.bukkit.util.config.Configuration;
    11.  
    12. public class Frz extends JavaPlugin{
    13.  
    14.  
    15. //Getting info from plugin.yml
    16. PluginDescriptionFile PDFile = this.getDescription();
    17.  
    18. //Creating a new data file
    19. File configFile = new File("plugins/" + PDFile.getName()
    20. + "/data.dat");
    21. Configuration config = new Configuration(configFile);
    22. //getting Minecraft logger to send console messages
    23. Logger log = Logger.getLogger("Minecraft");
    24. //creating Player Listener reference
    25. PListener playerListener = new PListener(this);
    26. //What happens when the plugin is being disabled
    27. public void onDisable(){
    28. //saving the data
    29. config.save();
    30. //sending console message saying the plugin has been disabled
    31. log.info(PDFile.getName() + " Version " + PDFile.getVersion() + " is DISABLED!");
    32. }
    33. public void onEnable(){
    34. //Sending a console message saying the plugin has been enabled
    35. log.info(PDFile.getName() + " Version " + PDFile.getVersion() + " is ENABLED!");
    36. //making a new file if a data file dose not exist
    37. new File("plugins/" + PDFile.getName()).mkdir();
    38. if (!configFile.exists()) {
    39. try {
    40. configFile.createNewFile();
    41.  
    42. } catch (Exception e) {
    43. //sending console message in case the data file could not be created
    44. log.info("[GlassBreak] Error when creating config file.");
    45. }
    46. }
    47. //Loading data file
    48. config.load();
    49. //getting the plugin manager to register events
    50. PluginManager pm = this.getServer().getPluginManager();
    51. //player join event registration
    52. pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Event.Priority.Normal, this);
    53. }
    54. }
    55.  
  2. Offline

    ZephyrSigmar

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  3. Offline

    beatcomet

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Well sorry this is the last thread i'n opening.

    Here is my PListener :

    Code:java
    1. package me.beatcomet.Freeze;
    2.  
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.player.PlayerJoinEvent;
    7. import org.bukkit.event.player.PlayerListener;
    8. import org.bukkit.event.player.PlayerMoveEvent;
    9.  
    10. public class PListener extends PlayerListener{
    11. Frz plugin;
    12. public PListener(Frz instance){
    13. plugin = instance;
    14. }
    15. public void onPlayerJoin(PlayerJoinEvent event){
    16. Player player = event.getPlayer();
    17. String name = player.getName();
    18. if(plugin.config.getProperty(name) == null)
    19. {
    20. plugin.config.setProperty(name, false);
    21. plugin.config.save();
    22. }
    23. }
    24. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    25. if(!(sender instanceof Player)){
    26. if(commandLabel.equalsIgnoreCase("Freeze") || commandLabel.equalsIgnoreCase("f")){
    27. String name = args[0];
    28. if(args[0] == null){
    29. plugin.log.info("No player name inserted");
    30. }
    31. if((Boolean) plugin.config.getProperty(name) == false){
    32. plugin.config.setProperty(name, true);
    33. plugin.log.info("Player " + name + " Has been freezed!");
    34. }
    35. if((Boolean) plugin.config.getProperty(name) == true){
    36. plugin.config.setProperty(name, false);
    37. plugin.log.info("Player " + name + " Has been unfreezed!");
    38. }else{
    39. plugin.log.info("Player " + name + " Does not exist in the data file");
    40. plugin.config.setProperty(name, false);
    41. }
    42. }
    43. }if(sender instanceof Player){
    44. if(sender.isOp()){
    45. if(commandLabel.equalsIgnoreCase("Freeze") || commandLabel.equalsIgnoreCase("f")){
    46. String name = args[0];
    47. if(args[0] == null){
    48. sender.sendMessage("No name inserted!");
    49. }
    50. if((Boolean) plugin.config.getProperty(name) == false){
    51. plugin.config.setProperty(name, true);
    52. sender.sendMessage("Player " + name + " Has been freezed!");
    53. }
    54. if((Boolean) plugin.config.getProperty(name) == true){
    55. plugin.config.setProperty(name, false);
    56. sender.sendMessage("Player " + name + " Has been unfreezed!");
    57. }else{
    58. sender.sendMessage("Player " + name + " Does not exist in the data file");
    59. plugin.config.setProperty(name, false);
    60. }
    61. }
    62. }
    63. }
    64. return false;
    65. }
    66. public void onPlayerMove(PlayerMoveEvent event){
    67. Player player = event.getPlayer();
    68. String name = player.getName();
    69. if(plugin.config.getBoolean(name, true)){
    70. event.setCancelled(true);
    71. }
    72. }
    73. }
    74.  
  4. Offline

    Evenprime

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    The bold line is where the actual error happens. Let's see what it is:

    Code:
       configFile = new File("plugins/" + PDFile.getName() + "/data.dat");
    So bukkit doesn't like that you use PDFile.getName() at that point. Try to move everything from up there into your onEnable() method and it should work. Like this:

    Code:
    package me.beatcomet.Freeze;
    
    import java.io.File;
    import java.util.logging.Logger;
    
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;
    
    public class Frz extends JavaPlugin{
    
    	PluginDescriptionFile PDFile;
    	File configFile;
    	Configuration config;
    	Logger log;
    	PListener playerListener;
    
    	//What happens when the plugin is being disabled
    	public void onDisable(){
    		//saving the data
    		config.save();
    		//sending console message saying the plugin has been disabled
    		log.info(PDFile.getName() + " Version " + PDFile.getVersion() + " is DISABLED!");
    	}
    	public void onEnable(){
    		//Getting info from plugin.yml
    		PDFile = this.getDescription();
    
    		//Creating a new data file
    		configFile = new File("plugins/" + PDFile.getName()
    			+ "/data.dat");
    		config = new Configuration(configFile);
    		//getting Minecraft logger to send console messages
    		log = Logger.getLogger("Minecraft");
    		//creating Player Listener reference
    		playerListener = new PListener(this);
    
    		//Sending a console message saying the plugin has been enabled
    		log.info(PDFile.getName() + " Version " + PDFile.getVersion() + " is ENABLED!");
    		//making a new file if a data file dose not exist
    		new File("plugins/" + PDFile.getName()).mkdir();
    		if (!configFile.exists()) {
    			try {
    				configFile.createNewFile();
    
    			} catch (Exception e) {
    				//sending console message in case the data file could not be created
    				log.info("[GlassBreak] Error when creating config file.");
    			}
    		}
    		//Loading data file
    		config.load();
    		//getting the plugin manager to register events
    		PluginManager pm = this.getServer().getPluginManager();
    		//player join event registration
    		pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Event.Priority.Normal, this);
    	}
    }
    

Share This Page