Solved Inerting data into database [NPE]

Discussion in 'Plugin Development' started by envic, Sep 18, 2016.

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

    envic

    Hi guys.
    I'm using MAMP for creating MySQL server!
    and i used JDBC for connection!

    but in inserting data, i got an error! Null Pointer Exception!


    I tried:
    Code:
    PreparedStatement newPlayer = main.connection.prepareStatement("INSERT INTO `player_data` values(?,0);");
    newPlayer.setString(1, player.getName());
    newPlayer.execute();
    newPlayer.close();
    
    and:
    Code:
    PreparedStatement newPlayer = main.connection.prepareStatement("INSERT INTO `player_data`(`player`, `player_coins`) VALUES (?,0);");
    newPlayer.setString(1, player.getName());
    newPlayer.execute();
    newPlayer.close();

    Crash Log:
    Code:
    [17:39:51] [Server thread/WARN]: java.lang.NullPointerException
    [17:39:51] [Server thread/WARN]:     at envica.coins.shop.commands.commands.onCommand(commands.java:65)
    [17:39:51] [Server thread/WARN]:     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [17:39:51] [Server thread/WARN]:     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
    [17:39:51] [Server thread/WARN]:     at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645)
    [17:39:51] [Server thread/WARN]:     at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1302)
    [17:39:51] [Server thread/WARN]:     at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1137)
    [17:39:51] [Server thread/WARN]:     at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)
    [17:39:51] [Server thread/WARN]:     at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)
    [17:39:51] [Server thread/WARN]:     at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13)
    [17:39:51] [Server thread/WARN]:     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    [17:39:51] [Server thread/WARN]:     at java.util.concurrent.FutureTask.run(Unknown Source)
    [17:39:51] [Server thread/WARN]:     at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45)
    [17:39:51] [Server thread/WARN]:     at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:716)
    [17:39:51] [Server thread/WARN]:     at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400)
    [17:39:51] [Server thread/WARN]:     at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:655)
    [17:39:51] [Server thread/WARN]:     at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:554)
    [17:39:51] [Server thread/WARN]:     at java.lang.Thread.run(Unknown Source)
    Happy to hear your help :)

     
  2. Offline

    bwfcwalshy Retired Staff

    Line 65?
     
  3. Offline

    Matthiaantje

    Null Pointer Exception,
    maybe you need to add this...
    Code:
    try
    {
        your code here...
    }
    catch (NullPointerException) ex
    {
        handle the exception here...
    }
    
    Hope this helps!
     
  4. Offline

    timtower Moderator Moderator

    @Matthiaantje Unless you can prevent it all together of course.

    @envic I assume that main or connection is null.
     
  5. Offline

    mythbusterma

    @envic

    You don't need the entire MAMP suite just to have the MySQL server. It is a standalone piece of software that you can install from Oracle's website.

    Anyway, avoid public feilds for this exact reason. You should have one class that's responsible for handling the MySQL connections, writing, etc. and you shouldn't do this on the main thread or chat threads. Also, consider that you likely don't need MySQL for what you're doing, and using configuration YAML files is better in almost every conceivable way.

    @Matthiaantje

    A NullPointerException is a subclass of "RuntimeException," exceptions that do not need to be declared. These exceptions have one purpose: telling the programmer they fucked up. In general, do not handle RuntimeExceptions. You should look for problems in your code, rather than sweep them under the rug.
     
    I Al Istannen likes this.
  6. Offline

    I Al Istannen

    @mythbusterma
    Any advice on NumberFormatException? How do you check that, without using exceptions for you program flow?
     
  7. Offline

    frostalf

    @I Al Istannen You need to catch it like any other exception

    Code:
    try {
    code here of what might throw some kind of exception
    } catch (NumberFormatException ex) {
    do something here when exception is thrown
    }
    
    @envic I notice you are only setting the player name but your sql database from your prepared statement also needs a value for coins and I don't see where you are setting that. Also, you need to surround your sql code in a try catch so that it doesn't crash your server.

    Code:
    try{
    PreparedStatement newPlayer = main.connection.prepareStatement("INSERT INTO `player_data`(`player`, `player_coins`) VALUES (?,0);");
    newPlayer.setString(1, player.getName());
    newPlayer.execute();
    newPlayer.close();
    
    } catch (SQLException ex) {
    do something here upon exception
    }
    
    now because its a NullPointerException. It is most likely not your SQL connection doing it, as that will throw a SQLException not an NPE. So it is most likely somewhere in your commands.java around line 65 that is causing the issue.
     
  8. Offline

    mythbusterma

    @frostalf

    It wouldn't crash the server, the contexts a plugin runs in all will handle "Exception," the Bukkit server does this so that you can't crash it under normal circumstances.

    Always log exceptions, or handle them appropriately.


    @I Al Istannen

    That particular exception is one that you have to catch, there are ways of parsing the number beforehand to make sure it's actually a number, but it's more trouble than it's worth. The key is to not use Exceptions for normal program flow, or major portions of flow in general. Getting a String when you expect a number is indeed not normal and should be handled, but it should be handled as soon as possible (i.e. don't pass it up).

    It's one thing about user input, you never know what you're going to get, so you do have to catch these exceptions.
     
  9. Offline

    I Al Istannen

  10. Offline

    envic

    wow,
    i was in a vacation and i hadn't internet connection! so i couldn't check your answers!
    but a way that i solved it with creating a SQLManager class!
    and i recode it and now it works fine!

    but thanks for all answers :p
     
  11. Offline

    bwfcwalshy Retired Staff

    @envic Please mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page