SQL query to read out database doesn´t work

Discussion in 'Plugin Development' started by RE3ELL, May 26, 2013.

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


    i´m writing a command to read out my sqlite database. The database contains some integers for every player. These integers are called block1, block2, ... blockx.

    The database looks like this:

    id player block1 block2 block3
    1 Steve 1 5 0
    2 Notch 3 0 7

    What i want to do is:
    Give a message like this to a player using the /getlimit <player> command:
    " Steve has: [ 1 , 5, 0] "

    I wrote this code in my command executor class to read it out:

    Alternative: http://pastebin.com/0DaFcke6

    package me.Cakestory.restrict;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    public class restrictCommandExecutor implements CommandExecutor {
        private restrict main = new restrict();
        int[] limit = null;
        public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("rsreload")) {
                // Does not work whyever! Find out!
                return true;
            if (cmd.getName().equalsIgnoreCase("getlimit")) {
                if (args.length == 1) {
                    try {
                        Player target = Bukkit.getServer().getPlayer(args[0]);
                        try {
                            for (int x = 0; x < restrict.blocks.length; x++) {
                                int index = x + 1;
                                String blockName = "block" + index;
                                ResultSet rs = restrict.sql.query("SELECT " + blockName + " FROM restrict WHERE player='" + target.getDisplayName() + "'");
                                while (rs.next()) {
                                    limit[x] = rs.getInt(blockName);
                        } catch (SQLException e) {
                        String message = target.getDisplayName() + ": [";
                        for (int i = 0; i < restrict.blocks.length; i++) {
                            message = message + " " + limit[i] + " |";
                        message = message + " ]";
                    } catch (NullPointerException e) {
                        sender.sendMessage(ChatColor.RED + "Please select an online Player. " + args[0] + " is not online.");
                } else {
                    sender.sendMessage(ChatColor.RED + "Syntax error. Usage: /getlimit <player>");
                return true;
            return false;

    Sadly it won´t work. I tried to find out where the problem comes from by disableing some parts of the code and used my playername instead of variables, but it won´t work correct.
    In this structure it always jumps to the second catch(NullpointerException e) part.

    Other information:
    - I am using SQLibrary API for more simple sql queries.
    - My main class is called "restrict"

    May you help me to get it to work please?

    Thanks, re3ell.

    P.S. If you need additional information please tell me.
  2. Offline


    I'd like you to do this:
    In the second catch-block, put e.printStackTrace();

    There is loads of things that could be null at this point, not just the player. If you add that line for me, and then try again, you will get a stack trace in the console. If you copy that over here, I'll be able to help you.

    Actually! Never mind! Your int[] limit is null.

    You set it to null and then you try to change the entries. So instead of doing:
    int[] limit = null;
    You should do:
    int[] limit = new int[restrict.blocks.length];
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 1, 2016
  3. Offline


    Omg. :rolleyes:
    I could ram my head against a wall.
    Thank you. It solved my problem. And saved my day :)
Thread Status:
Not open for further replies.

Share This Page