Getting "Object reference not set to an instance of an object" Error

var ballArray : Array;
ballArray = GameObject.FindGameObjectsWithTag(“PunishmentBall”);
ballArray.Add(GameObject.FindGameObjectsWithTag(“StandardBall”));
ballArray.Add(GameObject.FindGameObjectsWithTag(“PowerUpBall”));

		var balls : GameObject[] = new GameObject[ballArray.length];

		for(var i = 0; i < ballArray.length; i++)
		{
			(ballArray *as GameObject).GetComponent(BallController).startSlow = Time.time;*

_ (ballArray as GameObject).GetComponent(BallController).isSlowed= true;_
* }*
I have a trigger object that reads what it collides with, then performs an action. The code is supposed to get all the balls on the screen, then slow them down, but I get
NullReferenceException: Object reference not set to an instance of an object
Whenever it collides with this ball. The error occurs at line 8 here,
(ballArray as GameObject).GetComponent(BallController).startSlow = Time.time;

Do you have objects with “StandardBall” and “PowerUpBall” tags? Cause if you don’t, it’ll return null and add a null reference to your array.
Be aware that TAG is not NAME. To find object by name you should use just “Find”.

Also, are you sure that your objects have the BallController component?

Anyway, I recommend you to add a Debug to your code to check if all the entries in your array are valid:

var ballArray : Array;
ballArray = GameObject.FindGameObjectsWithTag("PunishmentBall");
ballArray.Add(GameObject.FindGameObjectsWithTag("StandardBall"));
ballArray.Add(GameObject.FindGameObjectsWithTag("PowerUpBall"));

for(var i = 0; i < ballArray.length; i++)
{
    if (ballArray *!= null) {*

var ballController = (ballArray as GameObject).GetComponent(BallController);
if (ballController != null) {
ballController.startSlow = Time.time;
ballController.GetComponent(BallController).isSlowed= true;
} else {
Debug.Log(i + " has no ball controller");
}
} else {
Debug.Log(i + " is null!");
}
}
PS: Your “balls” variable is doing nothing.