tags:

views:

409

answers:

8

I must be going out of my mind, because my unit tests are failing because the following code is throwing a null ref exception:

int pid = 0;
if (parentCategory != null)
{
    Console.WriteLine(parentCategory.Id);
    pid = parentCategory.Id;
}

The line that's throwing it is:

pid = parentCategory.Id;

The console.writeline is just to debug in the NUnit GUI, but that outputs a valid int.

Edit: It's single-threaded so it can't be assigned to null from some other thread, and the fact the the Console.WriteLine successfully prints out the value shows that it shouldn't throw.

Edit: Relevant snippets of the Category class:

public class Category
{
    private readonly int id;

    public Category(Category parent, int id)
    {
        Parent = parent;
        parent.PerformIfNotNull(() => parent.subcategories.AddIfNew(this));
        Name = string.Empty;
        this.id = id;
    }
    public int Id
    {
        get { return id; }
    }
}

Well if anyone wants to look at the full code, it's on Google Code at http://code.google.com/p/chefbook/source/checkout

I think I'm going to try rebooting the computer... I've seen pretty weird things fixed by a reboot. Will update after reboot.

Update: Mystery solved. Looks like NUnit shows the error line as the last successfully executed statement... Copy/pasted test into new console app and ran in VS showed that it was the line after the if statement block (not shown) that contained a null ref. Thanks for all the ideas everyone. +1 to everyone that answered.

+1  A: 

Yes, could we see the code of the category class .. in particular the Id property? Assuming the Id property is an int (and not a nullable int), the get method must be accessing a NULL object.

misteraidan
+2  A: 

What doesn't make sense, though, is that the OP states that the console.writeline outputs a valid int and it calls the same property as the line where he states the error is being thrown.

A stacktrace would be helpful, or being able to look at the actual unit test.

David Yancey
+1  A: 

Id could be a nullable int type (int?) with no value, however I feel that the property Id must be doing more than just returning an int, and something inside that is null.

EDIT Your edit reveals this is more than peculiar, could you supply us with a stack trace?

johnc
+7  A: 

Based on all the info so far, I think either "the line that's throwing is" is wrong (what makes you think that), or possibly your 'sources' are out of sync with your built assemblies.

This looks impossible, so some 'taken for granted assumption' is wrong, and it's probably the assumption that "the source code you're looking at matches the process you're debugging".

Brian
Good point ....
johnc
@Brian, don't you think that if he had mismatch code VS would give him a warning?
Vadim
@Vadim: I may have missed it, but I haven't seen evidence on the thread that he's even necessarily using VS.
Brian
if there is a mismatch between code and assembly..how is he able to debug it?
Christian Hagelid
+1  A: 

I'm not an expert on C#, and am not sure if it makes a difference, but are you debugging in Debug mode, or Release mode? If you're in release mode, the line that your IDE is pointing to and the line the problem is actually on may be different (I know this is the case in C++ when using Visual Studio)

Smashery
+1  A: 

I'd have to agree with Brian... Maybe you're using outdated .pdbs and the code you're seen on debug mode does not comply with the code actually being debugged.

Try cleaning the project, and rebuilding it in debug mode.

+3  A: 

When things look right on the surface then it's likely something you would dismiss. Are you 350% positive that your DLL/PDB matches your source code thus giving you the right line?

  • Try manually deleting your assemblies and run whatever it is you're running. Does it fail as it should?
  • Try cleaning out your solution and recompiling. Copy the assemblies to wherever and run it again. Does it work this time? Null ref at the same spot?
  • Debug the surrounding lines for values you expect. parentCategory, etc. should match what you think.
  • Modify the code by throwing an exception. Do the stack trace lines match exactly the lines in your code?

I've had some extremely mind-boggling experiences just like this because one of my assumptions was wrong. Typical is a stale assembly. Question all assumptions.

Colin Burnett
I've definitely seen that happen before. Not the case this time, but definitely something to check for
Davy8
A: 

Looks like NUnit shows the error line as the last successfully executed statement... Copy/pasted test into new console app and ran in VS showed that it was the line after the if statement block (not shown) that contained a null ref. Thanks for all the ideas everyone. +1 to everyone that answered.

Davy8
Which still shows us, however, that Console.WriteLine(parentCategory.Id); is throwing a null. What causes that?
johnc
I interepret 'line after the if' to mean 'line after the close-curly of the 'if' block, e.g. some code that OP never showed us.
Brian
@lagerdalek - if NUnit is showing the last successful line, and it's showing pid = parentCategory.Id;, then Console.WriteLine(parentCategory.Id); will have run fine. The line which breaks is the one after pid =..., which Davy8 hasn't shown us (not that it matters)
Smashery
Right, I meant the line after the if block that is not shown.
Davy8
Edited to clarify
Davy8
Ah, gotcha, thanks
johnc