I have a fairly simple web app that is working locally, but after deploying it to a server, the controller is throwing a null reference exception. Here is the stack trace.
[NullReferenceException: Object reference not set to an instance of an object.]
ExcelEmailImporter.Controllers.ExcelImporterController.Emails(String action) +2068
lambda_method(Closure , ControllerBase , Object[] ) +126
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +241
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +38
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation ierInvokeState) +11
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +138
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +111
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +452
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +37
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +241
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState ierState) +29
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState ierState) +51
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
The jQuery function:
$('#btnUpdate').click(function () {
var arrayOfChildren = [];
$('#EmailBody tr').each(function () {
var childNodes = $(this).find("td");
var childValues = []
childValues[0] = childNodes[0].ierHTML;
childValues[1] = childNodes[1].ierHTML;
childValues[2] = childNodes[2].ierHTML;
childValues[3] = childNodes[3].ierHTML;
arrayOfChildren.push(childValues);
})
$.ajax({
url: "/ExcelImporter/Emails",
type: "POST",
data: { action: JSON.stringify(arrayOfChildren) },
contentType: "application/json; charset=utf-8"
});
});
And the Controller being used:
public class ExcelImporterController : Controller
{
[HttpPost]
public ActionResult Emails(string action)
{
if (Request != null)
{
if (action.Equals("UploadExcel"))
{
HttpPostedFileBase file = Request.Files["file"];
if ((file != null) && !string.IsNullOrEmpty(file.FileName))
{
string fileName = file.FileName;
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
ExcelUploadModel uploadModel = new ExcelUploadModel();
using (var package = new ExcelPackage(file.InputStream))
{
ExcelWorksheets currentSheet = package.Workbook.Worksheets;
ExcelWorksheet workSheet = currentSheet.First();
int noOfCol = workSheet.Dimension.End.Column;
int noOfRow = workSheet.Dimension.End.Row;
for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
{
EmailContentModel ec = new EmailContentModel();
ec.EmailTo = workSheet.Cells[rowIterator, 1].Value.ToString();
uploadModel.EmailContentList.Add(ec);
}
}
retu View(uploadModel);
}
}
else if (Request.AcceptTypes.Contains("application/json"))
{
List<List<String>> modelDataList = JsonConvert.DeserializeObject<List<List<String>>>(action);
using (SqlCoection con = new SqlCoection(ConfigurationManager.CoectionStrings["EmailDatabaseCoection"].CoectionString))
{
foreach (List<string> valueList in modelDataList)
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO EmailContent (EmailContentKey, EmailAddress,EmailFrom ,Subject, Body, EmailID, IsSent) SELECT NEWID(),@EmailTo,@EmailFrom, @EmailSubject, @EmailBody,(SELECT Count(*) FROM EmailContent),0", con))
{
var _with1 = cmd;
_with1.CommandType = CommandType.Text;
_with1.Parameters.AddWithValue("@EmailTo", valueList[0]);
_with1.Parameters.AddWithValue("@EmailFrom", valueList[1]);
_with1.Parameters.AddWithValue("@EmailSubject", valueList[2]);
_with1.Parameters.AddWithValue("@EmailBody", valueList[3]);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
con.Close();
}
con.Close();
}
}
}
retu View();
}
}
retu View();
}
[HttpGet]
public ActionResult Emails()
{
ExcelUploadModel newModel = new ExcelUploadModel();
ViewBag.Message = "Import emails from excel.";
retu View(newModel);
}
}
Again, this works locally but I'm lost as to what's causing this on the server. Thanks for any help/advice!
