NullPointerException

Discussion in 'Plugin Development' started by Clybzotik, Feb 9, 2019.

  1. Offline

    Clybzotik

    Hi, when I join to the server, I get this java.lang.NullPointerException: null

    PlayerJoinEvent:
    Show Spoiler
    Code:
    @EventHandler
        public void join(PlayerJoinEvent e) {
            e.setJoinMessage("");
            Player player = e.getPlayer();
            String nick = player.getName();
            sqm.FirstConnectionInsert(nick);
            return;
        }
    

    sqm.FirstConnectionInsert
    Show Spoiler
    Code:
        public void FirstConnectionInsert(String nick) {
            Connection c = null;
            Statement stmt = null;
            try {
                c = DriverManager.getConnection("jdbc:sqlite:" + main.getDataFolder() + File.separator + "Users.db");
                stmt = c.createStatement();
                String sql = "INSERT OR IGNORE INTO USERS (ID, NICK, KITS, BALANCE, KILLS, DEATH) " +
                        "VALUES (null, '"+ nick + "', 0, 0 , 0, 0 );";
                stmt.executeUpdate(sql);
                stmt.close();
                c.close();
            } catch ( Exception e ) {
                 System.err.println( e.getClass().getName() + ": " + e.getMessage() );
                 System.exit(0);
            }
        }
       
    
     
  2. @Clybzotik Can you show us the stack trace with a screenshot?
     
  3. Offline

    timtower Ninja on the waves Moderator

    @Clybzotik Don't use System.exit in plugins, just disable the plugin or throw the error.
    Try e.printStackTrace() as well
     
  4. Offline

    Clybzotik

    Show Spoiler

    Code:
    [20:42:22] [Server thread/WARN]: java.lang.NullPointerException: null
    [20:42:22] [Server thread/WARN]: java.lang.NullPointerException
    [20:42:22] [Server thread/WARN]:     at kpvp.main.SQLiteManager.FirstConnectionInsert(SQLiteManager.java:20)
    [20:42:22] [Server thread/WARN]:     at kpvp.main.Handler.join(Handler.java:38)
    [20:42:22] [Server thread/WARN]:     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [20:42:22] [Server thread/WARN]:     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    [20:42:22] [Server thread/WARN]:     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    [20:42:22] [Server thread/WARN]:     at java.lang.reflect.Method.invoke(Unknown Source)
    [20:42:22] [Server thread/WARN]:     at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302)
    [20:42:22] [Server thread/WARN]:     at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    [20:42:22] [Server thread/WARN]:     at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500)
    [20:42:22] [Server thread/WARN]:     at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485)
    [20:42:22] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.PlayerList.onPlayerJoin(PlayerList.java:346)
    [20:42:22] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.PlayerList.a(PlayerList.java:166)
    [20:42:22] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.LoginListener.b(LoginListener.java:159)
    [20:42:22] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.LoginListener.e(LoginListener.java:57)
    [20:42:22] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.NetworkManager.a(NetworkManager.java:233)
    [20:42:22] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.ServerConnection.c(ServerConnection.java:140)
    [20:42:22] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:845)
    [20:42:22] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406)
    [20:42:22] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679)
    [20:42:22] [Server thread/WARN]:     at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577)
    [20:42:22] [Server thread/WARN]:     at java.lang.Thread.run(Unknown Source)
    [20:42:23] [Server thread/INFO]: Clybzotik[/127.0.0.1:52814] logged in with entity id 104 at ([world]146.5, 69.0, 247.5)
    
     
  5. Offline

    timtower Ninja on the waves Moderator

  6. Offline

    Clybzotik

    This: c = DriverManager.getConnection("jdbc:sqlite:" + main.getDataFolder() + File.separator + "Users.db");
     
  7. Offline

    timtower Ninja on the waves Moderator

    @Clybzotik Post the full class please, spoilers are not needed for it
     
  8. Offline

    Clybzotik

    Code:
    package kpvp.main;
    
    import java.io.File;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class SQLiteManager {
    
        private KitPVP main;
        public SQLiteManager(KitPVP main) {
            this.main = main;
        }
    
        public void FirstConnectionInsert(String nick) {
            Connection c = null;
            Statement stmt = null;
            try {
                Class.forName("org.sqlite.JDBC");
                c = DriverManager.getConnection("jdbc:sqlite:" + main.getDataFolder() + File.separator + "Users.db");
                stmt = c.createStatement();
                String sql = "INSERT OR IGNORE INTO USERS (ID, NICK, KITS, BALANCE, KILLS, DEATH) " +
                        "VALUES (null, '"+ nick + "', 0, 0 , 0, 0 );";
                stmt.executeUpdate(sql);
                stmt.close();
                c.close();
            } catch ( Exception e ) {
                 System.err.println( e.getClass().getName() + ": " + e.getMessage() );
                 e.printStackTrace();
            }
        }
      
        public boolean TestFirstConnect(String nick) {
            Connection c = null;
            Statement stmt = null;
            try {
                c = DriverManager.getConnection("jdbc:sqlite:" + main.getDataFolder() + File.separator + "Users.db");
                stmt = c.createStatement();
                ResultSet rs = stmt.executeQuery( "SELECT NICK FROM USERS WHERE NICK ='" + nick + "';" );
                if (!rs.isBeforeFirst()) {
                    return false;
                }
                stmt.close();
                c.close();
            } catch ( Exception e ) {
                 System.err.println( e.getClass().getName() + ": " + e.getMessage() );
                 System.exit(0);
            }
            return true;
        }
        public void getPlayerStats(String nick) {
            // Чото будет
            return;
        }
    }
     
    Last edited by a moderator: Feb 9, 2019
  9. Offline

    timtower Ninja on the waves Moderator

    @Clybzotik Spoilers are not needed, but please keep using the code blocks.
    What is null? Does the file exist?
     
  10. Offline

    Clybzotik

    Yes it exists.

    Does anyone know how to fix this?

    UPD: @timtower

    Removed plugin.getDataFolder from the connection and the error was solved. Apparently the problem is in the inheritance of the main class.
     
    Last edited: Feb 10, 2019 at 4:22 AM

Share This Page