facebook twitter hatena line email

「Unity/Csharp/Exception/NullReferenceException」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(GameObject.Findで、NullReferenceException発生)
(UnityEditroで、try-catchした場合)
 
(同じ利用者による、間の10版が非表示)
行20: 行20:
 
}
 
}
 
</pre>
 
</pre>
 +
===対応方法===
 +
null判定するが、一番良いかも
  
===対応方法1===
+
参考:https://baba-s.hatenablog.com/entry/2021/11/16/090000
is not nullを使う(これが一番良いかも)
+
====対応方法1====
 +
null判定する
 
<pre>
 
<pre>
if (text is not null)
+
if (text != null)
 
{
 
{
 
     text.text = "hoge";
 
     text.text = "hoge";
 
}
 
}
 
</pre>
 
</pre>
===対応方法2===
+
 
null判定する
+
====対応方法2====
 +
is not nullを使う
 
<pre>
 
<pre>
if (text != null)
+
if (text is not null)
 
{
 
{
 
     text.text = "hoge";
 
     text.text = "hoge";
行38: 行42:
 
</pre>
 
</pre>
  
===対応方法3===
+
====対応方法3====
 
ReferenceEqualsを使う
 
ReferenceEqualsを使う
 
<pre>
 
<pre>
行69: 行73:
  
 
===対策方法4===
 
===対策方法4===
上の対策方法でも良いが、go?とnull判定してもよい。
+
上の対策方法でも良いが、go?とnull判定できるが、挙動がおかしくなるという情報がある。
 
<pre>
 
<pre>
 
Debug.Log(go?.name);
 
Debug.Log(go?.name);
 +
</pre>
 +
参考:https://light11.hatenadiary.com/entry/2019/09/01/223507
 +
 +
==NullReferenceExceptionの出方==
 +
===UnityEditorで、try-catchなしの場合のエラー===
 +
<pre>
 +
public class NullReferenceExceptionScene : MonoBehaviour
 +
{
 +
    [SerializeField] Text text;
 +
    void Start()
 +
    {
 +
        text.text = "hoge";
 +
    }
 +
}
 +
</pre>
 +
エラーメッセージ
 +
<pre>
 +
NullReferenceException: Object reference not set to an instance of an object
 +
NullReferenceExceptionScene.Start () (at Assets/Scripts/Scene/NullReferenceExceptionScene.cs:11)
 +
</pre>
 +
sentryにエラーを送ったときのエラーメッセージ
 +
<pre>
 +
SentryProject/Assets/Scripts/Scene/NullReferenceExceptionScene.cs in Start at line 11:9
 +
</pre>
 +
 +
===UnityEditroで、try-catchした場合===
 +
<pre>
 +
using UnityEngine;
 +
using UnityEngine.UI;
 +
using System;
 +
 +
public class NullReferenceExceptionScene : MonoBehaviour
 +
{
 +
    [SerializeField] Text text;
 +
    void Start()
 +
    {
 +
        try
 +
        {
 +
            text.text = "hoge";
 +
        }
 +
        catch (Exception e)
 +
        {
 +
            Debug.LogError("Exception: " + e);
 +
        }
 +
    }
 +
}
 +
 +
</pre>
 +
エラーメッセージ
 +
<pre>
 +
Exception: System.NullReferenceException: Object reference not set to an instance of an object
 +
  at NullReferenceExceptionScene.Start () [0x00000] in SentryProject/Assets/Scripts/Scene/NullReferenceExceptionScene.cs:15
 +
UnityEngine.Debug:LogError (object)
 +
NullReferenceExceptionScene:Start () (at Assets/Scripts/Scene/NullReferenceExceptionScene.cs:19)
 
</pre>
 
</pre>

2023年10月25日 (水) 17:38時点における最新版

SerializeFieldで、NullReferenceException発生

以下SerializeFieldのtextに、オブジェクトを入れなかった場合、NullReferenceExceptionが発生する。

詳細エラー

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

サンプル

using UnityEngine;
using UnityEngine.UI;

public class NullReferenceExceptionScene : MonoBehaviour
{
    [SerializeField] Text text;
    void Start()
    {
        text.text = "hoge";
    }
}

対応方法

null判定するが、一番良いかも

参考:https://baba-s.hatenablog.com/entry/2021/11/16/090000

対応方法1

null判定する

if (text != null)
{
    text.text = "hoge";
}

対応方法2

is not nullを使う

if (text is not null)
{
    text.text = "hoge";
}

対応方法3

ReferenceEqualsを使う

if (!ReferenceEquals(text, null))
{
    text.text = "hoge";
}

参考:https://bluebirdofoz.hatenablog.com/entry/2022/09/18/234745

GameObject.Findで、NullReferenceException発生

GameObject.Findで存在しない、オブジェクト名を入れると、NullReferenceExceptionが発生する。

詳細エラー

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

サンプル

public class NullReferenceExceptionScene : MonoBehaviour
{
    void Start()
    {
        GameObject go = GameObject.Find("test");
        Debug.Log(go.name);
    }
}

対策方法4

上の対策方法でも良いが、go?とnull判定できるが、挙動がおかしくなるという情報がある。

Debug.Log(go?.name);

参考:https://light11.hatenadiary.com/entry/2019/09/01/223507

NullReferenceExceptionの出方

UnityEditorで、try-catchなしの場合のエラー

public class NullReferenceExceptionScene : MonoBehaviour
{
    [SerializeField] Text text;
    void Start()
    {
        text.text = "hoge";
    }
}

エラーメッセージ

NullReferenceException: Object reference not set to an instance of an object
NullReferenceExceptionScene.Start () (at Assets/Scripts/Scene/NullReferenceExceptionScene.cs:11)

sentryにエラーを送ったときのエラーメッセージ

SentryProject/Assets/Scripts/Scene/NullReferenceExceptionScene.cs in Start at line 11:9

UnityEditroで、try-catchした場合

using UnityEngine;
using UnityEngine.UI;
using System;

public class NullReferenceExceptionScene : MonoBehaviour
{
    [SerializeField] Text text;
    void Start()
    {
        try
        {
            text.text = "hoge";
        }
        catch (Exception e)
        {
            Debug.LogError("Exception: " + e);
        }
    }
}

エラーメッセージ

Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at NullReferenceExceptionScene.Start () [0x00000] in SentryProject/Assets/Scripts/Scene/NullReferenceExceptionScene.cs:15 
UnityEngine.Debug:LogError (object)
NullReferenceExceptionScene:Start () (at Assets/Scripts/Scene/NullReferenceExceptionScene.cs:19)