Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all,i could not able to solve the problem
Object reference not set to an instance of an object.


i an uploading an excel and it calculates the values and gives the output for one filename which in compatibility mode.after uploading another file it gives the following error
..please help


Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:


Line 642: finally
Line 643: {
Line 644: workbook.Close(Missing.Value, Missing.Value, Missing.Value);
Line 645: application.Quit();
Line 646: this.releaseObject(xlWs);

Source File: c:\Users\ccbt\Desktop\PPDM_TEST_HITVALIDATION_MODULE\SECONDARY_VALUES_STORAGE\SECONDARY_VALUES_STORAGE\test123.aspx.cs Line: 644

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
SECONDARY_VALUES_STORAGE.test123.GetExcel(String fileName) in c:\Users\ccbt\Desktop\PPDM_TEST_HITVALIDATION_MODULE\SECONDARY_VALUES_STORAGE\SECONDARY_VALUES_STORAGE\test123.aspx.cs:644
SECONDARY_VALUES_STORAGE.test123.btnUpload_Click_Click(Object sender, EventArgs e) in c:\Users\ccbt\Desktop\PPDM_TEST_HITVALIDATION_MODULE\SECONDARY_VALUES_STORAGE\SECONDARY_VALUES_STORAGE\test123.aspx.cs:208
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9553594
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Posted

Based on the error message the workbook is null when the finally block is executed. So what probably happens is that previously in your try or catch block you either don't set the workbook at all or you set it to null in some place.

Without seeing te whole code, it's impossible to say, but could it be that in case of an error for example when opening a workbook, you hit the catch block and when finally block is entered, the variable hasn't yet been set.

You should investigate the source of the problem but in order to handle such situations you should also add
C#
finally
{
   if (workbook != null) {
      workbook.Close(Missing.Value, Missing.Value, Missing.Value);
   }
   if (application != null) {
      application.Quit();
   }
}
 
Share this answer
 
Comments
Member 11819086 28-Sep-15 1:40am    
hi,thank you for response here i am giving the method as follows

private void GetExcel(string fileName)
{

// string filenamecmp = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\CMPDATAFILES\" + FileUpload1.FileName);

string str = string.Empty;
Application application = null;
Workbook workbook = null;
Worksheet xlWs = null;
Dictionary<string, string=""> dictionarySingleValue = new Dictionary<string, string="">();
try
{
application = (Application)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
if (application == null)
{
Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
}
else
{
application.Visible = false;
workbook = application.Workbooks.Open(fileName, 0, false, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// workbook = application.Workbooks.Open(fileName, 0, false, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

string str2 = System.IO.Path.GetFileName(this.fileuploader.FileName);
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(this.fileuploader.FileName);
for (int i = 1; i <= workbook.Worksheets.Count; i++)
{
if (!((Worksheet)workbook.Worksheets[i]).Name.StartsWith("Sheet"))
{
string str5;
string str6;
Range range;
int num8;
int num9;
xlWs = (Worksheet)workbook.Worksheets[i];
double mpMax = 0.0;
double mpMin = 0.0;
double sdMax = 0.0;
double sdMin = 0.0;
double dZPrime = 0.0;
if (xlWs == null)
{
Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
}
System.Data.DataTable dtData = new System.Data.DataTable();

dtData.Columns.Add("PlateId");
dtData.Columns.Add("WellPosition");
dtData.Columns.Add("DataType");
dtData.Columns.Add("Value");
dtData.Columns.Add("Replicates");
dtData.Columns.Add("Concentration");
dtData.Columns.Add(" Assay_Type");
/************************** Initializing the Values *****************************************************************/


string rep = ddlreplicates.SelectedItem.Value;
string concen = ddlconcentrate.SelectedItem.Value;
string assy_type = ddlassaytype.SelectedItem.Value;


DataView defaultView = ((System.Data.DataTable)this.ViewState["TemplateDetls"]).DefaultView;
defaultView.RowFilter = "TEMP_ID = '" + this.ddlTemplate.SelectedItem.Value + "'";
System.Data.DataTable table2 = defaultView.ToTable();
this.ViewState["dvdt"] = table2;
defaultView.RowFilter = "";
defaultView.RowFilter = "TEMP_ID = '" + this.ddlTemplate.SelectedItem.Value + "' AND START_POINT = END_POINT AND CALCU
Member 11819086 28-Sep-15 1:45am    
i have just changed the name and make it another file which is working properly.. even though it is not working i,e it gives same error..... what i can do?
Wendelius 29-Sep-15 14:01pm    
I'd suggest debugging the code line by line. For example if creating the application fails the workbook is still null so the code in your question would fail.

You didn't post the catch block but it's important that you handle the exceptions properly. That's the only way your program can tell what has gone wrong.
Hi,

As per your error, it is throwing because of null referenced, please debug your solution and then check whether WorkBook contains value or not. I guess it doesn't contain any value, it's return null;
 
Share this answer
 
Comments
Member 11819086 28-Sep-15 3:00am    
hi here i am attaching the method and marked as red line is the error where i got the error

private void GetExcel(string fileName)
{

// string filenamecmp = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\CMPDATAFILES\" + FileUpload1.FileName);
string str = string.Empty;
Application application = null;
Workbook workbook = null;
Worksheet xlWs = null;
Dictionary<string, string=""> dictionarySingleValue = new Dictionary<string, string="">();
try
{
application = (Application)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
if (application == null)
{
Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
}
else
{
application.Visible = false;
workbook = application.Workbooks.Open(fileName, 0, false, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// workbook = application.Workbooks.Open(fileName, 0, false, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

string str2 = System.IO.Path.GetFileName(this.fileuploader.FileName);
string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(this.fileuploader.FileName);
for (int i = 1; i <= workbook.Worksheets.Count; i++)
{
if (!((Worksheet)workbook.Worksheets[i]).Name.StartsWith("Sheet"))
{
string str5;
string str6;
Range range;
int num8;
int num9;
xlWs = (Worksheet)workbook.Worksheets[i];
double mpMax = 0.0;
double mpMin = 0.0;
double sdMax = 0.0;
double sdMin = 0.0;
double dZPrime = 0.0;
if (xlWs == null)
{
Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
}
System.Data.DataTable dtData = new System.Data.DataTable();

dtData.Columns.Add("PlateId");
dtData.Columns.Add("WellPosition");
dtData.Columns.Add("DataType");
dtData.Columns.Add("Value");
dtData.Columns.Add("Replicates");
dtData.Columns.Add("Concentration");
dtData.Columns.Add(" Assay_Type");
/************************** Initializing the Values *****************************************************************/
string rep = ddlreplicates.SelectedItem.Value;
string concen = ddlconcentrate.SelectedItem.Value;
string assy_type = ddlassaytype.SelectedItem.Value;


DataView defaultView = ((System.Data.DataTable)this.ViewState["TemplateDetls"]).DefaultView;
defaultView.RowFilter = "TEMP_ID = '" + this.ddlTemplate.SelectedItem.Value + "'";
System.Data.DataTable table2 = defaultView.ToTable();
this.ViewState["dvdt"] = table2;
defaultView.RowFilter = "";
defaultView.RowFilter = "TEMP_ID = '" + this.ddlTemplate.SelectedItem.Value + "' AND START_POINT = END_POINT AND CALCULATION = 'No'";
table2 = defaultView.ToTable();
List<string> list = new List<string>();
ArrayList list2 = new ArrayList();
dictionarySingleValue = new Dictionary<string, string="">();
int num7 = 0;
while (num7 < table2.Rows.Count)
{
str5 = Convert.ToString(table2.Rows[num7]["Start_Point"]);
str6 = Convert.ToString(table2.Rows[num7]["End_Point"]);
range = xlWs.get_Range(str5, str6);
if (range != null)
{
num8 = 1;
while (num8 <= range.Rows.Count)
{
num9 = 1;
while (num9 <= range.Columns.Count)
{
string str8 = ((string)Convert.ToString(((dynamic)range.Cells[num8, num9]).value2)).Split(new string[] { ":", ":" }, StringSplitOptions.None)[1].Trim();
if (string.Equals(Convert.ToString(table2.Rows[num7]["TEMP_DETAILS"]), "Date of Measurement", StringComparison.OrdinalIgnoreCase))
{
dictionarySingleValue.Add(Convert.ToString(table2.Rows[num7]["TEMP_DETAILS"]), str8.Substring(0, 10).Replace("-", "/"));
}
else
{
dictionarySingleValue.Add(Convert.ToString(table2.Rows[num7]["TEMP_DETAILS"]), str8);
}
num9++;
}
num8++;
}
}
num7++;
}
defaultView.RowFilter = "";
DataSet plateLayoutId = new DataSet();
if (this.ddlPlateLayout.Visible)
{
plateLayoutId = this.objHTSBO.GetPlateLayoutId(this.ddlPlateLayout.SelectedItem.Value);
}
else
{
plateLayoutId = this.objHTSBO.GetPlateLayoutId(fileNameWithoutExtension);
}
List

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900