System.NullReferenceException help

Started by Faryzal2020, October 31, 2018, 03:55:47 AM

Previous topic - Next topic

Faryzal2020

Hello, im new to the C# realm and i need help..
So i'm basicly want to get some data out of the projectile thingdef that loaded into a turret that uses this CompProperties_ChangeFiremode.
but it shows error System.NullReferenceException: Object reference not set to an instance of an object
i use the log message to find where the error happens and it happens after the last one "line4"

Now i just want to try getting the defname, but i actually need to get the speed and flyOverhead parameter. And is it possible to change those parameters with a click of a Gizmo button?
Check out my mod: Definitely More Cannons

Haplo

Might be that I'm blind, but where do you initiate the LoadedProjectile?
If you don't do that then it's state is 'null' --> NullRefException when trying to set it
Also, don't set it to a specific index as that index may not be there at the time of the run --> Use .Add(...) instead --> no defined index needed!

Faryzal2020

Quote from: Haplo on October 31, 2018, 07:19:46 AM
Might be that I'm blind, but where do you initiate the LoadedProjectile?
If you don't do that then it's state is 'null' --> NullRefException when trying to set it
Also, don't set it to a specific index as that index may not be there at the time of the run --> Use .Add(...) instead --> no defined index needed!
the initialization is right there above public ThingDef Turret;
i just removed the index and use .Add(...) for the LoadedProjectile assignment but its still throwing error "SaveableFromNode exception: System.NullReferenceException: Object reference not set to an instance of an object"
Check out my mod: Definitely More Cannons

Nargon

#3
Quote from: Faryzal2020 on October 31, 2018, 04:37:00 PM
the initialization is right there above public ThingDef Turret;
No it is not.
The line: "public List<String> LoadedProjectile;" is just a definition of a variable, but it is not an initialization of variable.
Inicialization is: "LoadedProjectile = new List<String>();". Without this inicialization, the variable "LoadedProjectile" has value "null" and nearly every using of that variable throws the "System.NullReferenceException", like in your case.

Faryzal2020

Quote from: Nargon on November 01, 2018, 04:02:09 AM
Quote from: Faryzal2020 on October 31, 2018, 04:37:00 PM
the initialization is right there above public ThingDef Turret;
No it is not.
The line: "public List<String> LoadedProjectile;" is just a definition of a variable, but it is not an initialization of variable.
Inicialization is: "LoadedProjectile = new List<String>();". Without this inicialization, the variable "LoadedProjectile" has value "null" and nearly every using of that variable throws the "System.NullReferenceException", like in your case.
Ah so thats what sir Haplo meant, sorry i'm a dum..

Thank you very much, it's solved now.  :D
Check out my mod: Definitely More Cannons