5

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?

9
  • What kind of object is it? What result do you expect if it is any kind of object? Suppose the object is an Apple, or a House, or whatever, then want integer value would you expect? Commented Mar 30, 2017 at 12:51
  • try Integer.parseInt(oldComment.get("name").toString()) Commented Mar 30, 2017 at 12:51
  • It is of type java.lang.object Commented Mar 30, 2017 at 12:52
  • @NishadKAhamed Tried that also but didn't work Commented Mar 30, 2017 at 12:53
  • Please provide an example of one of your objects, and which Integer it should give, and why . Commented Mar 30, 2017 at 12:55

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Its a method from other lib is returning. I don't have a choice but to try and convert it
Ok so you can use my method or just only a Integer.parseInt(myObj) but you should catch the NumberFormatException to manage the empty String (maybe return 0 in this case ?)
your method is similar is this line right? (Integer.parseInt(oldComment.get("likes").toString()))
I am not sure why it is throwing NFE when there is a value inside it
Yes the result is the same. In your stacktrace, the is no value : java.lang.NumberFormatException: For input string: ""
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.