NullPointerExeption in plugin!

Discussion in 'Plugin Development' started by tyler53, May 25, 2012.

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

    tyler53

    My plugin is just a tester right now that takes a file and stores values in it.
    I have a class for writing the data to the file, and that is my DataStore.java
    Which reads
    Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
     
    public class DataStore {
     
        public File storageFile;
        public ArrayList<String> values = new ArrayList<String>();
       
        public DataStore(File file){
            this.storageFile = file;
            if(this.storageFile.exists() == false){
                try {
                    this.storageFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }   
            }
        }
       
        public void load(){
            try {
                DataInputStream input = new DataInputStream(new FileInputStream(this.storageFile));
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
               
                String line;
               
                while((line = reader.readLine()) != null){
                    if(this.values.contains(line) == false){
                        this.values.add(line);
                    }
                }
                reader.close();
                input.close();
               
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
       
        public void save(){
            try {
                FileWriter stream = new FileWriter(this.storageFile);
                BufferedWriter out = new BufferedWriter(stream);
               
                for(String value : this.values){
                    out.write(value);
                    out.newLine();
                }
               
                out.close();
                stream.close();
               
            } catch (IOException e) {
                e.printStackTrace();
            }
               
        }
        public boolean contains(String value){
            return this.values.contains(value);
        }
       
        public void add(String value){
            if(this.contains(value) == false){
                this.values.add(value);
            }
        }
       
        public void remove(String value){
            this.values.remove(value);
        }
       
        public ArrayList<String> getValues(){
            return this.values;
        }
    }
    
    I then use this in my main plugin class by using this block of code
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
        if(commandLabel.equalsIgnoreCase("jcp")){
               
                if(args.length == 1){
                   
                    Player log = plugin.getServer().getPlayer(args[0]);
                    plugin.jailedPlayers.add(args[0]);
                   
                player.sendMessage(ChatColor.DARK_GREEN + "Player Name Logged");
                player.sendMessage(ChatColor.DARK_RED + "Type /JCT [Time] to continue Logging");   
                }
            }
    }
    
    So that when they type /JCP <name> it logs that name

    When I try this ingame I get a nullpointerexception error saying
    Code:
    [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'jcp' in plugin JailCheck v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:558)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:450)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at me.tyler53.JailCheck.JailCheck.onCommand(JailCheck.java:79)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 12 more
    And I thought it might have been because I didn't set up the array correctly, but I set it up in my onEnable like so
    Code:
    ArrayList<String> values = new ArrayList<String>();
           
            this.jailedPlayers = new DataStore(new File(pluginFolder + File.separator + "jailed-players.txt"));
            
    the second part is me setting up the DataStore file for the actual logging of people.

    Anyways I have no idea why I am getting these errors, please help
     
  2. Offline

    Gravity

    at me.tyler53.JailCheck.JailCheck.onCommand(JailCheck.java:79)
    You need to look there, you just pasted with CODE so I can't tell which line that is.
     
  3. Offline

    tyler53

    Right Sorry, forgot about that :p
    Its the Player log = plugin.getServer().getPlayer(args[0]);
    But before I had that I just had the plugin.jailedPlayers.add(args[0]);
    And I got the same error
     
  4. Offline

    Gravity

    My guess is plugin is null.
     
  5. Offline

    tyler53

    What do you mean by plugin is null? The variable? How would I fix that?
     
  6. Offline

    Gravity

    Yes, the variable. You'd have to make sure you assigned a value to plugin before you use it.

    But make sure I'm right first...
    if(plugin == null)
    System.out.println("Plugin is null");
     
  7. Offline

    tyler53

    public static JailCheck plugin;
    Is how i declared that variable
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    you dont need the static, and you need to assign a value to it.
     
  9. Offline

    tyler53

    So something along the lines of
    Code:
    public JailCheck(JailCheck, plugin){
        this.plugin = plugin;
    }
    ?
    This gives me an error "the left hand side of the argument must be a variable"
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    yes, but you have an extra comma in there.

    it needs a handle you your plugin.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  11. Offline

    tyler53

    When I put in
    Code:
    public JailCheck(JailCheck plugin){
        this.plugin = plugin;
    }
    I get this error when running the plugin
    Code:
    [SEVERE] Could not load 'plugins\JailCheck.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NoSuchMethodException: me.tyler53.JailCheck.JailCheck.<init>()
        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:213)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:189)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:53)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:157)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:423)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NoSuchMethodException: me.tyler53.JailCheck.JailCheck.<init>()
        at java.lang.Class.getConstructor0(Unknown Source)
        at java.lang.Class.getConstructor(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:142)
     
  12. Offline

    Gravity

    Are you using an IDE? If so you should have seen that error before you compiled it. You need to pass the 'Plugin' (the main class) along when using the JailCheck constructor. If you are defining it from the main class, it would look something like new JailCheck(this);
     
  13. Offline

    Coryf88

    Remove the constructor that you added, then in onCommand change "plugin" to "this".
     
  14. Offline

    Gravity

    That will only work if the onCommand method is in a class that extends JavaPlugin (the main class)
     
  15. Offline

    Coryf88

    According to the OP, it is.

     
  16. Offline

    tyler53

    I'm using eclipse, but it didn't tell me that I had this error

    It seems to be working now! thanks so much!
     
  17. Offline

    Coryf88

    An IDE would not specify that error since that's a RuntimeException which occurs in Bukkit due to no default constructor in the main plugin class.
     
  18. Offline

    tyler53

    when i use add(args[0])
    How would I make it so that it just added the entire line that was entered, instead of just the first argument
     
  19. Offline

    Coryf88

    Either create a StringBuilder, loop through the args and append them to the StringBuilder, and append a space in-between them or use Joiner.on(' ').join(args);
     
  20. Offline

    tyler53

    Okay, cool thanks so much, now when I got back to call this line I am storing it on, I want to only have it check for the value for the first argument, or the first maybe 4 letters because they wiill be inputting [Name] [Time] [Reasons] ect. into this string but i only want the plugin to check the log file for [Name] when I call it back to send to the player. How would i do that?

    Edit: I figured I would use a substring of some sort but I just don't know how to do that

    I'm sorry I don't think that last post was very clear lol :p (Sorry its a bit late)
    Right now I have a Class for DataStore that has the methods add, remove, save, load, and getValues (all relating to the "jailed-players.txt".

    "jailed-players.txt" is the file that is holding all the inputs I am getting from the senders of the commands. So for example i would type ingame "/jcp tyler53 4:30 3 days for griefing" and the plugin would store that whole string into one line in the "jailed-players.txt", and then make a new line for the next entry that someone makes.

    So the jailed players txt file would have things like
    tyler53 4:30 3 days for griefing
    anothermember 3:10 1 hour for spamming
    anothermember1 9:00 2 minutes for scamming


    and what I need to do is be able to have a method for getting the string that has "tyler53" so that i can send the player "tyler53 4:30 3 days for griefing"

    This plugin is used for logging and checking statuses of players.

    Anyways, my current method for this is
    Code:
    public ArrayList<String> getValues(){
            return this.values;
        }
    Which prints out the entire list of the values, so it would print out all the names and all the info. However I want to be able to have the sender type /jcc tyler53

    and have the plugin JUST send it the string starting with tyler53.

    Maybe I could do this using a contains(value) or something? because I have a contains method that reads
    Code:
    public boolean contains(String value){
            return this.values.contains(value);
        }
    Already written to check if the name already exists in the file.
    I hope this is clear enough I know its long, but thanks for all your help so far guys :D
    hopefully this will be my last issue before I release this plugin :)

    P.S. would it be easier to store all this info in a config.yml instead of the jailed players txt?

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

Share This Page