"NullReferenceException: Object reference not set to an instance of an object"

I’ve been messing with this for a few hour and I just cant seem to get what I am doing wrong here. I’ve read and searched for awhile on Unity Answers, the Forums, and MSDN: but I just can’t understand what is going on. Any help and tips would be extremely appreciated. Basically what the script is doing is downloading a list of images via WWW, and using another script to cache them or load them from the file.

If I take away the part referring to the instance, it tells me I am pointing to non-static methods, when they have static in their name.

The Complete error is:

This is the full FlagsCache file, about 90% of it is from Unity Answers.

using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Text;

public class FlagsCache : MonoBehaviour {
    static public FlagsCache instance;
    void Awake()
    {
        instance = this;
    }
    static public WWW getCachedWWW(string url, string imgname)
    {
        string filePath = Application.persistentDataPath;
        filePath += "/" + imgname;
        string loadFilepath = filePath;
        bool web = false;
        WWW www;
        bool useCached = false;
        useCached = System.IO.File.Exists(filePath);
        if (useCached)
        {
            //check how old
            System.DateTime written = File.GetLastWriteTimeUtc(filePath);
            System.DateTime now = System.DateTime.UtcNow;
            double totalHours = now.Subtract(written).TotalHours;
            if (totalHours > 300)
                useCached = false;
        }
        if (System.IO.File.Exists(filePath))
        {
            string pathforwww = "file://" + loadFilepath;
            Debug.Log("TRYING FROM CACHE " + url + "  file " + pathforwww);
            www = new WWW(pathforwww);
        }
        else
        {
            web = true;
            www = new WWW(url);
        }
        instance.StartCoroutine(doLoad(www, filePath, web));
        return www;
    }

    static IEnumerator doLoad(WWW www, string filePath, bool web)
    {
        yield return www;

        if (www.error == null)
        {
            if (web)
            {
                //System.IO.Directory.GetFiles
                Debug.Log("SAVING DOWNLOAD  " + www.url + " to " + filePath);
                // string fullPath = filePath;
                File.WriteAllBytes(filePath, www.bytes);
                Debug.Log("SAVING DONE  " + www.url + " to " + filePath);
                //Debug.Log("FILE ATTRIBUTES  " + File.GetAttributes(filePath));
                //if (File.Exists(fullPath))
                // {
                //    Debug.Log("File.Exists " + fullPath);
                // }
            }
            else
            {
                Debug.Log("SUCCESS CACHE LOAD OF " + www.url);
            }
        }
        else
        {
            if (!web)
            {
                File.Delete(filePath);
            }
            Debug.Log("WWW ERROR " + www.error);
        }
    }
}

This is the one who calls the Cache file and tells them what to do. I wrote this one myself. I have been messing with them both for awhile trying to figure it out and learn a very rememberable lesson, but it just isn’t working out.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class FlagsLoader : MonoBehaviour {
    public Dropdown FlagOption;
    int FlagsType;

    public UnityEngine.UI.Button FlagPreview;
    public GameObject FlagsChooser;

    public string flagsURL;

    string[] RetrievedFlags;
    [SerializeField] Transform menuPanel;
    [SerializeField] GameObject buttonPrefab;

    public void StartFlags()
    {
        StartCoroutine(GetTheFlags());
    }

    //Fill Array with IEnumerator
    public void GetFlags()
    {
        if (FlagsType == 0)
        {
            flagsURL = "http://site.com/WorldFlags/";
        }
        else
        {
            if (FlagsType == 1)
            {
                flagsURL = "http://site.com/CustomFlags/";
            }
        }
        FlagsCache instance = new FlagsCache();

        for (int i = 0; i < RetrievedFlags.Length; i++)
        {
            GameObject button = (GameObject)Instantiate(buttonPrefab);
            string index = GetFlagData(RetrievedFlags[i], "Nation:");
            string imgname = GetFlagData(RetrievedFlags[i], "URL:");

            button.GetComponentInChildren<Text>().text = index;
            button.GetComponent<Button>().onClick.AddListener(
                () => { SetFlag(index, imgname); }
        );
            button.GetComponent<RawImage>().texture = FlagsCache.getCachedWWW(flagsURL + imgname, imgname).texture;
            button.GetComponent<RawImage>().SetNativeSize();
            button.transform.parent = menuPanel;

        }
    }

    string GetFlagData(string Data, string Indexes)
    {
        string value = Data.Substring(Data.IndexOf(Indexes) + Indexes.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        return value;
    }


    void SetFlag(string index, string flagurl)
{
        RegistrationController.Flag = index;
        FlagPreview.GetComponent<RawImage>().texture = FlagsCache.getCachedWWW(flagsURL + flagurl, flagurl).texture;
        FlagsChooser.SetActive(false);
    }

    public void ChangeFlagsDisplay()
    {
        if (FlagOption.value == 0)
        {
            FlagsType = 0;
            GetFlags();
        } else
        {
            if (FlagOption.value == 1)
            {
                FlagsType = 1;
                GetFlags();
            }
        }
    }



    IEnumerator GetTheFlags()
    {

        WWWForm FlagForm = new WWWForm();
        FlagForm.AddField("type", FlagsType);

        WWW GetFlagsData = new WWW("http://site.com//CustomFlags.php", FlagForm);
        yield return GetFlagsData;

        string GetFlagsDataString = GetFlagsData.text;
        print(GetFlagsDataString);
        RetrievedFlags = GetFlagsDataString.Split(';');
        GetFlags();

    }
}

Any help is extremely appreciated, and thank you so much for reading :).

You start the coroutine (line 42) but then immediately return the WWW object that the coroutine is waiting for to the caller which then tries to immediately access the texture property of that WWW object. My guess is that you were expecting the doLoad coroutine to pause the return of the WWW object until it was completed which is not the case.

1 Like

You, I like you

1 Like

(bah beaten to it but I’ve written it now :P)

I can’t believe I missed that. I have been making so many Coroutines lately it was bound I would eventually confuse them. Thank you so much!

Thank you I like you too :slight_smile:

1 Like