Silentexecute Posted June 29 Share Posted June 29 (edited) Hi, I'm not sure what causing the NullReferenceException in my script when booting up GTA 5. I assume its something to do with the foreach loop but can't figure what exactly is the problem. I wrote a script that scans all objects in the world that match a model with a bus stop and then attaching a blip if the object does not currently have one. Bus stands and bus signs have two different blips hence the two foreach loops. The first part simply spawns in some extra bus stops with a bool to stop multiple instance spawnings, this works as I tested this before adding the foreach segment. I'm using a ScriptHookVDotNet3 as my reference file. Thanks using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using GTA; using GTA.Math; using GTA.Native; namespace BusStops { public class Class1 : Script { public Class1() { Tick += OnTick; } bool objectsspawned = false; public void OnTick(object sender, EventArgs e) { if (objectsspawned == false) { // Dashound // Dashound Signs Prop DHbussign1 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(399.5974f, -747.6259f, 28.28553f), new Vector3(0f, 0f, 90f), false, false); Prop DHbussign2 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(247.0426f, -829.9383f, 28.84298f), new Vector3(0f, 0f, -21f), false, false); Prop DHbussign3 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(106.2687f, -957.2974f, 28.49615f), new Vector3(0f, 0f, 70f), false, false); Prop DHbussign4 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(197.394f, -1081.93f, 28.29406f), new Vector3(0f, 0f, 85f), false, false); Prop DHbussign5 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(257.4973f, -1177.393f, 28.51799f), new Vector3(0f, 0f, 0f), false, false); Prop DHbussign6 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(87.79559f, -1450.335f, 28.19555f), new Vector3(0f, 0f, 46f), false, false); Prop DHbussign7 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(-60.51649f, -1460.293f, 30.97322f), new Vector3(0f, 0f, 11f), false, false); Prop DHbussign8 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(-190.2245f, -1615.454f, 32.85874f), new Vector3(0f, 0f, 86f), false, false); Prop DHbussign9 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(-90.007f, -1785.146f, 27.75162f), new Vector3(0f, 0f, 137f), false, false); Prop DHbussign10 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(137.4198f, -1822.437f, 26.21942f), new Vector3(0f, 0f, -133f), false, false); Prop DHbussign11 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(227.3857f, -1738.329f, 27.9874f), new Vector3(0f, 0f, -141f), false, false); Prop DHbussign12 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(396.6592f, -1778.343f, 28.22147f), new Vector3(0f, 0f, 140f), false, false); Prop DHbussign13 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(505.3119f, -1743.756f, 27.86801f), new Vector3(0f, 0f, -110f), false, false); Prop DHbussign14 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(516.6769f, -1600.696f, 28.27563f), new Vector3(0f, 0f, -129f), false, false); Prop DHbussign15 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(405.9883f, -1366.651f, 29.41969f), new Vector3(0f, 0f, -40f), false, false); Prop DHbussign16 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(234.3909f, -1196.672f, 28.34907f), new Vector3(0f, 0f, -86f), false, false); Prop DHbussign17 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(261.1187f, -973.7057f, 28.33515f), new Vector3(0f, 0f, -110f), false, false); Prop DHbussign18 = World.CreateProp(new Model("prop_bus_stop_sign"), new Vector3(354.6406f, -708.9678f, 28.27382f), new Vector3(0f, 0f, -110f), false, false); // Dashound Stands Prop DHbusstand1 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(395.6796f, -744.0556f, 28.27941f), new Vector3(0f, 0f, 89f), false, false); Prop DHbusstand2 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(252.4204f, -828.0403f, 28.73823f), new Vector3(0f, 0f, -20f), false, false); Prop DHbusstand3 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(104.9835f, -953.0038f, 28.54762f), new Vector3(0f, 0f, 70f), false, false); Prop DHbusstand4 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(193.6411f, -1078.229f, 28.29406f), new Vector3(0f, 0f, 86f), false, false); Prop DHbusstand5 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(260.795f, -1173.924f, 28.39584f), new Vector3(0f, 0f, 0f), false, false); Prop DHbusstand6 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(88.12967f, -1445.7f, 28.24245f), new Vector3(0f, 0f, 51f), false, false); Prop DHbusstand7 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(-58.01865f, -1455.61f, 31.1154f), new Vector3(0f, 0f, 9f), false, false); Prop DHbusstand8 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(-194.1142f, -1612.23f, 32.92382f), new Vector3(0f, 0f, 86f), false, false); Prop DHbusstand9 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(-94.61531f, -1784.533f, 27.93844f), new Vector3(0f, 0f, 141f), false, false); Prop DHbusstand10 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(137.79f, -1825.92f, 26.05f), new Vector3(0f, 0f, -131f), false, false); Prop DHbusstand11 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(225.6988f, -1742.523f, 28.11305f), new Vector3(0f, 0f, -147f), false, false); Prop DHbusstand12 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(391.7884f, -1778.194f, 28.24051f), new Vector3(0f, 0f, 139f), false, false); Prop DHbusstand13 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(507.122f, -1748.378f, 27.74495f), new Vector3(0f, 0f, -112f), false, false); Prop DHbusstand14 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(516.7751f, -1605.935f, 28.31999f), new Vector3(0f, 0f, -131f), false, false); Prop DHbusstand15 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(412.1253f, -1366.436f, 29.10775f), new Vector3(0f, 0f, -42f), false, false); Prop DHbusstand16 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(238.255f, -1200.648f, 28.31784f), new Vector3(0f, 0f, -84f), false, false); Prop DHbusstand17 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(262.1421f, -978.6238f, 28.35717f), new Vector3(0f, 0f, -111f), false, false); Prop DHbusstand18 = World.CreateProp(new Model("prop_busstop_02"), new Vector3(357.2667f, -715.2408f, 28.26788f), new Vector3(0f, 0f, -110f), false, false); objectsspawned = true; } // Attach blips to bus stops/stands var allobjs = World.GetAllEntities(); foreach (var obj in allobjs) { if ((obj.Model == "prop_busstop_02" && obj.Exists()) || (obj.Model == "prop_busstop_04" && obj.Exists())|| (obj.Model == "prop_busstop_05" && obj.Exists())) { if (!obj.AttachedBlip.Exists()) { obj.IsPersistent = true; Blip _blip = obj.AddBlip(); _blip.Color = BlipColor.Green; _blip.Scale = 0.5f; _blip.IsShortRange = true; _blip.Name = "Bus Stand"; _blip.ShowRoute = false; } } else if (obj.Model == "prop_bus_stop_sign" && obj.Exists()) { if (!obj.AttachedBlip.Exists()) { obj.IsPersistent = true; Blip _blip2 = obj.AddBlip(); _blip2.Color = BlipColor.GreenDark; _blip2.Scale = 0.5f; _blip2.IsShortRange = true; _blip2.Name = "Bus Sign"; _blip2.ShowRoute = false; } } } } } } This is what shows in the ScriptHookVDotNet log [01:43:04] [DEBUG] Loading API from .\ScriptHookVDotNet2.dll ... [01:43:04] [DEBUG] Loading API from .\ScriptHookVDotNet3.dll ... [01:43:04] [DEBUG] Loading scripts from F:\Rockstar Games\Grand Theft Auto V\scripts ... [01:43:06] [DEBUG] Successfully compiled ForestShelter.cs. [01:43:06] [INFO] Found 1 script(s) in ForestShelter.cs resolved to API 2.11.3. [01:43:06] [DEBUG] Loading assembly AddedTraffic.dll ... [01:43:06] [INFO] Found 1 script(s) in AddedTraffic.dll resolved to API 2.11.3. [01:43:06] [DEBUG] Loading assembly BusSimulatorV.dll ... [01:43:06] [INFO] Found 2 script(s) in BusSimulatorV.dll resolved to API 2.11.3. [01:43:06] [DEBUG] Loading assembly BusStops.dll ... [01:43:06] [INFO] Found 1 script(s) in BusStops.dll resolved to API 3.4.0. [01:43:06] [DEBUG] Loading assembly Coach2.dll ... [01:43:07] [INFO] Found 1 script(s) in Coach2.dll resolved to API 2.11.3. [01:43:07] [DEBUG] Loading assembly DamageEffects.dll ... [01:43:07] [INFO] Found 1 script(s) in DamageEffects.dll resolved to API 2.11.3. [01:43:07] [DEBUG] Loading assembly Disarm.dll ... [01:43:07] [WARNING] Resolving API version 0.0.0 referenced in Disarm.dll. [01:43:07] [INFO] Found 1 script(s) in Disarm.dll resolved to API 2.11.3. [01:43:07] [DEBUG] Loading assembly EnableAllinteriors.dll ... [01:43:08] [INFO] Found 2 script(s) in EnableAllinteriors.dll resolved to API 2.11.3. [01:43:08] [DEBUG] Loading assembly FailCam.dll ... [01:43:08] [INFO] Found 1 script(s) in FailCam.dll resolved to API 3.4.0. [01:43:08] [DEBUG] Loading assembly iFruitAddon2.dll ... [01:43:09] [INFO] Found 1 script(s) in iFruitAddon2.dll resolved to API 2.11.3. [01:43:09] [DEBUG] Loading assembly INMNativeUI.dll ... [01:43:09] [INFO] Found 1 script(s) in INMNativeUI.dll resolved to API 2.11.3. [01:43:09] [DEBUG] Loading assembly KillCam.dll ... [01:43:09] [INFO] Found 1 script(s) in KillCam.dll resolved to API 2.11.3. [01:43:09] [DEBUG] Loading assembly LemonUI.SHVDN3.dll ... [01:43:09] [INFO] Found 0 script(s) in LemonUI.SHVDN3.dll. [01:43:09] [DEBUG] Loading assembly LosSantosBusService2.dll ... [01:43:09] [INFO] Found 1 script(s) in LosSantosBusService2.dll resolved to API 2.11.3. [01:43:09] [DEBUG] Loading assembly LSTransit.dll ... [01:43:09] [INFO] Found 1 script(s) in LSTransit.dll resolved to API 2.11.3. [01:43:09] [DEBUG] Loading assembly MadMilkman.Ini.dll ... [01:43:09] [INFO] Found 0 script(s) in MadMilkman.Ini.dll. [01:43:09] [DEBUG] Loading assembly Metadata.dll ... [01:43:09] [INFO] Found 0 script(s) in Metadata.dll. [01:43:09] [DEBUG] Loading assembly NativeUI.dll ... [01:43:09] [INFO] Found 1 script(s) in NativeUI.dll resolved to API 2.11.3. [01:43:09] [DEBUG] Loading assembly PedStatus.dll ... [01:43:10] [INFO] Found 1 script(s) in PedStatus.dll resolved to API 3.4.0. [01:43:10] [DEBUG] Loading assembly Persistent Weapon Flashlight.dll ... [01:43:10] [INFO] Found 1 script(s) in Persistent Weapon Flashlight.dll resolved to API 3.4.0. [01:43:10] [DEBUG] Loading assembly RouteCreator.dll ... [01:43:10] [INFO] Found 1 script(s) in RouteCreator.dll resolved to API 2.11.3. [01:43:10] [DEBUG] Loading assembly TeamCore.dll ... [01:43:10] [INFO] Found 0 script(s) in TeamCore.dll. [01:43:10] [DEBUG] Loading assembly TheCharacterVehicleMod.dll ... [01:43:10] [INFO] Found 1 script(s) in TheCharacterVehicleMod.dll resolved to API 2.11.3. [01:43:10] [DEBUG] Instantiating script AddedTraffic.Traffic ... [01:43:13] [INFO] Started script AddedTraffic.Traffic. [01:43:13] [DEBUG] Instantiating script BusSimulatorV.BusSim ... [01:43:13] [INFO] Started script BusSimulatorV.BusSim. [01:43:13] [DEBUG] Instantiating script BusSimulatorV.BusSimTimer ... [01:43:13] [INFO] Started script BusSimulatorV.BusSimTimer. [01:43:13] [DEBUG] Instantiating script BusStops.Class1 ... [01:43:13] [INFO] Started script BusStops.Class1. [01:43:13] [DEBUG] Instantiating script Coach2.Coach2 ... [01:43:13] [INFO] Started script Coach2.Coach2. [01:43:13] [DEBUG] Instantiating script DamageEffects.Main ... [01:43:13] [INFO] Started script DamageEffects.Main. [01:43:13] [DEBUG] Instantiating script Disarm.Main ... [01:43:13] [INFO] Started script Disarm.Main. [01:43:13] [DEBUG] Instantiating script EnableAllinteriors.Class1 ... [01:43:13] [ERROR] Failed to instantiate script EnableAllinteriors.Class1 because constructor threw an exception: System.IO.FileNotFoundException: Could not load file or assembly 'LemonUI.SHVDN2, Version=1.5.2.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. File name: 'LemonUI.SHVDN2, Version=1.5.2.0, Culture=neutral, PublicKeyToken=null' at EnableAllinteriors.Class1..ctor() WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. [01:43:13] [DEBUG] Instantiating script EnableAllinteriors.EAIBLip ... [01:43:13] [INFO] Started script EnableAllinteriors.EAIBLip. [01:43:13] [DEBUG] Instantiating script ForestShelter.Main ... [01:43:13] [INFO] Started script ForestShelter.Main. [01:43:13] [DEBUG] Instantiating script FailCam.Main ... [01:43:14] [INFO] Started script FailCam.Main. [01:43:14] [DEBUG] Instantiating script iFruitAddon2.iFruitAddon2 ... [01:43:14] [INFO] Started script iFruitAddon2.iFruitAddon2. [01:43:14] [DEBUG] Instantiating script INMNativeUI.BigMessageThread ... [01:43:14] [INFO] Started script INMNativeUI.BigMessageThread. [01:43:14] [DEBUG] Instantiating script KillCam.Main ... [01:43:14] [INFO] Started script KillCam.Main. [01:43:14] [DEBUG] Instantiating script LosSantosBusService2.LosSantosBusService2 ... [01:43:14] [INFO] Started script LosSantosBusService2.LosSantosBusService2. [01:43:14] [DEBUG] Instantiating script LSTransit.Main ... [01:43:14] [INFO] Started script LSTransit.Main. [01:43:14] [DEBUG] Instantiating script NativeUI.BigMessageThread ... [01:43:14] [INFO] Started script NativeUI.BigMessageThread. [01:43:14] [DEBUG] Instantiating script CompanionHealth.Main ... [01:43:14] [INFO] Started script CompanionHealth.Main. [01:43:14] [DEBUG] Instantiating script PersistentWeaponFlashlight.PersistantWeaponFlashlight ... [01:43:24] [INFO] Started script PersistentWeaponFlashlight.PersistantWeaponFlashlight. [01:43:24] [DEBUG] Instantiating script RouteCreator.Creator ... [01:43:24] [INFO] Started script RouteCreator.Creator. [01:43:24] [DEBUG] Instantiating script TheCharacterVehicleMod ... [01:43:24] [INFO] Started script TheCharacterVehicleMod. [01:43:24] [ERROR] Caught fatal unhandled exception: System.NullReferenceException: Object reference not set to an instance of an object. at BusStops.Class1.OnTick(Object sender, EventArgs e) at SHVDN.Script.MainLoop() [01:43:24] [ERROR] The exception was thrown while executing the script BusStops.Class1. [01:43:24] [WARNING] Aborted script BusStops.Class1. Edited June 29 by Silentexecute Link to comment Share on other sites More sharing options...
LeeC22 Posted June 29 Share Posted June 29 (edited) Edit: Just noticed your post on 5Mods... the code I posted was incorrect with regards to AttachedBlip, so I have removed my comment. That seems to be a new SHVDN v3 thing which I have no experience with, I still use SHVDN v2. Edited June 29 by LeeC22 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now