Help with Null Reference Exception please.

I’m getting a Null Reference exception on a line checking if a variable is null before copying it?

ANSWERED

using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// A simple script that holds an array of audio clips so they can be triggered to play from an animation.
/// Use PlayClip( clipnumber ) to trigger the clip you would like played.
/// </summary>
class Anim_PlayAudio : MonoBehaviour
{
	/// <summary>
	/// Audio clip and priority data.
	/// </summary>
	[System.Serializable]
	public class Audiodata 
	{
		/// <summary>
		/// A string containing a name for this clip for easy reference in the Inspector;
		/// </summary>
		public string name = "";
		/// <summary>
		/// <see cref="UnityEngine.AudioClip"/>
		/// </summary>
		public AudioClip	audioClip = null;
		/// <summary>
		/// A <see cref="System.Int32"/> specifiying the priority of this AudioClip.
		/// </summary>
		public int			priority = 16;
		public Audiodata() 
		{
			priority = 16;
		}
	}

	/// <summary>
	/// An array of <see cref="Anim_PlayAudio.AudioData"/> clips for playback.
	/// </summary>
	public Audiodata[]		audioClips = null;
	private AudioSource[]	_sources = null;

	void Awake() 
	{
		if (audioClips == null)
		{
			ZCTDebug.EditorMessageBox( "Anim_PlayAudio on " + this.gameObject.name + ": no Audioclips in list to play. Please add one in the inspector.");
		}
		else 
		{
			_sources = new AudioSource[audioClips.Length];
			for (int i = 0; i < _sources.Length; i++ )
			{
				_sources *= new AudioSource();*

if (audioClips != null && audioClips.audioClip != null) sources.clip = audioClips*.audioClip;
else*

* ZCTDebug.EditorMessageBox("Anim_PlayAudio on " + this.gameObject.name + “: Audio clip at index " + i + " is null!”);*
* }
}
}*_

* ///

*
* /// Plays an audio clip from the audioClips array.*
* ///
*
* /// The index of the clip to be played.*
* void PlayClip(int clipNumber)*
* {*
* if (audioClips == null)*
* {*
* ZCTDebug.EditorMessageBox("Anim_PlayAudio on " + this.gameObject.name + “: no Audioclips in list to play. Please add one in the inspector.”);
_ }
if (clipNumber >= audioClips.Length)
{_

ZCTDebug.EditorMessageBox("Anim_PlayAudio on " + this.gameObject.name + “: Clip selected is out of range. Check your PlayClip( num ) function.”);
_ }
if (clipNumber < 0)
{_

ZCTDebug.EditorMessageBox("Anim_PlayAudio on " + this.gameObject.name + “: Clip selected is out of range.
PlayClip( num ) function. Value cannot be less than zero.”);

_ }_
if (sources[clipNumber].clip.isReadyToPlay)*
* {_
sources[0].Play();*
* }
}*_

* void PlayClipNamed(string clipDataName)*
* {*
* if (audioClips == null)*
* {*
* ZCTDebug.EditorMessageBox("Anim_PlayAudio on " + this.gameObject.name + “: no Audioclips in list to play.
Please add one in the inspector.”);

_ }_
bool foundclip = false;
Listindexes = new List();*
* for(int i = 0; i < audioClips.Length; i++)*

* {*
if ( audioClips*.audioClip == null) ZCTDebug.EditorMessageBox("Anim_PlayAudio on " + this.gameObject.name + “: Clip selected does not exist. PlayClip( name ) function. The AudioClip specified is null.”);
if ( audioClips.name == clipDataName) foundclip = true;
indexes.Add(i);*
* }*

* if (!foundclip)*
* {_
ZCTDebug.EditorMessageBox("Anim_PlayAudio on " + this.gameObject.name + “: Clip selected does not exist. PlayClip( name ) function. The audio clip specified wasn’t found.”);
_ }
else*

* {_
foreach (int index in indexes)*
* {
PlayClip(index);
}
}
}
}
The line generating the error was:_

if (audioClips.audioClip != null) sources.clip = audioClips.audioClip;*
I’m including the output script referencing this one as well since it’s useful to anyone reading this question in the future and in general.
using UnityEngine;
using UnityEditor;
using System.Collections;_

///


/// Debug replacement for comments only meant to be seen in the Unity Editor console.
///

public static class ZCTDebug {
* /// *
* /// Output a message to the console after checking to be sure the project is running in the Unity Editor.*
* ///
*

* /// A string containing the message to be echoed to the console. Recommended to begin with the name of the outputting Class.*
* public static void Log (string Output) {*

* throw new System.Exception(Output);*
* }*

* ///

*
* /// Output a modal popup message box to the editor user.*
* ///
*
* /// A string containing the message to be displayed to the user. Recommend including GameObject name and outputting Class name to aid in debugging.*
* public static void EditorMessageBox(string Output)*
* {*
#if UNITY_EDITOR
* EditorUtility.DisplayDialog(“Error!”, Output, “Derp!”, “”);*
#endif
* }*
}

Ok so there are a couple of things here:

  • You do null checks but don’t put the code that requires the item to be non null in an else block, or return when the value was null.
  • You don’t check whether audioClips is null. Just because the array has a slot, it doesn’t mean that slot is filled with an AudioClip - it will be null until initialised.