NullPointerException

Discussion in 'Spigot Plugin Development' started by Daeshan, Jun 1, 2016.

Thread Status:
Not open for further replies.
  1. So I am trying to intialize Main in my classes to avoid statics...help me :D

    Code:
    Code (Text):
    public class MYSQL_InsertPlayerData implements Listener {
       
        private Main m;
        public MYSQL_InsertPlayerData(Main main) {
            this.m = main;
        }
        private  int air;
        private  int level;
        private  String rank;
        private  int money;
       
        // CHECK IF PLAYER EXIST===========================
        public boolean playerExists(String uuid) throws SQLException {
            PreparedStatement rs = m.c.prepareStatement("SELECT * FROM Atlas WHERE UUID='"+ uuid + "';");
                rs.getResultSet().next();
                if (rs.getResultSet().next()) {
                    return rs.getResultSet().getString("UUID") != null;
                }
            return false;
        }
    Stack Trace:
    Code (Text):
    [01:14:24 ERROR]: Could not pass event PlayerJoinEvent to AtlasCraft v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[latest.jar:git-Spigot-8a048fe-a022dd2]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[latest.jar:git-Spigot-8a048fe-a022dd2]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at net.minecraft.server.v1_9_R2.PlayerList.onPlayerJoin(PlayerList.java:333) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at net.minecraft.server.v1_9_R2.PlayerList.a(PlayerList.java:159) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at net.minecraft.server.v1_9_R2.LoginListener.b(LoginListener.java:144) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at net.minecraft.server.v1_9_R2.LoginListener.c(LoginListener.java:54) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at net.minecraft.server.v1_9_R2.NetworkManager.a(NetworkManager.java:233) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at net.minecraft.server.v1_9_R2.ServerConnection.c(ServerConnection.java:140) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:825) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [latest.jar:git-Spigot-8a048fe-a022dd2]
            at java.lang.Thread.run(Thread.java:745) [?:1.7.0_79]
    Caused by: java.lang.NullPointerException
            at me.dabuseck.atlas.MYSQL.MYSQL_InsertPlayerData.playerExists(MYSQL_InsertPlayerData.java:25) ~[?:?]
            at me.dabuseck.atlas.tools.checkers.OnJoin_Checker.onJoin(OnJoin_Checker.java:31) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79]
            at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[latest.jar:git-Spigot-8a048fe-a022dd2]
            ... 14 more
     
  2. What is there on line 25? *Show me*
    Next, wtf avoid static, uhh, does passing instance doesn't for you
     
    #2 Banbeucmas, Jun 1, 2016
    Last edited: Jun 1, 2016
  3. It doesn't look like you actually execute the statement anywhere, so getResultSet() will always be null.

    Look into https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeQuery()
    Don't forget to protect your code from SQL injection by using a '?' instead of uuid then inserting the uuid as a prepared parameter
    This code will lock your server's main thread while it is executing, consider using AsyncPlayerPreLoginEven instead
    Calling ResultSet::next twice will switch to the second row, which will always be null as you won't have two rows with the same player uuid
    Don't check if UUID is null in the result set, because if ResultSet::next isn't null, UUID won't be either, you need to check how many rows are in the result set instead (Hint: 0 rows means the player doesn't exist)
     
    • Winner Winner x 1
  4. This issue is with the public Main m;
     
  5. Try to pass the main instance instead, it should be easy
     
  6. 4chan

    Supporter

     
    • Optimistic Optimistic x 1
  7.  
Thread Status:
Not open for further replies.