c# - Object reference not set to an instance of an object (int) -


this error keeps popping , can't seem figure out it's coming from.

if (!ispostback) {     datatable localcart = new datatable();     localcart = (datatable)session["cart"];     int localcartitemcount = (int) session["cartitemcount"];     decimal localcartamount = (decimal)session["cartamount"];      if (localcart.rows.count == 0)     {         titlelabel.text = "your shopping cart empty!";         gridcart.visible = false;         updatebutton.enabled = false;         checkoutbutton.enabled = false;         totalamountlabel.text = string.format("{0:c}", 0);     }     else     {         gridcart.datasource = localcart;         gridcart.databind();         titlelabel.text = "these products in shopping cart:";         gridcart.visible = true;         updatebutton.enabled = true;         checkoutbutton.enabled = true;         totalamountlabel.text = string.format("{0:c}", localcartamount);     } 

it's saying error here -> int localcartitemcount = (int) session["cartitemcount"];

you not checking see if key "cartitemcount" exists in session. if not exist, result of session["cartitemcount"] return null , create error when trying cast null (int).


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -