Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Debugging Script, Type Error when checking for Null

1608502124

Edited 1608502392
Elgin
Pro
Marketplace Creator
I have a script that works well enough on one game, but breaks once imported to another. Here is the snippet in question    debugVariable = function(target_var) {     sendChat('',JSON.stringify (target_var)); }, ......         nl.get('notes', function(n){                     debugVariable('hhh2');                     debugVariable(n);             if(!_.isNull(n)){ This basically looks up the content of a Journal entry and is nearly copy pasted from another script called JournalLog. The fourth/fifth line sets 'n' to the contents of the log correctly and is outputted into the chat on line sixth/seventh. The final line, for some unknown reason, throws a fit and dies TypeError: Cannot read property '0' of null TypeError: Cannot read property '0' of null at incrementDate (apiscript.js:3590:56) at Timeout._onTimeout (apiscript.js:3514:30) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7) I cannot understand for the life of me why this would fail here when it runs fine everywhere else Any ideas on what mistake I am making? Since the error is referencing incrementDate, even though it doesn't make it that far....here is some more of the code }, incrementDate = function(journal_contents,args) {   debugVariable('bla');// Doesn't output ....... }, passTime = function(args) {         nl = getNoteLog();                     debugVariable('hhh1');   // outputs fine         nl.get('notes', function(n){                     debugVariable('hhh2'); //outputs fine                     debugVariable(n); //outputs fine             if(!_.isNull(n)){                     debugVariable('hhh3'); //Doesn't output                 setTimeout(function(){                     debugVariable('hhh4');                     let text=incrementDate(n,args);                     debugVariable(text);                     sendChat('',`/desc ${getDate(text,'short')}`);                     nl.set('notes',text);                 },0);             }         }); },
1608503271
The Aaron
Roll20 Production Team
API Scripter
Can you post all of IncrementDate?  sendChat() is asynchronous, so a failure further in the function could prevent the sendChat from occurring, even if the failure is further along in the code.
1608503717
The Aaron
Roll20 Production Team
API Scripter
.match() on a string returns the special value null when it doesn't find a match.  So this line: var results = journal_contents.match(/.*>It is (\d+) seconds past (\d+):(\d+) (\w+) (\w+) \(\w+\) (\d+).. year (\d+)<\/h1>/i); is failing to match the contents and is returning null .  Then this line is trying to deference index 0 from it:   var remainder = journal_contents.substring(results[0].length); There are a number of ways you might fix that, depending on what you're after.  As of Javascript ES6, there's no problem comparing to null directly, so you could just check and return:   var results = journal_contents.match(/.*>It is (\d+) seconds past (\d+):(\d+) (\w+) (\w+) \(\w+\) (\d+).. year (\d+)<\/h1>/i); if(null === results){ return; } etc.
1608503969

Edited 1608504031
Elgin
Pro
Marketplace Creator
Nevermind. Thank you I figured it must be that line, but since the debugVariable wasn't outputting I was looking elsewhere