0

Is there a way to get out of this error. I know it is null but i already put try catch in it why its still showing error and the condition if it is null but the error still showing that my session is null. here's my code:

 try {

           if ('<%=Session["Selected"].ToString()%>' == null) {
               loadTimesheetgrid();
           }
           else {

               if ('<%=Session["Selected"].ToString()%>' == 'More Than 60 Hrs') {
                   //call the script you will make for morethan 60 hrs
               }
               else {
                   loadTimesheetgrid();
               }
           }
       }
       catch (error) {
           loadTimesheetgrid();
       }

Error showing:

System.NullReferenceException: Object reference not set to an instance of an object.

Ghasem
  • 14,455
  • 21
  • 138
  • 171
  • since `<%=Session["Selected"].ToString()%>` is in `''` it won't ever be null ... will it be `'null'` or just `''` - only you can say – Bravo Oct 10 '19 at 07:54
  • That looks like a Java error, not a JavaScript one. – Quentin Oct 10 '19 at 07:55
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – VLAZ Oct 10 '19 at 07:57
  • is that javascript supposed to catch the exception? cause the error showing is on the javascript part beacaus eit on line 117 and i dont have line 117 in behind code only in script part – Von Justine Napalang Oct 10 '19 at 08:03
  • from your template and error, it looks like the template is in some format like ejs or hbs etc which is being parsed at server end – Ayush Gupta Oct 10 '19 at 08:06
  • sorry what do you mean?,at the first load of the site the session if of course null it will have value after user click on the button, but whats happening at first load it'sl not load because the session is null that's why i need to handle in first load if it is null then i will excute the loadTimesheetgrid() method – Von Justine Napalang Oct 10 '19 at 08:11

2 Answers2

1

If your session value is null it will fail to convert it while .ToString() So better you first check whether it is null or it has some value, if it has then only try to convert it into string.

if ('<%=Session["Selected"]%>' != null) 
     {
               if ('<%=Session["Selected"].ToString()%>' == 'More Than 60 Hrs') 
                   {
                      //call the script you will make for morethan 60 hrs
                   }
              else {              
                      loadTimesheetgrid();               
                   }
     }
else {              
                   loadTimesheetgrid();               
     }

I would suggest you not to place try and catch blocks in your view file as sometimes if it fails it loads partial html elements and you may face some alignment issues. However it is your choice to wrap your code in try and catch blocks.

try
{
if ('<%=Session["Selected"]%>' != null) 
         {
                   if ('<%=Session["Selected"].ToString()%>' == 'More Than 60 Hrs') 
                       {
                          //call the script you will make for morethan 60 hrs
                       }
                  else {              
                          loadTimesheetgrid();               
                       }
         }
    else {              
                       loadTimesheetgrid();               
         }
}
catch(error)
{
loadTimesheetgrid();
}
Sarthak Gupta
  • 205
  • 1
  • 15
0

I'm going to assume that you're using ASP.Net given your post history.

With regard to your question, there's two issues here. Firstly the Session["Selected"] is null on the server side so calling ToString() on that is the cause of your error. If you're using C#6 then you can use the null coalescing operator to return a string instead. If not, then you would need a separate if condition.

Secondly you need to compare the value to an empty string in your client side JS, as '' will never equal null.

Here's a full example:

try {
  if ('<%= (Session["Selected"] ?? "").ToString() %>' === '') {
    loadTimesheetgrid();
  } else {
    if ('<%= (Session["Selected"] ?? "").ToString() %>' === 'More Than 60 Hrs') {
      //call the script you will make for morethan 60 hrs
    } else {
      loadTimesheetgrid();
    }
  }
} catch (error) {
  loadTimesheetgrid();
}

Depdending on your use case you may want to consider providing the session value to the View using a ViewModel, to DRY this up a little.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339