What prompts me to write this reminder to myself is the adventure I had with time limits. The Choco documentation (a PDF file dated 01/20/2010, so it's pretty current as of this writing) mentions two methods, setTimeLimit and setCpuTimeLimit. NetBeans refuses to believe that the setCpuTimeLimit method (or anything starting with 'setCpu') exists. That's fine; setTimeLimit should work for me.
Unfortunately, my code started generating an unlabelled exception, which I could not catch even if I surrounded the solve() method (which lists no thrown exceptions) with a try ... catch block. Running the solve() method inside a SwingBackgroundTask probably didn't enhance my debugging efforts, either. I eventually deduced the exception was a consequence of hitting the time limit, but had a heck of time figuring out what was throwing it.
The solve() method returns a Boolean (not boolean) result. Eventually I paid attention to that and figured it out: if the solver hits its time limit, it returns null rather than true or false. I was using the return value in a conditional, as in
if (solver.solve()) { // do something with solution }
and that was causing a null pointer exception if the return value was null. Live and learn.
Thank you for pointing out that Boolean type!
ReplyDelete