How to Fix "NullReferenceException"? Aim Down Sights Script

Hi community,
I am kind of new to the whole Unity/progamming thing and am working on a school assignment.

With pretty much every script I’ve done, I have come across the error: “NullReferenceException”, and, in most cases, have been quite successful in fixing the issue.

But, in the case of this AimDownSights Script, I have been less successful. I received the following error:

NullReferenceException: Object reference not set to an instance of an object
AimDownSights.Update () (at Assets/AimDownSights.js:44)

My script is as follows:

  var gun : Transform; 
  var curField = 60.0;
  private var dampVelocity2 = 0.4;
  var nextVector : Vector3; //Sights Transform
  var curVector : Vector3; //Normal Transform
  
  var curY = 234.8665; //Normal Rotation of Gun
  var nextY = 230.7; //Rotation of Gun when looking down Sights
 
  class weaponStats {
      var curDamage : int; //Normal Weapon Damage
      var nextDamage : int; //Sights Weapon Damage
      var gunScript : String;
      var mainCam : Crosshair_Script; 
  }

  var speed : float = 0.1;
  var stats : weaponStats;
  
  function Update () {
      //Smooth Changine Vield of View of Camera
     var newField = Mathf.SmoothDamp(Camera.main.fieldOfView, curField, dampVelocity2, .3);
     Camera.main.fieldOfView = newField;
  
     if (Input.GetButton("Fire2")) { 
         //adjust viewpoint and gun position
         curField = 40;
         //Rotating Gun to look down Camera Perfectly
         transform.localRotation.eulerAngles = Vector3(0,nextY,0);
  
         //slow down turning and movement speed
         transform.root.GetComponent(CharacterMotor).movement.maxForwardSpeed = 2.5;
         //Adjust Gun Damage
         GetComponent(stats.gunScript).damage = stats.nextDamage;
         stats.mainCam.enabled = false;
         //Moving the gun
         transform.localPosition = Vector3.Lerp(transform.localPosition,nextVector, 0.3);
     } else {
         //adjust viewpoint and gun position
         curField = 60.0;
         transform.localRotation.eulerAngles = Vector3(0,curY,0);
         //speed up turning and movement speed
         transform.root.GetComponent(CharacterMotor).movement.maxForwardSpeed = 10;
         stats.mainCam.enabled = true;
         GetComponent(stats.gunScript).damage = stats.curDamage;
         transform.localPosition = Vector3.Lerp(transform.localPosition,curVector, speed);
     }
  }

So, I get that this means that the error is on line 44 and it probably has something to do with the “mainCam” but I am not sure how I would go about fixing this. Any help for this particular problem would be appreciated, and some information on how to solve this sort of problem in the future would probably make my life a lot easier.

Please correct the formatting on your posted code so we see the same “line 44” you see. Cool username, btw.

A null ref exception is just that - a variable you’re trying to access points to something that isn’t there. You can use Debug.Log() statements to print info to the console. Try printing Debug.Log("is myObject null? " + myObject==null); for instance, to determine which particular object is null.