Jump to content

Object reference not set to an instance of an object


Recommended Posts

Posted

Hi guys,

after some hours of programming, I've got an error. This is my exception-log:

==================[28.12.2018 04:10:52]==================
System.NullReferenceException: Object reference not set to an instance of an object.
   at void GTV.Systems.Cargo.Core.AttachCargosToLoadings() in C:\Users\Kacper\Documents\GT-V\GTV\GTV\Systems\Cargo\core.cs:line 118
=========================================================

==================[28.12.2018 04:10:52]==================
System.Reflection.TargetParameterCountException: Parameter count mismatch.
   at object[] System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
=========================================================

Here's my console:

Step 1
Step 2
Step 3
Step 4, r: 1, c: 0, llc: 3
Step 5
[Cargo] Cargo destination has been set to 3
Step 6, 0
GTV.Systems.Cargo.Cargo

And here are the functions doing this staff:

...
public void AttachCargosToLoadings()
        {
            try
            {
                if (cargos_list.Count <= 0) throw new Exception("Brak towarów");
                if (loadings_list.Count <= 1) throw new Exception("Brak załadunków");
                int currentLoadingID = 0;
                NAPI.Util.ConsoleOutput("Step 1");
                foreach (Loading load in loadings_list)
                {
                    NAPI.Util.ConsoleOutput("Step 2");
                    for (var i=0;i<5;i++)
                    {
                        NAPI.Util.ConsoleOutput("Step 3");
                        int randomIndex = random.Next(cargos_list.Count - 1);
                        AttachDestinationToCargo(cargos_list[randomIndex],currentLoadingID);
                        NAPI.Util.ConsoleOutput("Step 6, "+randomIndex);
                        NAPI.Util.ConsoleOutput(cargos_list[randomIndex].ToString());
                        NAPI.Util.ConsoleOutput(cargos_list[randomIndex].GetType().ToString());
                        load.cargos.Add(cargos_list[randomIndex]);
                        NAPI.Util.ConsoleOutput("Step 7");
                        NAPI.Util.ConsoleOutput("[Cargo] Inserted Cargo ("+cargos_list[randomIndex].name+") to Loading ("+load.id+")", ConsoleColor.DarkGreen);
                        NAPI.Util.ConsoleOutput("Step 8");
                    }
                    currentLoadingID++;
                }
            }
            catch (Exception e)
            {
                NAPI.Util.ConsoleOutput("[Cargo] " + e.Message + " (Init Attach)", ConsoleColor.Red);
            }
        }

        public void AttachDestinationToCargo(Cargo cargo, int currentLoadingID)
        {
            int randomIndex = random.Next(loadings_list.Count - 1);
            while(randomIndex==currentLoadingID) {
                randomIndex = random.Next(loadings_list.Count - 1);
            }
            NAPI.Util.ConsoleOutput("Step 4, r: "+randomIndex+", c: "+currentLoadingID+", llc: "+loadings_list.Count);
            cargo.destination = loadings_list[randomIndex];
            NAPI.Util.ConsoleOutput("Step 5");
            NAPI.Util.ConsoleOutput("[Cargo] Cargo destination has been set to "+loadings_list[randomIndex].id, ConsoleColor.DarkCyan);
        }
...

Cargo class:

		public int id { get; set; }
        public string name { get; set; }
        public bool legal { get; set; }
        public string color = "#ffffff";
        public double money_multipler { get; set; }
        public double exp_multipler { get; set; }
        public int event_id = 0;
        public int courses = 0;
        public Loading destination { get; set; }

Loading class:

		public int id { get; set; }
        public Vector3 pos { get; set; }

        // Init cargos
        public List<Cargo> cargos { get; set; }

        //After InGame Create
        public Marker marker { get; set; }
        public ColShape colshape { get; set; }
        public Blip blip { get; set; }
        public TextLabel text { get; set; }

        bool error = false;

What Am I doing wrong? I really appreciate your help :)

Posted

Ok. I made it. The thread can be closed.

Solution:

in Loading.cs
before -> public List<Cargo> cargos {get; set;}
after -> public List<Cargo> cargos = new List<Cargo>();

 

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...