question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing msg.MailingListHelp breaks with Null reference exception when not present.

See original GitHub issue

This 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:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bendiknesbocommented, May 11, 2018

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:

var pName = prop.Name;
try {
    var value = prop.GetValue(msg);
    if (value is string str) {
        filedetails.Add(pName, str);
    }
} catch (Exception ex) {
    Log.Error($"Property \"{pName}\" in object Storage.Message was not retrievable.", ex);
    continue;
}
1reaction
bendiknesbocommented, May 11, 2018

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 NullReferenceExceptions.

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.

private readonly List<string> _msgReaderExcludeProperties = new List<string> {
	nameof(Storage.Message.MailingListHelp),
	nameof(Storage.Message.MailingListUnsubscribe),
};
public Dictionary<string, string> GetMsgFileDetails(string filepath) {
	var filedetails = new Dictionary<string, string>();
	var fullpath = Path.Combine(SelectedFolder, filepath);
	using (var msg = new Storage.Message(fullpath)) {
		foreach (var prop in msg.GetType().GetProperties()) {
			var pName = prop.Name;
			if (_msgReaderExcludeProperties.Contains(pName)) {
				continue;
			}

			var value = prop.GetValue(msg);
			// We only want the strings
			if (value is string str) {
				filedetails.Add(pName, str);
			}
		}
	}
	return filedetails;
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found