All jobs / Backend developer / Backend Languages
Backend developer interview question · 0–1 years
What is the most common cause of a NullReferenceException in a new codebase?
The strongest answer
A reference that was never assigned or came back null from a lookup and was used without a check — nullable reference types and a null check at the boundary are what prevent it
Why it works
Right, and enabling nullable reference types moves most of these to compile time.
What a good answer covers
- Something was null and was dereferenced.
- Enable nullable reference types.
- Check at the boundary, not everywhere.
- ?. and ?? express intent clearly.
Answers that fall short
- Close, but not the strongest: Calling a static method before the class is initialised
Static initialisation raises a different exception. - Weak: Using var instead of an explicit type
var is just inference; it has no effect on null. - Red flag: Comparing two objects with ==
== on nulls is safe; dereferencing is not.
Who gets asked this
More Backend Languages questions
- What is the difference between == and is?
- When do you use an array rather than a List<T>?
- You want a value from a dict, or a default if the key is missing, without raising. What do you use?
- You remove items from a List<T> inside a foreach over it and get an exception. Why?
- if items: — what exactly is being tested?
- When is var a good idea?
- Building a long string in a loop with s += piece is slow on big inputs. Why?
- Why expose a property rather than a public field?