Accessing msg.MailingListHelp breaks with Null reference exception when not present.
See original GitHub issueThis code:
string fullpath = Path.Combine(SelectedFolder, filepath); var msg = new Storage.Message(fullpath); var msgmlhelp = msg.MailingListHelp;
causes a Null reference exception in my code - target .Net 4.6.2. WPF app. I came across it because, not knowing which properties I actually want out of the file (I don’t need mailinglisthelp), I wrote this to extract the data:
public Dictionary<string, string> GetMsgFileDetails(string filepath)
{
Dictionary<string, string> filedetails = new Dictionary<string, string>();
//try
{
string fullpath = Path.Combine(SelectedFolder, filepath);
var msg = new Storage.Message(fullpath);
var msgmlhelp = msg.MailingListHelp;
foreach (var prop in msg.GetType().GetProperties())
{
string pname = prop.Name;
if (prop.GetValue(msg) != null)
{
object pvalue = prop.GetValue(msg);
}
// We only want the strings
if (prop.GetValue(msg) != null && (prop.GetValue(msg) is string))
{
filedetails.Add(prop.Name, prop.GetValue(msg).ToString());
}
}
return filedetails;
}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message, "Error trying to load file");
// return null;
//}
} (try catch disabled to aid debugging) s
It was breaking out in the test for null and I found it was MailingListHelp causing it. I didnt get any further so there could be others perhaps? Any thoughts on how to work around this ? Thanks, Brett
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
c# - Property.GetValue - Test for null breaks with inner ...
Main() Inner Exception 1: NullReferenceException: Object reference not set to an instance of an object. This seems to be due to the value...
Read more >Completely devoid of details · Issue #3858 · dotnet/runtime ...
If I get a NullReferenceException on this line, there are three different objects that could have been null and caused the error ...
Read more >How can I fix the error: System.NullReferenceException
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception ...
Read more >c# - Determining the object that caused a null reference ...
To prevent an X/Y problem, the goal is to simply record what object causes a null reference exception to be thrown, since the...
Read more >Object Reference Not Set to an Instance of an Object
This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you want to future-proof it by only populating your dictionary with the properties that are retrievable by ProperyInfo.GetValue, then you can rewrite the body of the foreach as such:
I did a quick check on a .NET 4.6 Unittest-project, using your method as a base, and found one more problematic property: MailingListUnsubscribe.
@Sicos1977 should probably look into why those two properties are throwing
NullReferenceException
s.If you do not need those two properties, then here is a method you can use to get all properties except two that cause problems. It is not future-proofed, so if any other properties seem to cause problems, then it will need to be modified.