Log in to reply
 

What is causing this NullReferenceException?



  • 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

    CODE:

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

    }



  • @silentexecute said in What is causing this NullReferenceException?:

    if (!obj.AttachedBlip.Exists())

    This should be

    if (obj.AttachedBlip == null)
    

    SHVDN returns null if no blip is found. If you try to call .Exists() on null, you get the error you're experiencing.

    And while not the source of your problem, it is good practice to check if the entity exists before checking what their model is. So change

    else if (obj.Model == "prop_bus_stop_sign" && obj.Exists())
    

    to

    else if (obj.Exists() && obj.Model == "prop_bus_stop_sign")
    


  • @silentexecute
    I recommend you use the Aborted event after creating something. and start using lists or arrays will help you avoid headaches.

    and if you don't mind me asking, what exactly are you trying to do?



  • @Niziul Abortions are being banned in the USA, so respect local state laws before using aborted in your scripts.



  • @JohnFromGWN said in What is causing this NullReferenceException?:

    @Niziul Abortions are being banned in the USA, so respect local state laws before using aborted in your scripts.

    That went from zero to one hundred very fast. 😳


Log in to reply
 

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.