Wednesday, April 02, 2008

Java: Collest bug, ever seen!

Recently, I was a fixing a bug (which was introduced by me!). The bug is very silly, you may never catch it!

Issue is, a there is a java bean factory class with factory methods. In one of the factory method, a recursive call was there (which was not supposed to be), it was causing a stack overflow exception.

The factory method is supposed to be like this,

(I am providing a sample code, not actual code)

public Employee newEmployeeImpl()
{
return (Employee)new EmployeeImpl();
}

Instead of this, the code was mistakenly written as,

public Employee newEmployeeImpl()
{
return (Employee)newEmployeeImpl(); //there is no space in between new and EmployeeImpl!
}

It was accidentally typed without leaving a space between new and EmployeeImpl class. It is syntactically correct. So, the compiler didnt crip for the expression. But, at runtime, it goes as a recursive call!

So, the newEmployeeImpl() factory is getting called at infinite number of times and results in Stack Overflow Exception...

I laughed out to myself when I found this issue!

No comments: