0

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
Herdo
  • 5,643
  • 2
  • 34
  • 60
  • 1
    I'm going for `sendungsTitel` being null. – David Arno Jul 15 '15 at 14:07
  • 2
    There's a chance VS is pointing to the wrong line, and that the exception was actually thrown from somewhere else. Try cleaning and rebuilding the solution. Also, check the stack trace. – dcastro Jul 15 '15 at 14:08
  • @DavidArno Sorry. Copy paste error due to company restrictions of publishing source code :) Fixed it. – Herdo Jul 15 '15 at 14:13
  • Have you built in Release mode or Debug? Can you post the stack trace of the exception? – Matteo Umili Jul 15 '15 at 14:19
  • @codroipo Release mode. – Herdo Jul 15 '15 at 14:32
  • So probably the exception has been thrown elsewhere, but VS point to the wrong line as @dcastro said. Do you have the same error in Debug mode? You should look at the exception's stack trace – Matteo Umili Jul 15 '15 at 14:38
  • @codroipo Going to look into the debug mode. Added the _censored_ stack trace. – Herdo Jul 15 '15 at 14:41

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.

Herdo
  • 5,643
  • 2
  • 34
  • 60
-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
  • 133
  • 9
-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.