• Post Reply Bookmark Topic Watch Topic
  • New Topic

Whats your biggest Java headache(s) caused by the smallest/simplest mistake?  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What small thing did you over look or perhaps didn't click-in/notice right away for you that caused so much frustration?

Awhile back ago, I had a difficult time parsing/splitting a CSV(Comma Seperated Values) file that had fields with commas in them "Plumbing & Cleaning, Inc." and "$1,999.00". So I read up on Regular Expressions hoping to get some voodoo magic working for a few days.
Then I realized I could just excel to select all the Strings and Integers, find & replace all the "," with "". Bam, all extra commas gone. Don't know how this slipped my mind.

How about yourself? What can we learn from your pain and suffering?
 
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure it's going to be whatever I did wrongly in setting up a connection pool, I just looked at the logs and I see that it's closing pooled connections immediately instead of keeping them open for 5 minutes as I thought I had told it. So far everything I've done looks kosher but for sure it's my fault. I just have to find out why.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Matthew wrote:Then I realized I could just excel to select all the Strings and Integers, find & replace all the "," with "". Bam, all extra commas gone. Don't know how this slipped my mind.

That really isn't a mistake, is it? It's more of a "Doh!" moment after realizing that you've been looking for a complex solution where a simpler and more straightforward one was right there all along.

The stray semicolon is common simple Java mistake:

Trying to compare a boolean variable to true or false and accidentally using = instead of ==:
 
Paul Clapham
Sheriff
Posts: 22845
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I'm pretty sure it's going to be whatever I did wrongly in setting up a connection pool, I just looked at the logs and I see that it's closing pooled connections immediately instead of keeping them open for 5 minutes as I thought I had told it. So far everything I've done looks kosher but for sure it's my fault. I just have to find out why.


It turns out that the connection pool is working correctly and my configuration is correct also. I was just looking at the logs when my application was asking for more connections than the pool maximum so there was a lot of creating extra connections going on. So there you go -- my mistake was in assuming the configuration wasn't what I thought it was. I might have to up the maximum connections, that's all.

What can you learn from that? Not much, I guess, except to notice that sometimes your error isn't an error at all, and it's quite possible to spend a bunch of time looking for errors where they don't exist.
 
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was during my initial days as programmer.
We had some Java processes in Unix box to do transactions in a bank.
Unfortunately I did not have the check for already running Java process in the shell script before starting it.
Unknowingly I started a process twice which credits fund into customers' account.
After some time we came to know what happened and stopped the Java process immediately.
By that time 73 accounts were credited twice.
The banker immediately started debiting the accounts, but few customers had withdrawn money.
It was headache for bankers to get the money from those customers.

It was the first mistake.
Then we traced why the heck our Java code did allow a transaction twice and found the loophole after few hours and corrected the second mistake.
 
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Matthew wrote:What small thing did you over look or perhaps didn't click-in/notice right away for you that caused so much frustration?

Awhile back ago, I had a difficult time parsing/splitting a CSV(Comma Seperated Values) file that had fields with commas in them "Plumbing & Cleaning, Inc." and "$1,999.00". So I read up on Regular Expressions hoping to get some voodoo magic working for a few days.
Then I realized I could just excel to select all the Strings and Integers, find & replace all the "," with "". Bam, all extra commas gone. Don't know how this slipped my mind.

How about yourself? What can we learn from your pain and suffering?


I have a Jtable with about 8 rows visible in the GUI before it'll scroll. I NEVER added more than 5 rows to it before tonight even though its been functional for a month. I figured it would scroll when I add more no problem. Well, that didn't happen. My scrollbar on the Jtable wouldn't move and I couldn't figure out why. After 2.5 hours of worry,research, and staring at it, it turned out that my Preferred Height of the table was set to the height of the table itself. All I had to do was increase the Preferred Height to a larger value and it worked.

All these little things add up and you learn from it so they aren't all that bad.


 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Zak wrote:All these little things add up and you learn from it so they aren't all that bad.

Learning from your own mistakes is the best thing.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Matthew wrote:How about yourself? What can we learn from your pain and suffering?

Probably not much, but I still forget sometimes that numeric types overflow (and underflow) silently.

One example is this innocuous looking snippet:
  for (char c = 0; c < 65536; c++) { ...
I call it the "Ever-Ready Bunny loop" because it just keeps going, and going, and going....

The classic one of course is:
  mid = (high + low) / 2;
which went undiscovered for 6 years after publication of the first "binary chop" algorithm (and I still see it done today).

Which just goes to prove that even the most brilliant programmer can make really basic mistakes - which I actually find kind of comforting .

And if any further proof was needed, here's another from a class written by none other than Joshua Bloch:and the public bitLength() method isn't much better - yet both are used copiously inside BigInteger.

I've written to Oracle about it a few times (the first about 9 years ago), but haven't received any response - presumably because nobody has actually run into the problem yet; but it IS a time-bomb waiting to happen.

Winston
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . . And if any further proof was needed, here's another from a class written by none other than Joshua Bloch . . .
Bloch does confess to another mistake with BigInteger/BigDecimal. He forgot to mark the classes final.

Maybe you haven't had a response from Sun/Oracle is that nobody can understand enough of that code to know what will go wrong
BigInteger does include a private constant called MAX_MAG_LENGTH which appears to be equal to 2²⁶; if you multiply that by 32 you get 2³¹, so that will probably prevent overflow in the bit length methods. It also says
BigInteger constructors and operations throw ArithmeticException when the result is out of the supported range of -2^Integer.MAX_VALUE (exclusive) to +2^Integer.MAX_VALUE (exclusive).
I haven't bothered to work out what 2²¹⁴⁷⁴⁸³⁶⁴⁷ − 1 is, but you need a pinch of salt to take with the following:-
"infinite word size" abstraction provided by this class.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:BigInteger does include a private constant called MAX_MAG_LENGTH which appears to be equal to 2²⁶; if you multiply that by 32 you get 2³¹, so that will probably prevent overflow in the bit length methods.

It does? I've looked through the Sun/Oracle source for 1.8, 1.7 and 1.5, and I can't find it anywhere, in BigInteger or MutableBigInteger.

It also says: "BigInteger constructors and operations throw ArithmeticException when the result is out of the supported range of -2^Integer.MAX_VALUE (exclusive) to +2^Integer.MAX_VALUE (exclusive)".

Now that I have found - but only in version 8. And I haven't yet found where it's actually enforced - probably in some internal method - but it's certainly not in the constructors themselves.

But the fact is that it was wrong. "Bit length" should have been a long right from outset; and IMO adding a constant to limit the size is a "poor man's" solution - although if you're going to do it that way, construction is probably the best time to do it (albeit "after the horse has gone" ).

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I haven't bothered to work out what 2²¹⁴⁷⁴⁸³⁶⁴⁷ − 1 is, but you need a pinch of salt to take with the following:- "infinite word size"...

Yes, and it is a VERY big number - ≈646 million digits - but given the sorts of things that BigInteger might be used for, I find it surprising that the bitLength() limit hasn't been breached yet.

Or maybe it has and we don't know...

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to breach it yesterday—and failed getting an out of memory error because of insufficient heap space.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!