Error in script, yet it does work

Posts: 61
Hey there,

I recently noticed that 1 of my scripts is throwing an error: Object reference not set to an instance of an object.
However, the script works but slows down after a while (seen with script performance).

This is the script, it is focused for class standings (class gaps to be exact).
public object Execute(ThemeContentItem item, object value, string parameter, ISimulation sim)
{
// Bind to "entitysessionresult_obj" to get the IEntitySessionResult as the value

// Cast to IEntitySessionResult type
IEntitySessionResult result = (IEntitySessionResult) value;

// Check DNS, Out, Pits, etc

if (result.Out)
return "OUT";


// We need the following information to determine whether to show interval or laps
var pos = result.ClassPosition;
var gap = result.ClassGap;
var laps = result.ClassGapLaps;
var type = result.Session.Type;

if (result.ClassPosition == 1)
return "LAP " + result.CurrentLap.Number;

// If car is lapped AND this is a race session,
// then show nr of gap laps instead of gap time
if (laps > 0 && type == SessionType.Race)
{
if (laps == 1)
return "+1 Lap"; // to avoid incorrect "1 Laps"
else
return "+" + laps + " Laps";
}

// If the car is in the same lap just return the time
// If the car is in same lap but over a minute behind, convert total seconds to proper format
var min = 0;
float sectime;
if (gap > 60)
{
min = (int) (gap / 60);
sectime = gap % 60;
return "+" + min + ":" + sectime.ToString("00.000");
}
else
return "+" + gap.ToString("0.000");
}


Can somebody help me and tell what I'm doing wrong? This script is based on the "Interval script" that was posted in an earlier thread on this website.
I adjusted that script for multiple tickers without any issues, its just when I tried this for specific class standings, it gives an error.

Any help is appreciated!
Posts: 287
I have got the same pb, and now it is ok. I work on a laptop and I don't really put it OFF. I don't sure but it seems to be OK after a complete restart of computer.
Posts: 61
Emmanuel Suter wrote:
I have got the same pb, and now it is ok. I work on a laptop and I don't really put it OFF. I don't sure but it seems to be OK after a complete restart of computer.

I restart my system every day. Error keeps popping up in the logs. I tried commenting out certain sections of this script to identify the error source, but with no avail.
Edited (1 time)
Posts: 287
What have you got on the top of script ?
Posts: 287
Oppps, my problem is already here. Yesterday I think it solve but no .... so same as you with same sort of script for gap in ticker.
Posts: 61
Emmanuel Suter wrote:
What have you got on the top of script ?

I assume you ask for this?
using System;
using ATVO.ThemesSDK;
using ATVO.ThemesSDK.Data.Entity;
using ATVO.ThemeEditor.ThemeModels;
using ATVO.ThemeEditor.Controls;
using ATVO.ThemeEditor.Scripting.DotNET;
using ATVO.ThemesSDK.Data.Enums;
using ATVO.ThemesSDK.Data.Results;
using System.Windows.Media;
Posts: 61
Emmanuel Suter wrote:
Oppps, my problem is already here. Yesterday I think it solve but no .... so same as you with same sort of script for gap in ticker.

My script for gaps for every single car is working without any problems, it's when I try to single them out for each class it throws an error, yet still works..
Posts: 785
You can debug these kind of problems by adding logging to the script. To log something to the Event Log you can add this:
Console.WriteLine("Write this to the event log");


You can add this line in multiple places to see exactly which part it does not reach.


The error says that you are trying to access an object with no value (a "null" object). To avoid these errors you have to check if any object you try to access is not null.

In the script you posted there are at least two places where this can happen:
1. The 'value' itself can be null if there is no 'entity session result' available.
2. The result.CurrentLap object may be null in rare cases too I think.

To avoid the error simply add these checks where appropriate:
if (value == null)
{
// do something appropriate, in this case probably just return nothing
return null;
}


