NullReferenceException in an array

NullReferenceException has brought me to my knees once more in the following script:

using UnityEngine;
using System.Collections;

public class ColorPicker : MonoBehaviour {

    private Texture2D[] currentColors;
    private GameObject[] currentCubes;
    Texture2D currentColorTexture;

    void Start() {
        currentColorTexture= new Texture2D(1,1);
    }

    void OnGUI() {
        for (int aY = 0;aY<currentColors.Length-1;aY++) {

            currentColorTexture = currentColors[aY];
            GUIStyle currentColorStyle = new GUIStyle();
            currentColorStyle.normal.background = currentColorTexture;

            if(GUI.Button(new Rect(10,(aY*10) + 10, 10,50), currentColors[aY], ""))
                Debug.Log("ah");

        }

    }

    public void UpdateColors() {
        currentCubes = GameObject.FindGameObjectsWithTag("Cube");       
        currentColors = new Texture2D[currentCubes.Length];

        for(int aA = 0;aA<currentColors.Length;aA++) {
            currentColors[aA] = new Texture2D(1,1);
            currentColors[aA].SetPixel((int)0,(int)0, currentCubes[aA].renderer.material.color);
        }

        for (int aB = 0;aB<currentCubes.Length;aB++) {          
            for (int aC = 0;aC<currentColors.Length;aC++) {
                currentColorTexture.SetPixel(0,0, currentCubes[aB].renderer.material.color);
                currentColors[aB] = currentColorTexture;
            }
        }

    }
}

I've literally spent six and half hours trying to find the error in this simple piece of work but it keeps giving me the high-blood-pressure-inducing NullReferenceException.

I would deeply appreciate it if somebody could explain what I'm doing wrong here.

Thanks, Elliot Bonneville

At a first glance.

currentCubes = GameObject.FindGameObjectsWithTag("Cube");       
currentColors = new Texture2D[currentCubes.Length];

Could potentially cause a NullReferenceException if nothing with a Cube tag exists.

currentCubes = GameObject.FindGameObjectsWithTag("Cube");
if (currentCubes != null)
{
    currentColors = new Texture2D[currentCubes.Length];
    ...
}

Should fix that.

Not sure if that's your specific issue though.

-edit-

also

void OnGUI() {
    for (int aY = 0;aY<currentColors.Length-1;aY++) {

If it runs before

public void UpdateColors()

Which I'm not sure when you call, will cause a NullReferenceException.

Should initialize it to 0.

private Texture2D[] currentColors = new Texture2D[0];

Or check if it's null before the for loop. Or call updatecolours from start() etc.