2

I am trying to test values before doing some actions with session variables. This is for initializing( As you can see Session("Chemin") is a List of String:

        @If (IsDBNull(Session("Chemin")) Or (ViewContext.RouteData.Values("action") = "Index")) Then
        @Code Dim lst As New List(Of String)()
        Session("Chemin") = lst  // Initialisation
     End Code
End If

But the problem is with the test here :

@If (Not IsDBNull(ViewContext.RouteData.Values("action")) AndAlso Not IsDBNull(Session("Chemin")) AndAlso Not Session("Chemin").Contains((ViewContext.RouteData.Values("action").ToString()))) Then

I sometimes get

System.NullReferenceException

I don't understand because I am just testing it, and yet it throws me an error. So my question is : Why and when exactly does it happens ? How to fix this ? Edit: not a duplicate because not a simple System.NullReferenceException

  • try changing it to `ViewContext.HttpContext.Request.QueryString.Get("action");` – Shashank Sood Jul 07 '16 at 10:11
  • ToString can't return System.DbNull which is what IsDbNull is comparing it to, your evaluation there will always return false. – Esko Jul 07 '16 at 10:32
  • Esko, It's the same without ToString I put it there because I was desperate, I was using it later . By I agree it was dumb – Gabriel Lemelle Jul 07 '16 at 10:50
  • Shashank, I get the same error – Gabriel Lemelle Jul 07 '16 at 11:01
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Enigmativity Jul 07 '16 at 11:01
  • why are you using IsDBNull? if the session value is a string use String.isnullorempty – Mark Homer Jul 07 '16 at 11:13
  • If there always a value in Session("Chemin")? If not you should check it before trying to use it. – Esko Jul 07 '16 at 11:52
  • Try if (Session("Chenin") = Nothing instead of dbnull – Haim Katz Jul 07 '16 at 12:19
  • See the section "DBNull is not the same as Nothing" at the selected answer on the duplicate target. Also, take note of the instructions on how to debug your error. A NRE is trivial to fix, and you should learn immediately how to track it down and avoid it in future. –  Jul 07 '16 at 13:05

2 Answers2

1

You should replace all your IsDBNull with IsNothing, that is what you are looking for in this case. Because I think your

    @If (IsDBNull(Session("Chemin"))

could not pass and so the Session("Chemin") could be Nothing.

You should check that ViewContext, ViewContext.RouteData , ViewContext.RouteData.Values and ViewContext.RouteData.Values("action") are not nothing just in case.

You can do it with this :

                                                @Code Dim values = ViewContext?.RouteData?.Values End Code
                                        @If (values IsNot Nothing) // And the rest of your tests
ElChapo
  • 228
  • 3
  • 13
0

First: DbNull is not the same as null (which is Nothing in VB slang). So you should check whether that method IsDbNull() crashes if you call it like this: IsDbNull(Nothing). I think so, but I'm not sure. If so, add that additional null-check and you're good.

If the problem is still present, lets dive deeper:

Is an expression like ViewContext.RouteData.Values("action") all the properties in the chain could be null. This means if ViewContext, RouteData or even the Values are null, this exception will be thrown.

The same for the Session itself: it is a kind of value container and you check whether a value at a given key in that container is null. But what if the Session itself is null? The same applies to the Values property as well.

Basically this would translate to null.ElementAt("Chemin"). And that will crash before the surrounding IsDbNull() gets called.

So you might check like this:

Session Is Nothing OrElse IsDBNull(Session("Chemin"))
' note: you might want to check if the session contains the key before getting a value with it

And

Dim values = ViewContext?.RouteData?.Values ' see "Elvis Operator" for the question marks
If (values IsNot Nothing AndAlso values("action") = "Index") Then
Waescher
  • 5,361
  • 3
  • 34
  • 51
  • If Session is null how do I un-null it ? Can Session be null ? And btw the error is on the second test so why would it pass the first and not the second ? – Gabriel Lemelle Jul 07 '16 at 13:01
  • If the first test is okay then your `Session` property is not null. In this case you can focus on the second test and check the property chain for nulls - this is pretty basic stuff I won't explain in here. – Waescher Jul 07 '16 at 13:39