You can do the same for 'result.CurrentLap'.
Posts: 785
For future reference: leaving these errors in your script is bad. We make sure an error in a script does not crash ATVO so we catch these errors when they happen. However, this is very costly in terms of performance especially if it keeps happening 30 times per second.

Adding this "value == null" check is extremely fast and does not cause any performance hit, so try to add these checks as much as possible.
Posts: 61
Nick Thissen wrote:
For future reference: leaving these errors in your script is bad. We make sure an error in a script does not crash ATVO so we catch these errors when they happen. However, this is very costly in terms of performance especially if it keeps happening 30 times per second.

Adding this "value == null" check is extremely fast and does not cause any performance hit, so try to add these checks as much as possible.

It's just weird to me that every other script that's is similar to this works fine with no errors, only this script is having the issues.
Posts: 287
Nick Thissen wrote:
For future reference: leaving these errors in your script is bad. We make sure an error in a script does not crash ATVO so we catch these errors when they happen. However, this is very costly in terms of performance especially if it keeps happening 30 times per second.

Adding this "value == null" check is extremely fast and does not cause any performance hit, so try to add these checks as much as possible.

I do, and now it is ok, but I have to had in different scripts cause when message disapears for one it comes for others after.
Posts: 61
@Nick

I added the "value == null" on certain points in the script. When I start the widget/theme now, the error seems to be fixed UNTILL my tickers that use this script switch to the next page, and the Object error reoccurs. Any thoughts?
Posts: 287
Perhaps I don't understand what you say but the condition null is to do in first just below reading value:

IEntitySessionResult result = (IEntitySessionResult) value;

if (value == null)
{
// do something appropriate, in this case probably just return nothing
return null;
}
Posts: 785
No, do it before the conversion to IEntitySessionResult.
Posts: 61
@Nick @Emmanuel

Thank for both of you input, I'm a noob at coding, and just realised what I did wrong!
Edited (1 time)
Posts: 287
me too
Thanks Nick
Posts: 785
So the issue was doing the check in the wrong place? Good to specify that in case anyone else reads this :)
I also moved the topic to the right forum.
Posts: 61
@Nick, Error is gone, but ATVO gets really slow quickly now :/

Edit: Well, the control interface gets very slow. The overlay however keeps working fine (as far as I can tell)
Edited (2 times)
Posts: 61
Nick Thissen wrote:
So the issue was doing the check in the wrong place? Good to specify that in case anyone else reads this :)
I also moved the topic to the right forum.

Yes, I placed the "value == null" check to the very top of the script before "IEntitySessionResult result = (IEntitySessionResult) value;"
Posts: 785
Dwayne Prins wrote:
@Nick, Error is gone, but ATVO gets really slow quickly now :/

Edit: Well, the interface gets very slow. The overlay however keeps working fine (as far as I can tell)
Can you check if a script is slowing it down or something else?
Posts: 61
It's the script that threw the error previously. That I thought was fixed now.
Edited (2 times)
Posts: 61
Nick Thissen wrote:
Dwayne Prins wrote:
@Nick, Error is gone, but ATVO gets really slow quickly now :/

Edit: Well, the interface gets very slow. The overlay however keeps working fine (as far as I can tell)
Can you check if a script is slowing it down or something else?

Okay, I was confused why it was grinding to a halt basicly, after some tweaking I came to the conclusing it was the freshly added check "value == null" function returning null. However, I changed the "return null" to -return ""- and that apperantly did the trick. Not sure what is diffrent otherwise.
But the control interface is staying usable now :)

Thanks for all the other tips and tricks to make my scripts smoother (and more preventing errors).
Posts: 785
That's interesting... I'll have to check it out why that's happening, it should not matter. Thanks for finding that!
Posts: 61
Nick Thissen wrote:
That's interesting... I'll have to check it out why that's happening, it should not matter. Thanks for finding that!

I can't tell for 100% that the freshly added function is causing it, but I did notice a stability within the control interface (and in the script performance it didn't spike as quickly and as high as it did) after I changed to what I've wrote in my previous post