Priya Nalang
Priya Nalang

Reputation: 117

java.sql.SQLException: 'Infinity' is not a valid numeric or approximate numeric value

I am using a java function which returns a float

private float getEventScaledX(float eventX, float resX) {
    return (eventX * frameWidth) / resX;
}

While storing it under mysql db it throws exception as

Caused by: java.sql.SQLException: 'Infinity' is not a valid numeric or approximate numeric value

Please suggest a right way to handle it at java side to stop this exception but also to get right expected result.

Upvotes: 0

Views: 3899

Answers (1)

Priya Nalang
Priya Nalang

Reputation: 117

The issue here was (as reported by @c0der and @apomene ) resX coming as zero.

JAVA float data type does not throw any arithmetic exception while division by zero operation, rather returns Infinity / NaN as value. Which MySQL can't store as valid numeric type so the exception.

Checking for ( resX > 0 ) fixed my issue .

Upvotes: 1

Related Questions