views:

878

answers:

1
namespace X{  public static class URLs
{
    public static TabController tabIdLookUp = new TabController();
    public static string DASHBOARD_AUDIT_PAGE = tabIdLookUp.GetTabByName("View My Safety", 2).TabID.ToString();
    public static string URL_GENERATE_WITH_MID(String TabName, int PortalId){        {
        return tabIdLookUp.GetTabByName(TabName, PortalId).TabID.ToString();
    }
}}

... in my user control i do this:

Response.Redirect("/" + X.URLs.URL_GENERATE_WITH_MID("test", 1)); // this causes the error

the error is: The type initializer for 'X.URLs' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object. at X.URLs..cctor()

can't debug because it works on my local box, but throws that error on the server.

any ideas?

P.S. the problem ended up being a trivial NUllReferenceException - GetTabByName() was returing NULL

A: 

Rather than having your initializer for "DASHBOARD AUDIT PAGE" refer to tabIdLookUp directly, why not instead initialize both of those variables in a static constructor and see if that fixes the error?

namespace X{  public static class URLs
{
    public static TabController tabIdLookUp;
    public static string DASHBOARD_AUDIT_PAGE;
    public static string URL_GENERATE_WITH_MID(String TabName, int PortalId){        {
        return tabIdLookUp.GetTabByName(TabName, PortalId).TabID.ToString();
    }

    static URLs() {
        tabIdLookUp = new TabController();
        DASHBOARD_AUDIT_PAGE = tabIdLookUp.GetTabByName("View My Safety", 2).TabID.ToString();
    }
}}

Another problem you could be having is if GetTabByName is returning a NULL reference, you're not protecting against that and just referencing the .TabID property. You should probably ensure that you're getting back a valid reference before referring to the property.

scwagner