-1

I have a Homepage which has 4 combobox named c_LOB, c_Projectname, c_countryname, c_releasename.

Now i'm trying to change the tooltip on the basis of enabled property of these combo boxes. Following is the code written in Visual Studio 2010 using C# and asp.net:

namespace GPTRGT
{
public partial class Homepage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserIdentification();
    }
    public void UserIdentification()
    {
        c_LOB.Items.Clear();
        c_LOB.Items.Add("RUSA");
        c_LOB.Items.Add("RETAIL");
        c_LOB.Items.Add("CARDS");
        if (c_CountryName.Enabled == false)
        {
            c_CountryName.ToolTip = "Please select your LOB first.";
        }
        else
        {
            c_CountryName.ToolTip = "Please select your Country name.";
        }

        if (c_ProjName.Enabled == false && c_CountryName.Enabled == false)
        {
            c_ProjName.ToolTip = "Please select you LOB and Country name first.";
        }
        else if (c_ProjName.Enabled == false)
        {
            c_ProjName.ToolTip = "Please select your Country name first.";
        }
        else
        {
            c_ProjName.ToolTip = "Please select your Project name.";
        }

        if (c_ProjName.Enabled == false && c_CountryName.Enabled == false && c_ProjName.Enabled == false)
        {
            c_ReleaseName.ToolTip = "Please select your LOB, Country name & Project name first";
        }
        else if (c_CountryName.Enabled == false && c_ProjName.Enabled == false)
        {
            c_ReleaseName.ToolTip = "Please select your Country name & Project name first";
        }
        else if (c_ProjName.Enabled == false)
        {
            c_ReleaseName.ToolTip = "Please select your Project name first";
        }
        else
        {
            c_ProjName.ToolTip = "Please select your Release name.";
        }
    }
}

}

This codes works fine. No issues. Now what i want to do is, i've created a seperate class named validation, and in that i've created this useridentification() method. And then i'm trying to call this method from homepage class. Have a look at the code.

namespace GPTRGT
{
public partial class Homepage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
        validation val = new validation();
        val.UserIdentification();
    }

}
class validation : Homepage
{
    public void UserIdentification()
    {
        c_LOB.Items.Clear();
        c_LOB.Items.Add("RUSA");
        c_LOB.Items.Add("RETAIL");
        c_LOB.Items.Add("CARDS");
        if (c_CountryName.Enabled == false)
        {
            c_CountryName.ToolTip = "Please select your LOB first.";
        }
        else
        {
            c_CountryName.ToolTip = "Please select your Country name.";
        }

        if (c_ProjName.Enabled == false && c_CountryName.Enabled == false)
        {
            c_ProjName.ToolTip = "Please select you LOB and Country name first.";
        }
        else if (c_ProjName.Enabled == false)
        {
            c_ProjName.ToolTip = "Please select your Country name first.";
        }
        else
        {
            c_ProjName.ToolTip = "Please select your Project name.";
        }

        if (c_ProjName.Enabled == false && c_CountryName.Enabled == false && c_ProjName.Enabled == false)
        {
            c_ReleaseName.ToolTip = "Please select your LOB, Country name & Project name first";
        }
        else if (c_CountryName.Enabled == false && c_ProjName.Enabled == false)
        {
            c_ReleaseName.ToolTip = "Please select your Country name & Project name first";
        }
        else if (c_ProjName.Enabled == false)
        {
            c_ReleaseName.ToolTip = "Please select your Project name first";
        }
        else
        {
            c_ProjName.ToolTip = "Please select your Release name.";
        }
    }
}
}

Now when i run it, it gives me the following error in internet explorer page:

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 21:         public void UserIdentification()
Line 22:         {
Line 23:             c_LOB.Items.Clear();
Line 24:             c_LOB.Items.Add("RUSA");
Line 25:             c_LOB.Items.Add("RETAIL");


Source File: C:\Documents and Settings\rs63386\my documents\visual studio 2010\Projects\GPTRGT\GPTRGT\Homepage.aspx.cs    Line: 23 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   GPTRGT.validation.UserIdentification() in C:\Documents and Settings\rs63386\my documents\visual studio 2010\Projects\GPTRGT\GPTRGT\Homepage.aspx.cs:23
   GPTRGT.Homepage.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\rs63386\my documents\visual studio 2010\Projects\GPTRGT\GPTRGT\Homepage.aspx.cs:15
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
user2992534
  • 7
  • 1
  • 1
  • 4

1 Answers1

0

Use defensive programming, like this:

public void UserIdentification()
{
    // Verify that c_LOB exists before we try to use it
    if(c_LOB != null)
    {
        c_LOB.Items.Clear();
        c_LOB.Items.Add("RUSA");
        c_LOB.Items.Add("RETAIL");
        c_LOB.Items.Add("CARDS");
    }
    ...
}

Generally when you get the object not set to an instance of an object error, you should put a break point on the line reported in the error message and then hover over the objects to see which one is null and then put the if condition to make sure you do not access objects that might be null. You should do this with all reference type parameters to your methods as well. Don't trust anything without verifying it.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • I've tried to put breakpoints on the line which was reporting error inorder to debug the reason, or to see which value is getting null. But the problem is even the breakpoint doesn't work. Program just executes and shows the error on IE. When i go back to see what issue there might be on the line breakpoint is placed, i don't see any null values passed or anything like that which might suggest that there is a problem. I even found that Breakpoint was never called. – user2992534 Nov 15 '13 at 11:09
  • @user2992534 - are you running the site in Visual Studio in `Debug` or `Release` mode? – Karl Anderson Nov 15 '13 at 12:48
  • I'm running in Debug Mode – user2992534 Nov 19 '13 at 17:53