初級 › Object reference not set エラー(null参照)の原因 › パターン4
Object reference not set エラー(null参照)の原因
C#で最も有名な実行時エラーです。
1 class Program { 2 static void Main(string[] args) { 3 string msg = ______; ^ 4 System.Console.WriteLine(msg.Length); 5 } 6 }
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Program.Main(String[] args) in Program.cs:line 4
空欄を埋めてビルドを通す
msgがnullのままLengthにアクセスするとNullReferenceExceptionになります。
次の問題