Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Object reference not set to an instance of an object.Hello, does anyone face this error before?, I got this error message. The following are the stack trace. Does anyone can help

.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 51:         foreach (Expert _expert in _expertDAL.SelectAllExperts())
Line 52:         {
Line 53:             organizationName = _organizationDAL.SelectOrganizationByID  (_expert.OrganizationID).OrganizationName;
Line 54:             _newExpert = new ExpertReport();
Line 55:             _newExpert.ExpertName = _expert.ExpertName;


Source File: d:\backup files sqlserver\material management new latest1\Materials Management System Backup\MMSUI\Reports\ExpertReportPage.aspx.cs    Line: 53

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   Reports_ExpertReportPage.PopulateExperts() in d:\backup files sqlserver\material management new latest1\Materials Management System Backup\MMSUI\Reports\ExpertReportPage.aspx.cs:53
   Reports_ExpertReportPage.ConfigureCrystalReports() in d:\backup files sqlserver\material management new latest1\Materials Management System Backup\MMSUI\Reports\ExpertReportPage.aspx.cs:38
   Reports_ExpertReportPage.Page_Init(Object sender, EventArgs e) in d:\backup files sqlserver\material management new latest1\Materials Management System Backup\MMSUI\Reports\ExpertReportPage.aspx.cs:34
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnInit(EventArgs e) +92
   System.Web.UI.Page.OnInit(EventArgs e) +12
   System.Web.UI.Control.InitRecursive(Control namingContainer) +134
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +489
Posted
Updated 29-Sep-13 23:39pm
v3

Hey there,

Have you tried debugging the code and see at which line does this exception occur?

From your code, I think it could be this line:
C#
organizationName = _organizationDAL.SelectOrganizationByID


_organizationDAL could be null, which can raise this exception.
Have you properly created the object, _organizationDAL?

Take a look and do let me know.

Hope it helps.

Azee...
 
Share this answer
 
Comments
mahitem 30-Sep-13 7:36am    
ya am sure i created _organizationDAL object
hi
you can check whether your table is configured in dataset or not which your are used.

when i have got this error that time i am not configured table in dataset.
Hope this can help you.
 
Share this answer
 
Comments
mahitem 11-Oct-13 8:15am    
I don't understand what dataset mean?
Your DAL's SelectOrganizationByID function returning null and you are trying to get the property OrganizationName of a null object, which is giving this error.

Before accessing the object's property, check whether the object is null.
 
Share this answer
 
Comments
mahitem 7-Oct-13 9:10am    
This is My OrganizationDAL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MMSEntities;

namespace MMSDataAccessLayer
{
public class OrganizationDAL
{
private string _connectionString = string.Empty;

public OrganizationDAL(string connectionString)
{
this._connectionString = connectionString;
}

public List<organization> SelectAllOrganizations()
{
List<organization> _organizationsList = new List<organization>();
using (SqlConnection _connection = new SqlConnection(this._connectionString))
{
_connection.Open();
using (SqlCommand _command = _connection.CreateCommand())
{
_command.CommandType = CommandType.StoredProcedure;
_command.CommandText = "spSelectAllOrganizations";
SqlDataReader _reader = _command.ExecuteReader();
Organization _organization = null;
while (_reader.Read())
{
_organization = new Organization();
_organization.OrganizationID = int.Parse(_reader["autoOrganizationID"].ToString());
_organization.OrganizationName = _reader["organizationName"].ToString();
_organization.Authority = _reader["authority"].ToString();
_organization.TypeOfOrganization = _reader["typeOfOrganization"].ToString();
_organization.Fax = _reader["fax"].ToString();
_organization.OrganizationSector = _reader["organizationSector"].ToString();
_organization.PhoneNumberTwo = _reader["phoneNumber2"].ToString();
_organization.PhoneNumber = _reader["phoneNumber"].ToString();

if (!string.IsNullOrEmpty(_reader["woredaID"].ToString()))
{
_organization.WoredaID = int.Parse(_reader["woredaID"].ToString());
}
else
{
_organization.WoredaID = 0;
}
if (!string.IsNullOrEmpty(_reader["zoneID"].ToString()))
{
_organization.ZoneID = int.Parse(_reader["zoneID"].ToString());
}
else
{
_organization.ZoneID = 0;
}
_organizationsList.Add(_organization);
}
}
}
return _organizationsList;
}

public void InsertOrganization(Organization organization)
{
using (SqlConnection _connection = new SqlConnection(this._connectionString))
{
_connection.Open();
using (SqlCommand command = _connection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "spInsertOrganization";
command.Parameters.Add(new SqlParameter("organizationName", organization.OrganizationName));
command.Parameters.Add(new SqlParameter("authority", organization.Authority));
command.Parameters.Add(new SqlParameter("typeOfOrganization", organization.TypeOfOrganization ));
command.Parameters.Add(new SqlParameter("phoneNumber", organization.PhoneNumber));
command.Parameters.Add(new SqlParameter("phoneNumber2", organization.PhoneNumberTwo));
command.Parameters.Add(new SqlParameter("fax", organization .Fax));
command.Parameters.Add(n

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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