Got error when try to put sql Timestamp in JsonArray.

1,172 views
Skip to first unread message

ROHIT KOUSHAL

unread,
Jun 23, 2015, 8:48:31 AM6/23/15
to ve...@googlegroups.com
Hello all,

I am facing weird issue when try to put Timestamp in JsonArray. I created the JsonArray in following way

JsonArray temp = new JsonArray().add(new Timestamp(System.currentTimeMillis()));

here is the error log.
java.lang.IllegalStateException: Illegal type in JsonObject: class java.sql.Timestamp
    at io
.vertx.core.json.impl.Json.checkAndCopy(Json.java:94)
    at io
.vertx.core.json.JsonArray.add(JsonArray.java:397)
    at com
.pluggedin.vertx.auth.AuthVertxAPI.authenticate(AuthVertxAPI.java:31)
    at com
.pluggedin.vertx.pip.PipMainVerticle.start(PipMainVerticle.java:102)
    at io
.vertx.core.AbstractVerticle.start(AbstractVerticle.java:111)
    at io
.vertx.core.impl.DeploymentManager.lambda$doDeploy$155(DeploymentManager.java:471)
    at io
.vertx.core.impl.DeploymentManager$$Lambda$6/891894789.handle(Unknown Source)
    at io
.vertx.core.impl.ContextImpl.lambda$wrapTask$15(ContextImpl.java:279)
    at io
.vertx.core.impl.ContextImpl$$Lambda$7/1103017075.run(Unknown Source)
    at io
.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:380)
    at io
.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
    at io
.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
    at java
.lang.Thread.run(Thread.java:745)



I did a quick research on it and found that checkAndCopy method of Json class does not have the support for sql Timestamp. Can anyone please help me here.

Paulo Lopes

unread,
Jun 23, 2015, 8:54:21 AM6/23/15
to ve...@googlegroups.com
Timestamp is not a valid JSON type: http://json.org/

You should either encode it as a long or as a String

ROHIT KOUSHAL

unread,
Jun 23, 2015, 2:19:54 PM6/23/15
to ve...@googlegroups.com
Thanks Paulo for you response. I give it a try but facing a new problem, not able to figure it out what went wrong

here is the code base
                    String insertQuery = "INSERT INTO TOKEN"
                           
+ " (TOKEN_ID, FROM_DATE, THRU_DATE, USER_LOGIN_ID)"
                           
+ " VALUES (?, ?, ?, ?)";
                   
JsonArray pramToInsert = new JsonArray().add(token.getString("tokenId"))
                           
.add(token.getLong("fromDate"))
                           
.add(token.getLong("thruDate"))
                           
.add(username);
                    client
.getConnection(res -> {
                       
if (res.succeeded()) {
                           
SQLConnection conn = res.result();
                           
System.out.println("Inserting Data " + pramToInsert);
                            conn
.updateWithParams(insertQuery, pramToInsert, insRes -> {
                               
if (insRes.succeeded()) {
                                   
UpdateResult updateResult = insRes.result();
                                   
System.out.println("No. of rows updated: " + updateResult.getUpdated());
                               
} else {
                                   
System.out.println("Failed to insert token in database");
                                    insRes
.cause().printStackTrace();
                               
}
                           
});
                       
}
                   
});



and here is the stack trace
java.lang.NullPointerException
    at java
.util.Objects.requireNonNull(Objects.java:203)
    at io
.vertx.core.json.JsonArray.add(JsonArray.java:396)
    at io
.vertx.ext.jdbc.impl.actions.JDBCUpdate.executeStatement(JDBCUpdate.java:53)
    at io
.vertx.ext.jdbc.impl.actions.JDBCUpdate.executeStatement(JDBCUpdate.java:33)
    at io
.vertx.ext.jdbc.impl.actions.AbstractJDBCStatement.execute(AbstractJDBCStatement.java:44)
    at io
.vertx.ext.jdbc.impl.actions.AbstractJDBCAction.handle(AbstractJDBCAction.java:48)
    at io
.vertx.ext.jdbc.impl.actions.AbstractJDBCAction.handle(AbstractJDBCAction.java:33)
    at io
.vertx.core.impl.ContextImpl.lambda$executeBlocking$14(ContextImpl.java:233)
    at io
.vertx.core.impl.ContextImpl$$Lambda$82/1994584263.run(Unknown Source)
    at io
.vertx.core.impl.OrderedExecutorFactory$OrderedExecutor.lambda$new$147(OrderedExecutorFactory.java:91)
    at io
.vertx.core.impl.OrderedExecutorFactory$OrderedExecutor$$Lambda$4/482052083.run(Unknown Source)
    at java
.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java
.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java
.lang.Thread.run(Thread.java:745)


Any help greatly appreciated. Thanks in advance.

Jez P

unread,
Jun 23, 2015, 2:34:25 PM6/23/15
to ve...@googlegroups.com
Please post more of your code including inputs and outputs. I'm guessing that one of your token.get... or username is returning null. Object.requireNonNull simply detects a null value and throws an exception.. Maybe you could try attaching a debugger to see what the input is. 
...

ROHIT KOUSHAL

unread,
Jun 23, 2015, 3:32:25 PM6/23/15
to ve...@googlegroups.com
Hi Jaz P

thanks for your reply. I also used below Json. but getting same error.
JsonArray pramToInsert = new JsonArray().add("4r0qvk9ifc86tpjsqdbkdetan8")
                         
.add(1435079782539L)
                         
.add(1435166182539L)
                         
.add("admin");

Jez P

unread,
Jun 23, 2015, 3:46:06 PM6/23/15
to ve...@googlegroups.com
I was wrong, it's not one of your inputs to that line that's causing the problem, it's how you're calling the JDBC.

Try a breakpoint on io.vertx.ext.jdbc.impl.actions.JDBCUpdate.executeStatement(JDBCUpdate.java:53) and see if you can spot what is null

Jez P

unread,
Jun 23, 2015, 3:50:38 PM6/23/15
to ve...@googlegroups.com
The line in question is

keys.add(convertSqlValue(rs.getObject(1)));

convertSQLValue converts a null to a null, so that would imply that rs.getObject(1) is returning null. You might need to debug and look at what rs.getObject(1) is doing.

rohit....@hotwaxsystems.com

unread,
Jun 25, 2015, 5:24:54 AM6/25/15
to ve...@googlegroups.com
Hi all,

I think this code is causing problem 
// Retrieves any auto-generated keys created as a result of executing this Statement object. If this  Statement object did not generate any keys, an empty ResultSet object is Returned. 
    ResultSet rs = statement.getGeneratedKeys();
    JsonArray keys = new JsonArray();
    while (rs.next()) {
      keys.add(convertSqlValue(rs.getObject(1)));
    }

getGeneratedKeys method return the list auto-generated keys, but my table does not have that, still it is returning 1 with empty value. Any suggestion what wrong with it.

Thanks again

rohit....@hotwaxsystems.com

unread,
Jun 26, 2015, 4:10:00 AM6/26/15
to ve...@googlegroups.com
I also tried by adding an column which have auto generated values. but it doesn't help still facing problem. Query for the table creation is mention below
CREATE TABLE TOKEN (ID int GENERATED ALWAYS AS IDENTITY, TOKEN_ID VARCHAR(255), USER_LOGIN_ID VARCHAR(20), FROM_DATE VARCHAR(20), THRU_DATE VARCHAR(20))
Reply all
Reply to author
Forward
0 new messages