How to debug java.lang.NullPointerException in business rule

jean-lucchatton
Kilo Guru

Hello,

When I've looked in the sytem log, I've found a lot of "debug java.lang.NullPointerException"   triggered from a   business rule.

In the records in the system log, the whole business rule is contained in the "message" field. Unfortunately, the line that triggered the exception is not marked. Is there a possibility to find it without doing a trial and error debuggind with gs.log ?

Thanks for you help

Jean-Luc

8 REPLIES 8

Chuck Tomasi
Tera Patron

Hi Jean-Luc,



Good of you to check the logs! Bonus points!



Debugging these is tricky. Somewhere in that stack trace is some indicator where the issue lies, but reading those is difficult (even for experienced ServiceNow users.) I often look for things that COULD have the value of null. Dot-walking is always a good place to start.



Example



var mgr = current.assignment_group.manager;



If assignment_group is empty, BOOM!



A better way



var mgr;


if (current.assignment_group)


        mgr = current.assignment_group.manager;



Hope that helps.



Coding Best Practices - ServiceNow Wiki


Hi Chuck,



Thanks for the hints.



A naive question: In the ServiceNow implementation of Javascript, is there a 'linenumber' attribute of the error object ? In that case, could be a solution to wrap the code of the business rule with a try-catch instruction and then to log the linenumber with an exception is raised ?



Regards


Jean-Luc


While you can do try/catch (and it's a general good practice regardless), I don't know if it can throw the line number.



Another good practice is to keep your scripts small, use script includes with small functions.



All the technical best practices (learned from years of experience) are documented here:;


Technical Best Practices - ServiceNow Wiki



Many are explained and demonstrated in this series of videos:


TechNow Episode List  


Hi Chuck,



Thanks for your advices.



For your info, I've summited the case to the SN-Support. A couple of points :


- If there is a null pointer exception then it's a severe error and the Javascript error handler could not help to find the issue


- The Javascript error object from SN has the line indication. It will show the line that trigger the error if it is not too severe. The example that you gave is in this case, i.e. it do not trigger a Java exception


- It is a pitty that we - the customers - do not have access to all the log. What was in the log that showed me the SN engineer allowed to find to root cause of the incident



Regards


Jean-Luc