Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

bug: NullReferenceException

Discussion in 'Scripting' started by YY2266, Nov 20, 2015.

  1. YY2266

    YY2266

    Joined:
    Feb 17, 2015
    Posts:
    1
    Help!

    I encountered two bugs related to NullReferenceException and the error lines are like this:

    (1) cs: 36

    public void loadStringTable(string filename)
    {
    TextAsset ta = (TextAsset)Resources.Load("Text/" + filename, typeof(TextAsset));
    if (m_stringTable != null) {
    m_stringTable = null;
    }
    m_stringTable = MiniJSON.Json.Deserialize(ta.text) as Dictionary<string, object>;
    Resources.UnloadAsset(ta);

    (2) cs: 186

    public void fadeOut()
    {
    objMask.GetComponent<Renderer>().material.color = new Color(0.0f, 0.0f, 0.0f, 1.0f);
    objMask.GetComponent<Renderer>().enabled = true;
    HOTween.To(objMask.GetComponent<Renderer>().material, 0.5f, new TweenParms().Prop("color", new Color(0.0f, 0.0f, 0.0f, 0.0f)).Ease(EaseType.Linear));
    Invoke("onDone_FadeOut", 0.5f);

    }
    void onDone_FadeOut()
    {
    objMask.GetComponent<Renderer>().enabled = false;
    if(m_nextScene.Substring(0, 4) == "Sce_" && !Dialogue.IsEnd){Dialogue.checkEnd();}


    I am not a programmer but had learnt a bit of c#. How do I modify those bugs and why do they happen?
    Thank you!!
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    It's really hard to read the code in your post. Please use code tags.
     
  3. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
  4. btft

    btft

    Joined:
    Aug 11, 2015
    Posts:
    14
    No idea how you want us to know which line is line 36 and 186.
    Any chance on telling us on which page IDE opens when you double-click error in Unity Editor console?

    In first method possible points of failure are:
    - null string - filename

    I would also consider to always wrap Resources.Load with try-catch block as it will throw an exception on failure.

    Also GetComponent<T>() will return null if not found, so I would recommend to assign for example:

    Code (CSharp):
    1. public void fadeOut()
    2. {
    3. //Check for nulls down here:
    4. if (objMask == null) return;
    5. var renderer = objMask.GetComponent<Renderer>();
    6. if (renderer == null) return;
    7. //Now renderer will be used only if it's not null.
    8. renderer.material.color = new Color(0.0f, 0.0f, 0.0f, 1.0f);
    9. renderer.enabled = true;
    10. HOTween.To(objMask.GetComponent<Renderer>().material, 0.5f, new TweenParms().Prop("color", new Color(0.0f, 0.0f, 0.0f, 0.0f)).Ease(EaseType.Linear));
    11. Invoke("onDone_FadeOut", 0.5f);
    12.  
    13. }
    Generally NullPointerException tells you that variable of reference type has not be assigned or points to object that has been already disposed/destroyed.