An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Hello @Yash Bokade and thank you for reaching out.
The code you've shared already checks for null before calling ToUpper(), so it should not cause a NullReferenceException under normal circumstances.
A few points worth noting:
- ReadLine() returns null at the end of the file, while empty lines are returned as
"". CallingToUpper()on an empty string is safe. - You can use string.IsNullOrWhiteSpace(line) if you also want to skip lines containing only spaces or tabs.
- Issues such as a missing file or insufficient access permissions generally produce different exceptions rather than NullReferenceException.
If you're still encountering the exception, could you confirm which line throws it and share the exception's stack trace?
It may also be worth checking whether the code currently being executed matches the snippet you've posted, since the null check shown here should already prevent this particular issue.
If you found my response helpful or informative, I would greatly appreciate it if you could follow this guide for your confirmation. Thank you.