[Solved] java.lang.NumberFormatException: For input string: "null"

Discussion in 'Plugin Development' started by Kajiakuma, Aug 2, 2011.

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

    Kajiakuma

    I have an error wich I can't solve. This is the error message:

    Code:
    2011-08-02 18:02:27 [SEVERE] Could not pass event PLAYER_QUIT to OnlineTime
    java.lang.NumberFormatException: For input string: "null"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Long.parseLong(Long.java:441)
        at java.lang.Long.parseLong(Long.java:483)
        at de.Kajiakuma.OnlineTime.OnlineTimePlayerListener.onPlayerQuit(OnlineTimePlayerListener.java:145)
        at org.bukkit.plugin.java.JavaPluginLoader$2.execute(JavaPluginLoader.java:251)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:332)
        at net.minecraft.server.ServerConfigurationManager.disconnect(ServerConfigurationManager.java:146)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:608)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:231)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:85)
        at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
        at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:451)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:361)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    The error only occures in normal mode (mysql.enable set to false). In mysql mode no error.

    The part of the code were the error occures:

    Code:
        /**
         * @description calculates the players playtime for last session and add it to the complete playtime on player quit. If the client crashes, no data will be stored
         * @param event
         */
        @Override
        public void onPlayerQuit(PlayerQuitEvent event){
            Player player = event.getPlayer();
            if(player.getName() !=null){
    
            // playtime calculation follows here
            Calendar cal = Calendar.getInstance();
            Date now = cal.getTime();
            Timestamp timestamp = new Timestamp(now.getTime());
            Timestamp lastlogin = null;
            long playedalltime = 0;
            long playedlasttime = 0;
    
            if(plugin.isMysqlEnabled()==true){
            try {
                Database.openConnection();
                Database.query("SELECT Player, MAX(Date) AS lastlogin FROM Online WHERE Player='"+player.getName()+"'");
                Database.rs.next();
                if(Database.rs.getString("lastlogin")!=null)
                lastlogin = Timestamp.valueOf(Database.rs.getString("lastlogin"));
                Database.query("SELECT Player, playtimeever FROM Playtime WHERE Player='"+player.getName()+"'");
                Database.rs.next();
                if(Database.rs.getString("playtimeever")!=null)
                    playedalltime = Long.parseLong(Database.rs.getString("playtimeever"));
                Database.closeConnection();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
            else{
                if(plugin.getPlayer(player.getName()+".playedalltime")!=null){
    /* Line 145: */ playedalltime = Long.parseLong(plugin.getPlayer(player.getName()+".playedalltime"));
                }
                if(plugin.getPlayer(player.getName()+".lastlogin")!=null){
                    lastlogin = Timestamp.valueOf(plugin.getPlayer(player.getName()+".lastlogin"));
                }
            }
    
            playedlasttime = Math.abs(timestamp.getTime () - lastlogin.getTime ());
            playedlasttime = playedlasttime / 60000;
            playedalltime += playedlasttime;
            try {
                plugin.setPlayer(player.getName(), null, null,playedalltime,playedlasttime);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    Funktion getPlayer() and setPlayer() from main class:

    Code:
    /**
         * @description returns a single value from the config file from the defined yaml path
         * @param path
         * @return string
         */
        public String getPlayer(String path){
            Object object = config.getProperty(path);
            String string = String.valueOf(object);
            config.load();
            return string;
        }
    
        /**
         * @description create or update a path in the Mysql Database or config.yml
         * @param player
         * @param firstlogin
         * @param lastlogin
         * @param playedalltime
         * @param playedlasttime
         * @throws SQLException
         * @throws InstantiationException
         * @throws IllegalAccessException
         * @throws ClassNotFoundException
         */
        public void setPlayer(String player, Timestamp firstlogin, Timestamp lastlogin, long playedalltime, long playedlasttime) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
            Configuration config = getConfiguration();
    
            if(isMysqlEnabled()==false){
    
            if(firstlogin!=null){
                config.setProperty(player+".firstlogin",firstlogin);
                config.save();
            }
    
            if(lastlogin!=null){
                config.setProperty(player+".lastlogin",lastlogin);
                config.save();
            }
    
            if(playedalltime!=0){
                config.setProperty(player+".playedalltime",playedalltime);
                config.save();
            }
    
            if(playedlasttime!=0){
                config.setProperty(player+".playedlasttime",playedlasttime);
                config.save();
            }
            }
            if(isMysqlEnabled()==true){
                Database.openConnection();
                if(lastlogin!=null)
                Database.query("INSERT INTO Online (Player,Date) VALUES ('"+player+"','"+lastlogin+"')");
                if(playedalltime!=0){
                    Database.query("SELECT Player FROM Playtime WHERE Player='"+player+"'");
                    Database.rs.next();
                    if(Database.rs.getString("Player").isEmpty()==true){
                        Database.update("INSERT INTO Playtime (Player,playtimeever, playtimelast) VALUES ('"+player+"','"+playedalltime+"','"+playedlasttime+"')");
                    }
                    else
                        Database.update("UPDATE Playtime SET Player='"+player+"',playtimeever='"+playedalltime+"', playtimelast='"+playedlasttime+"'");
                }
    
                Database.closeConnection();
            }
        }

    stored data in config.yml (only data from me, not other players):
    Code:
    Mysql:
        dbpass: ****
        dbuser: ni7868_1_DB
        dburl: jdbc:mysql://localhost/ni7868_1_DB
        enable: 'false'
    Nero90:
        firstlogin: 2011-07-26T18:56:01.380Z
        lastlogin: 2011-08-01T22:11:48.370Z
    If I quit the game and type later "/onlinetime player Nero90" it would return my playtime, but it only returns "null". If I open config.yml no playtime data is stored. If I switch to mysql mode ( /onlinetime setmysql true) everything is ok, playtime data stored and returned as chat message on command. Why does this not work in normal mode and why did this error occure? Timestamp format seems to be correct in config.yml. I have no idea how to solve this.

    I hope someone helps me. Thanks for your efforts.
     
  2. Offline

    masteroftime

    which line is line 145 in OnlineTimePlayerListener?
     
  3. Offline

    Nitnelave

    Well, the problem is that the online time is not in the config. It checks for it, and as it can't be fund, the getPlayer method retruns null, thus triggering the error. You have to look for the reason why it didn't get written.
    And this is it : in the setPlayer, you check for firstTime != null, which will always return false (at least, from the only call I've seen), same thing for last Login.
     
  4. Offline

    Kajiakuma

    Alright, one problem solved, another remaining.
    java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
    at line:
    lastlogin = Timestamp.valueOf(plugin.getPlayer(player.getName()+".lastlogin"));
    And the line wich is read in config.yml:
    lastlogin: 2011-08-02T17:42:04.613Z

    This is a normal Timestamp, but why did this error occure? Or do I oversight something?
     
  5. Offline

    Nitnelave

    Maybe when you write to the database you could first convert the timestamp to a string, and reaplce the "t" by a space, and delete the Z. That would fit the format, I think.
     
  6. Offline

    Kajiakuma

    Alright, last problem solved. I had written the lastlogin as timestamp into the config, wich creates these 't' and 'z'. I changed it to lastlogin.toString() instead of lastlogin to write it into the config. Thanks for your tipps
    Thread can be closed.
     
  7. Offline

    Tster

    Dont think anyone closes them, just leave it to die
     
  8. Offline

    Nitnelave

    If you want to, you can edit the thread title to add [SOLVED] at the beginning. (thread tools -> edit thread)
     
Thread Status:
Not open for further replies.

Share This Page