NullRefernceException runtime error but no build errors [ScriptHookVDotNet]
-
I'm writing a script using ScriptHookVDotNet and when I build it, it doesn't give me any errors at all. So I move it to the scripts folder and open the game, I press J and it does nothing even though I added a keyDown event and when I check the log file It says there was a NullRefernceException at the keyDown event. I posted an issue on GitHub more than an hour ago but nobody answered so I came here to get some help.
Here is the code.
-
I think you're confusing a few concepts here.
There are compile-time errors, which mean that your program is syntactically incorrect and does not compile. It doesn't run - there's nothing to run.
There are run-time errors, which mean the program was compiled successfully, but it ran an invalid operation. This is your problem. The program runs up to the point of the error.
I don't know why you asked on GitHub, as GitHub issues are usually meant for reporting bugs with the program the repository provides, not with user error where you just happen to use a library.
A
NullReferenceException
means your program/script accessed an invalid address, most likely you're trying to use a reference to something which hasn't been initialized yet.The log file ScriptHookV.NET generates should usually pinpoint the exact line where the error occurs, it might help people help you debug the problem. In addition to the log file, you might also want to explain what your script is supposed to do.
As for what exactly goes wrong in your script - I don't know. The checks for not-
null
-ness seem to be present, so the only thing I think can cause it would beTarget.AddBlip();
failing andTarget.CurrentBlip
is stillnull
when you callTarget.CurrentBlip.Sprite = BlipSprite.BigCircle;
next.
-
@ikt Thanks for the feedback, the log just says that the error was at the keydown event and not on a specific line. I'll try to see if that's the case (AddBlip() Failing).
-
@yoni1857
A full log would be helpful.
-
@ikt [ERROR] Caught unhandled exception:
System.NullReferenceException: Object reference not set to an instance of an object.
at TrackersV.TrackersV.<>c.<.ctor>b__0_0(Object sender, KeyEventArgs e)
at GTA.Script.raise_KeyDown(Object value0, KeyEventArgs value1)
at GTA.Script.MainLoop()
-
@yoni1857
Well, crap.Moving your event handler out of there might help debugging.
-
@ikt what do you mean by moving the event handler? like making it a different function? already tried that before and got the same result. Adding a try-catch does nothing.
-
Changing it to
public TrackersV() { KeyDown += OnKeyDown; } void OnKeyDown(object sender, EventArgs e) { // Key handler }
doesn't make the exception clearer?
-
@ikt Nope, like I said I already tried that and got the same result.
-
@ikt ?
-
I have a feeling it is due to this snippet:
Target.AddBlip(); Target.CurrentBlip.Sprite = BlipSprite.BigCircle; Target.CurrentBlip.Color = blipColor; Target.CurrentBlip.Name = Target.Model.ToString();
I think I remember having problems with CurrentBlip in the past. Try doing this instead:
Blip newBlip = Target.AddBlip(); newBlip.Sprite = BlipSprite.BigCircle; newBlip.Color = blipColor; newBlip.Name = Target.Model.ToString();
-
LeeC2202 spotted this: (thanks, I completely missed this!)
That coding question you're answering on 5Mods, the exception is on Line 49. Target will always be null in that else if, because he already checks if (Target != null) above it on Line 22.
-
@ikt Wait so what does this mean? (What's the fix)
-
@Jitnaught Thank you! Now script works but the blip doesn't show so i'll investigate that.
-
@yoni1857
Move theelse if (Target.CurrentBlip != null) { Target.CurrentBlip.Remove(); }
inside the
if (Target != null)
branch.Like so:
https://pastebin.com/G8feDHdyI also took the liberty to move things into functions, it just makes me go "what" a lot less.
-
@ikt Thanks! Will try this out
-
@ikt Still same error:
system.NullReferenceException: Object reference not set to an instance of an object.
at TrackersV.TrackersV.AddPlayerBlip()
at TrackersV.TrackersV.OnKeyDown(Object source, KeyEventArgs e)
at GTA.Script.raise_KeyDown(Object value0, KeyEventArgs value1)
at GTA.Script.MainLoop()
-
So I combined @ikt and @Jitnaught 's code and i'm still getting the same error.
[ERROR] Caught unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object. at TrackersV.TrackersV.AddPlayerBlip() at TrackersV.TrackersV.OnKeyDown(Object source, KeyEventArgs e) at GTA.Script.raise_KeyDown(Object value0, KeyEventArgs value1) at GTA.Script.MainLoop()
-
Can we see the code?
-
@Jitnaught there you go https://pastebin.com/qEw79Ldc
-
Change
if (Target.Exists())
to
if (Target != null && Target.Exists())
-
@Jitnaught Okay
-
@Jitnaught Okay so I've got good news and bad news.
Good news: No Errors from the script.
Bad news: The blip doesn't show up.
-
Add some
UI.ShowSubtitle("text here");
's so you know what the script is doing. Like "Blip added" and "Target is null"
-
@Jitnaught I added UI.ShowSubtitle(newBlip.toString()); under Blip newBlip = Target.addBlip(); and it did nothing