Nullable object must have a value… why? what? huh?

Category : VB.Net

Ok, here is the situation, you have a method in a class that accepts a number of parameters.

myMethod (byval myint as integer, byval mydouble as double, etc.)

Now in your class you declare variables that you will send to that method but you want them to be Nullables because you are getting them from a database.

So you declare:

dim myInteger2 as Nullable(of Integer)
dim myDouble2 as Nullable(of Integer)

Then you test them from your dataset.

If Not mydatasetpieceofData("TheInteger") is DBNull.Value then
myInteger2 = mydatasetpieceofData("TheInteger")

etc….

Now you pass these variables onto your method….

myMethod( myInteger2, myDouble2 )

Run it and you will get an error that says “Nullable Object must have a value.

If you are an idiot like me, you will search and search the internet and finally find the answer. THEN DO NOTHING and forget about it in 2 weeks UNTIL YOU RUN INTO THIS STUPID ERROR AGAIN!!!!!!

Or if you are smart, you will bookmark this post or copy the answer …whatever… WRITE THIS DOWN SOMEWHERE!!! It is one of those stooopid errors that will drive you crazy and waste TONS of your time!!!!

** ANSWER:
————————————————————————————–
YOUR METHOD HAS TO LOOKING FOR NULLABLES! Our method needs to look like this:

myMethod (byval myint as Nullable(Of Integer), _
byval mydouble as Nullable(Of Double),etc.)

This may seem obvious to some, but it has occured in our workplace several times — it’s just ONE OF THOSE THINGS that you have to watch out for.  So I hope this helps….and oh ya, WRITE THIS DOWN!!!!