I have a method which returns a object of type java.lang.object and I want to convert it to int. I tried this:
oldComment.get("count"); 
return a java.lang.object and I want to convert it to int. I tried:
(Integer)oldComment.get("count");
Integer.valueOf(oldComment.get("count"));
Integer.parseInt(oldComment.get("count"));
con = Base.connection();
String query = "UPDATE COMMENT SET LIKES = ? WHERE POST_ID = ?";
PreparedStatement pst = con.prepareStatement(query);
pst.setObject(1, (Integer.parseInt(oldComment.get("likes").toString())) + Integer.valueOf(rateComment.getCount()));
pst.setString(2, rateComment.getPost_id());
int k = pst.executeUpdate();
This is the code (Integer.parseInt(oldComment.get("likes").toString())) that is causing issue.
Error Stacktrace:
[qtp25844331-17] ERROR spark.http.matching.GeneralError -
java.lang.NumberFormatException: For input string: ""
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:592)
        at java.lang.Integer.parseInt(Integer.java:615)
        at com.soul.seeker.serviceImpl.CommentRatingServiceImpl.rateComment(CommentRatingServiceImpl.java:50)
        at com.soul.seeker.Application.lambda$main$12(Application.java:161)
        at spark.ResponseTransformerRouteImpl$1.handle(ResponseTransformerRouteImpl.java:47)
        at spark.http.matching.Routes.execute(Routes.java:61)
        at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:130)
        at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
        at org.eclipse.jetty.server.Server.handle(Server.java:517)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
        at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
        at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
        at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
        at java.lang.Thread.run(Thread.java:745)
None of the above worked. How do I convert it?
First of all, you shouldn't return java.util.Object, it's a very bad habit. If your value it's a Numeric, you should return java.lang.Number. If it's a String, you should return java.lang.String, etc.
If you don't have choice, you can convert it with this code :
// This method can throw NumberFormatException, catch it if you want
public Integer toInt(Object obj) {
    // Use intValue on a Number to improve performance
    if(obj instanceof Number) {
         return ((Number) obj).intValue();
    }
    return Integer.parseInt(obj.toString());
}
EDIT : In your stacktrace, your program try to parse an empty String so it throws an NumberFormatException, you should catch it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With