1

Note: This is not answerable by What is a NullReferenceException, and how do I fix it?

I came across a strange error right now. My call of Int32.TryParse(string, out int) is giving me a System.NullReferenceException.

The idea is pretty simple - replace a metadata entry with a technical representation, rather than using an integer value:

public void GenerateClipName()
{
    // Copy metadata entries, to prevent modifications on the actual viewmodel
    var metadataEntries = ViewModel.MasterObject.MetadataEntries.Copy();
    // Get the correct entry
    var entry = metadataEntries.Single(m => m.Key.EndsWith("/" + MetaDataKeys.TITLE));
    // Get all valid entries for the metadata key
    var validEntries = MetadataDefinitions.Single(x => x.Id.EndsWith("/" + MetaDataKeys.TITLE));
    int parsedInt;
    // Try to parse the integer value, and replace it with the technical representation
    if (int.TryParse(entry.Value, out parsedInt))
        entry.Value = validEntries.ValidEntries.Single(m => m.Value == parsedInt).TechnicalRepresentation;

    // Some further actions will be implemented here later
}

But the "expected output" is more like an "unexpected output":

Int32.TryParse throwing NullReferenceException

As you can see in the Locals window below the editor window: the value of entry.Value is "86".

EDIT #1: As requested, the variables before Int32.TryParse gets executed: enter image description here

EDIT #2: Exception StackTrace:

at ...Presenter.GenerateClipName()
at ...Presenter.Cancel()
at ...View.CancelButton_Click(object sender, System.Windows.RoutedEventArgs e)

The stack trace doesn't include the Int32.TryParse method, which is wondering me somehow.

Community
  • 1
  • 1
Stefan Over
  • 5,851
  • 2
  • 35
  • 61

3 Answers3

1

Finally figured it out:
The debug resources, while running in Release mode, were the same as the compiled Release resources. However, due to jumping around in the code by dragging the (yellow) debug cursor, Visual Studio messed up the information at specific lines. Therefore, the NullReferenceException was thrown.

Stefan Over
  • 5,851
  • 2
  • 35
  • 61
-2

The out parameter modifier can't return a null value, and for what I see in your debugging window it seems to be the case. Try giving it a value.

Although variables passed as out arguments do not have to be initialized before being passed, the called method is required to assign a value before the method returns.

About out parameters: https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx

And regarding your code, you are using int.TryParse(string, out int), try changing it to Int32.TryParse(string, out int) and see if that solves the problem.

Gonçalo
  • 144
  • 11
-3

I suspect the null reference has nothing to do with your string, but rather with your out variable: Notice at the bottom of your screenshot, your parsedInt is null. Personally I have no love for nullable integers, but if you simply initialise it to 0 when you create it (int parsedInt = 0;) it should solve your problem.