NullReferenceException তখন থ্রো হয় যখন null রেফারেন্সের মেম্বার (ফিল্ড, মেথড, প্রোপার্টি) এক্সেস করার চেষ্টা করা হয়।
উদাহরণ
using System;
class Demo {
static void Main() {
string str = null;
if (str.Length > 0) { // এখানে NullReferenceException
Console.WriteLine(str);
}
}
}
আউটপুট
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
প্রতিরোধ: ব্যবহার করার আগে null চেক করুন (if (str != null) বা str?.Length)।