ATVO by Appgineering
Download
Read-only

Error in script, yet it does work

24 posts 14,108 views Started 12 May 2020, 10:22
Showing 1–24 of 24 posts
Dwayne P.
Original poster

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!

Emmanuel S.
Reply #1

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.

Dwayne P.
Reply #2
· edited

Emmanuel S. 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.

Emmanuel S.
Reply #4

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.

Dwayne P.
Reply #5

Emmanuel S. 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;
Dwayne P.
Reply #6

Emmanuel S. 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..

Nick Thissen Appgineering
Reply #7

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'.

Nick Thissen Appgineering
Reply #8

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.

Dwayne P.
Reply #9

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.

Emmanuel S.
Reply #10

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.

Dwayne P.
Reply #11

@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?

Emmanuel S.
Reply #12

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;
				}
Nick Thissen Appgineering
Reply #13

No, do it before the conversion to IEntitySessionResult.

Dwayne P.
Reply #14
· edited

@Nick @Emmanuel

Thank for both of you input, I'm a noob at coding, and just realised what I did wrong!

Nick Thissen Appgineering
Reply #16

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.

Dwayne P.
Reply #17
· edited

@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)

Dwayne P.
Reply #18

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;"

Nick Thissen Appgineering
Reply #19

Dwayne P. 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?

Dwayne P.
Reply #21

Nick Thissen wrote:
Dwayne P. 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).

Nick Thissen Appgineering
Reply #22

That's interesting... I'll have to check it out why that's happening, it should not matter. Thanks for finding that!

Dwayne P.
Reply #23

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

Archive · Read-only

New replies have moved to Discord.