c# - Why NullReferenceException is not being caught? -


i have generic catch(exception ex) block in method. why not catching nullreferenceexception exception ? happens once in while.

public bool changeorder(string conumber, string recepitnumber, string webserviceurl, string username, string password,ref string error) {     error = "";     bool result = false;     try     {         movexapi objmovexapi = new movexapi();         objmovexapi.apiname = "zoi100mi";         objmovexapi.transactionname = "chghead";                      objmovexapi.url = webserviceurl;          objmovexapi.maxrecords = 0;         objmovexapi.inputkeyvaluepair = new dictionary<string, string>();         objmovexapi.returncolumns = new list<string>();          string url = objmovexapi.url + objmovexapi.apiname + "/" + objmovexapi.transactionname;         string returncols = string.join(",", objmovexapi.returncolumns.select(s => s.tostring()).toarray());         string maxrecords = objmovexapi.maxrecords.tostring();         string inputvalues = "";          objmovexapi.inputkeyvaluepair.add("cono", "100");         objmovexapi.inputkeyvaluepair.add("orno", conumber);         objmovexapi.inputkeyvaluepair.add("oref", recepitnumber);          foreach (keyvaluepair<string, string> kvp in objmovexapi.inputkeyvaluepair)         {             inputvalues += kvp.key + "=" + kvp.value + "&";  //"?cono=100&facf=h01&fact=h01&stsf=20&stst=90";         }         if (inputvalues.length > 0)         {             inputvalues = inputvalues.substring(0, inputvalues.length - 1);         }          httpwebrequest request = webrequest.create(url + "?" + inputvalues) httpwebrequest;          request.credentials = new networkcredential(username, password);          request.accept = "application/json";         request.contenttype = "application/json";          // response           using (httpwebresponse response = request.getresponse() httpwebresponse)         {             if (response.statuscode == httpstatuscode.ok)             {                 result = true;             }         }     }     catch (webexception wex)     {         error = ((httpwebresponse)wex.response).statusdescription;         result = false;     }     catch (exception ex) // shouldn't catch exceptions ???     {         error = ex.message;         result = false;     }     return result;  } 

exception stack trace below indicates nullreferenceexception occurred in above method. why not being caught exception ex ?

exception type: nullreferenceexception  exception message: object reference not set instance of object.  @ staffsales.m3.outbounddata.changeorder(string conumber, string recepitnumber, string webserviceurl, string username, string password, string& error)  @ staffsales.bl.customerorder.approvecolines(string conumber, string recepitno, string divi, string webserviceurl, string username, string password, list`1& apierrors) @ staffsales.controllers.homecontroller.processpayment(copaymentmodel objmodel) @ lambda_method(closure , controllerbase , object[] ) @ system.web.mvc.actionmethoddispatcher.execute(controllerbase controller, object[] parameters) @ system.web.mvc.reflectedactiondescriptor.execute(controllercontext controllercontext, idictionary`2 parameters) @ system.web.mvc.controlleractioninvoker.invokeactionmethod(controllercontext controllercontext, actiondescriptor actiondescriptor, idictionary`2 parameters) @ system.web.mvc.async.asynccontrolleractioninvoker.b__39(iasyncresult asyncresult, actioninvocation innerinvokestate) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresult`2.callenddelegate(iasyncresult asyncresult) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresultbase`1.end() @ system.web.mvc.async.asynccontrolleractioninvoker.endinvokeactionmethod(iasyncresult asyncresult) @ system.web.mvc.async.asynccontrolleractioninvoker.asyncinvocationwithfilters.b__3d() @ system.web.mvc.async.asynccontrolleractioninvoker.asyncinvocationwithfilters.<>c__displayclass46.b__3f() @ system.web.mvc.async.asynccontrolleractioninvoker.<>c__displayclass33.b__32(iasyncresult asyncresult) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresult`1.callenddelegate(iasyncresult asyncresult) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresultbase`1.end() @ system.web.mvc.async.asynccontrolleractioninvoker.endinvokeactionmethodwithfilters(iasyncresult asyncresult) @ system.web.mvc.async.asynccontrolleractioninvoker.<>c__displayclass21.<>c__displayclass2b.b__1c() @ system.web.mvc.async.asynccontrolleractioninvoker.<>c__displayclass21.b__1e(iasyncresult asyncresult) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresult`1.callenddelegate(iasyncresult asyncresult) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresultbase`1.end() @ system.web.mvc.async.asynccontrolleractioninvoker.endinvokeaction(iasyncresult asyncresult) @ system.web.mvc.controller.b__1d(iasyncresult asyncresult, executecorestate innerstate) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncvoid`1.callenddelegate(iasyncresult asyncresult) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresultbase`1.end() @ system.web.mvc.controller.endexecutecore(iasyncresult asyncresult) @ system.web.mvc.controller.b__15(iasyncresult asyncresult, controller controller) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncvoid`1.callenddelegate(iasyncresult asyncresult) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresultbase`1.end() @ system.web.mvc.controller.endexecute(iasyncresult asyncresult) @ system.web.mvc.controller.system.web.mvc.async.iasynccontroller.endexecute(iasyncresult asyncresult) @ system.web.mvc.mvchandler.b__5(iasyncresult asyncresult, processrequeststate innerstate) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncvoid`1.callenddelegate(iasyncresult asyncresult) @ system.web.mvc.async.asyncresultwrapper.wrappedasyncresultbase`1.end() @ system.web.mvc.mvchandler.endprocessrequest(iasyncresult asyncresult) @ system.web.mvc.mvchandler.system.web.ihttpasynchandler.endprocessrequest(iasyncresult result) @ system.web.httpapplication.callhandlerexecutionstep.system.web.httpapplication.iexecutionstep.execute() @ system.web.httpapplication.executestep(iexecutionstep step, boolean& completedsynchronously) 

this line of code possible issue;

error = ((httpwebresponse)wex.response).statusdescription; 

according msdn article https://msdn.microsoft.com/en-us/library/system.net.webexception.response(v=vs.110).aspx

webexception.response property

property value type: system.net.webresponse if response available internet resource, webresponse instance contains error response internet resource; otherwise, null.

that means need check nulls.

 if(wex.response != null)  {      error = ((httpwebresponse)wex.response).statusdescription;  } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -