NullPointerException

Discussion in 'Plugin Development' started by CompuIves, Apr 11, 2012.

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

    CompuIves

    Hey guys!
    I got another problem :S, this time with calling a function from the main class...
    Code:
    2012-04-11 20:45:12 [SEVERE] Could not load 'plugins\HungerGames.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ExceptionInInitializerError
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:150)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:207)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:183)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:53)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:156)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:422)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ExceptionInInitializerError
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:139)
        ... 8 more
    Caused by: java.lang.NullPointerException
        at net.iveserver.allgamer.HungerGames.TributeList.load(TributeList.java:110)
        at net.iveserver.allgamer.HungerGames.TributeList.<init>(TributeList.java:21)
        at net.iveserver.allgamer.HungerGames.HungerGames.<clinit>(HungerGames.java:20)
        ... 11 more
    My main code is
    Code:
    package net.iveserver.allgamer.HungerGames;
     
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashSet;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class HungerGames extends JavaPlugin
    {
        public static HungerGames plugin;
          final private static TributeList tributelist = new TributeList(plugin); //line 20
          Logger log;
          public HashSet<Player> Frozen = new HashSet<Player>();
          public FileConfiguration config;
          private String pluginName;
          private FileConfiguration tributefile = null;
        private File customtributefile = null;
     
      public void onEnable()
      {
        PluginDescriptionFile yml = getDescription();
        this.log = getLogger();
        this.log.info("HungerGames has been Enabled");
        this.pluginName = yml.getName();
        this.config = getConfig();
        this.config.options().copyDefaults(true);
        this.getTributeFile().options().copyDefaults(true);
        getCommand("hg").setExecutor(new HungerGamesCommander(this));
        saveDefaultConfig();
        saveTributeFile();
        getServer().getPluginManager().registerEvents(new DeathListener(this), this);
      }
     
      public void onDisable() {
        this.log = getLogger();
        this.log.info("HungerGames has been Disabled");
      }
        public void reloadTributeFile() {
            if (customtributefile == null) {
            customtributefile = new File(getDataFolder(), "tributes.yml");
            }
            tributefile = YamlConfiguration.loadConfiguration(customtributefile);
     
            // Look for defaults in the jar
            InputStream defConfigStream = getResource("tributes.yml");
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                tributefile.setDefaults(defConfig);
            }
        }
     
        public FileConfiguration getTributeFile() {
            if (tributefile == null) {
                reloadTributeFile();
            }
            return tributefile;
        }
     
        public void saveTributeFile() {
            if (tributefile == null || customtributefile == null) {
            return;
            }
            try {
                tributefile.save(customtributefile);
            } catch (IOException ex) {
                Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE, "Could not save config to " + customtributefile, ex);
            }
        }
     
      public void log(String message)
      {
        this.log.info("[" + this.pluginName + "] " + message);
      }
      public static TributeList getTributeList()
      {
      return tributelist;
      }
     
      }
    
    And this is part of the class that wants to call it:
    Code:
    package net.iveserver.allgamer.HungerGames;
     
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.text.ParseException;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Server;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.configuration.file.FileConfiguration;
     
     
     
    public class TributeList extends JavaPlugin {
        private ArrayList<Tribute> Tributes = new ArrayList<Tribute>();
        HungerGames plugin;
          TributeList(HungerGames instance) {
              this.plugin = instance;
              load(); //line 21
              }
    /*   
     
    ...
     
        public void load() {
            FileConfiguration tributefile = plugin.getTributeFile(); //line 110
            SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
            for (int i = 0; i < 25; i++) {
                String tributeEntry = "Tribute" + Integer.toString(i + 1);
                if (!tributefile.isSet(tributeEntry + ".Name")) return;
                String name = tributefile.getString(tributeEntry + ".Name");
                int score = tributefile.getInt(tributeEntry + ".Score");
                boolean alive = tributefile.getBoolean(tributeEntry + ".Alive");
                    Date deathdate = new Date();
                    try {
                    deathdate = format.parse(tributefile.getString(tributeEntry + ".Date"));
                } catch (ParseException e) {
                    this.plugin.log("can't read the date:" + deathdate + " resetting date.");
                    deathdate = new Date(1L);
                }
                Tribute player = new Tribute(name, score, alive, deathdate);
                this.Tributes.add(player);
            }
        }
    I hope someone can help me with this cause it's freaking me out xD.
    Thanks in advance.

    btw, I've commented the line numbers the error refers too.
     
  2. Offline

    Sessional

    the null pointer exception is happening on line 110 of TributeList.java
    Could you tell us what line that is? You seem to have removed a bit of the code with ...

    EDIT:Sorry. Missed the comment.

    I'd avoid statics. I would also avoid defining the variables outside of the "onEnable" method. I'mnot quite sure, but I think it might be because of that static variable being defined somewhere.
     
  3. Offline

    CompuIves

    I've commented the line, it is:
    Main class:
    Code:
          final private static TributeList tributelist = new TributeList(plugin); //line 20
    Custom class:
    Code:
              load(); //line 21
    Code:
            FileConfiguration tributefile = plugin.getTributeFile(); //line 110
    Thank you for helping :)
     
  4. Offline

    Sessional

    Yeah. Avoid the statics. If I remember correctly statics are defined before anything else in the class. So the TributeList(plugin) will be defining itself before plugin is ever defined.
    It will be creating TributeList(null) as it's tribute list (I think.) Try creating these as non-statics inside the onEnable method and that should fix it.
     
  5. The variable plugin is not set to a value when you initialize the TributeList.
     
  6. Offline

    CompuIves

    Thank you guys! It worked!
     
  7. Offline

    Njol

    btw: If you don't want to create a plugin called TributeList you should remove 'extends JavaPlugin' from TributeList.java
     
  8. Offline

    CompuIves

    Ahh, I see, thanks, removed it.
     
Thread Status:
Not open for further replies.

Share This Page