locked
Localization - Object reference not set to an instance of an object. RRS feed

  • Question

  • User1863994004 posted

    Hi. I want to access specific resource when specific number come from my db (when accessed it). So i want to show a particular resource from my file, but i guess i need to check for null values, but not sure how to do it. I got the following mistake for this line:

    Label_Result_ANeB.Text = GetLocalResourceObject("Label_Result_ANeBResource2.Text.Text").ToString();

    "Object reference not set to an instance of an object."

    Resources are ok. How to check for nulls or sth?

    Wednesday, July 25, 2012 11:23 AM

Answers

  • User97853568 posted

    This error tells you that GetLocalResourceObject("Label_Result_ANeBResource2.Text.Text") return null ,it could not find the resource key,

    make sure that "Label_Result_ANeBResource2.Text.Text" exists in your resource file, make sure you have the correct

    resource file YourPageName.aspx.resx or YourPageName.aspx.en-US.resx, if do not guarantee the key will be there always,

    you have to check for null as explained in my first reply.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2012 6:44 AM

All replies

  • User97853568 posted
            object value = GetLocalResourceObject("Label_Result_ANeBResource2.Text.Text");
    
            if (value != null)
            {
                Label_Result_ANeB.Text = GetLocalResourceObject("Label_Result_ANeBResource2.Text.Text").ToString();
            }

    Wednesday, July 25, 2012 8:09 PM
  • User1863994004 posted

    Ok but i have case block, can i do it like this - i mean i tried and asking am i missing somethinh?

     switch (error_number)
                    {                              
                        case 1:       
    
                            object value = GetLocalResourceObject("Label_Result_ANeBResource4.Text.Text");
    
                            Label_Result_ANeB.Visible = true;
                            Label_Result_ANeB.ForeColor = System.Drawing.Color.Green;
                            if (Convert.ToString(Thread.CurrentThread.CurrentCulture) == "bg-BG")
                               if( value != null )
                                   Label_Result_ANeB.Text = GetLocalResourceObject("Label_Result_ANeBResource4.Text.Text").ToString();
                            else
                               if (value != null )
                                    Label_Result_ANeB.Text = GetLocalResourceObject("Label_Result_ANeBResource4.Text.Text").ToString();
                            break;
    
                        case -2:
                            value = GetLocalResourceObject("Label_Result_ANeBResource2.Text.Text");
                            Label_Result_ANeB.Visible = true;
                            Label_Result_ANeB.ForeColor = System.Drawing.Color.Red;
    
                            if (Convert.ToString(Thread.CurrentThread.CurrentCulture) == "bg-BG")
                                if (value != null )
                                    Label_Result_ANeB.Text = "Книгатa вече СЪЩЕСТВУВА в базата!";
                            else        
                                if( value != null )
                                    Label_Result_ANeB.Text = GetLocalResourceObject("Label_Result_ANeBResource2.Text.Text").ToString();
                                //Label_Result_ANeB.Text = "This book EXISTS in the database!";
    
                            break;
    
                        default:                                
                            value = GetLocalResourceObject("Label_Result_ANeBResource3.Text.Text");  
                            Label_Result_ANeB.Visible = true;
                            Label_Result_ANeB.ForeColor = System.Drawing.Color.Yellow;
                               
                            if (Convert.ToString(Thread.CurrentThread.CurrentCulture) == "bg-BG")
                                if (value != null)
                                    Label_Result_ANeB.Text = "Грешка!";
                            else
                                if ( value != null )
                                    Label_Result_ANeB.Text = GetLocalResourceObject("Label_Result_ANeBResource3.Text.Text").ToString();       
                                //Label_Result_ANeB.Text = "Error!";
    
                            break;
                    }            
    I'm not sure if this is the rigth solution?

    Thursday, July 26, 2012 3:13 AM
  • User97853568 posted

    can you provide more explanation and be more specific of what your trying to do, so we can help you. 

    Thursday, July 26, 2012 5:30 AM
  • User1863994004 posted

    Ok. I have Stored procedure that inserts rows in my db. This SP return a number( error_number variable) that tells me what kind of mistake happened when inserting new book. So i create several resources in my resx fil that explain the error. For example : "This book has been successfully added!" or "This book is already in the database!" . After knowing the error_number i want the text of Label: Label_Result_ANeB to become one of the resources that i made. But when debug and go to this line for example:

    Label_Result_ANeB.Text = GetLocalResourceObject("Label_Result_ANeBResource2.Text.Text").ToString();

    it says: Object reference not set to an instance of an object.

    So this is my problem. I will give you now my first code so you can understan what i try to do in first place:

    Hope you understand me.

    Thursday, July 26, 2012 5:48 AM
  • User97853568 posted

    This error tells you that GetLocalResourceObject("Label_Result_ANeBResource2.Text.Text") return null ,it could not find the resource key,

    make sure that "Label_Result_ANeBResource2.Text.Text" exists in your resource file, make sure you have the correct

    resource file YourPageName.aspx.resx or YourPageName.aspx.en-US.resx, if do not guarantee the key will be there always,

    you have to check for null as explained in my first reply.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2012 6:44 AM
  • User1863994004 posted

    You are right. It seems i add some extra ".Text" so now everything is fine. 10x all everyone. I will post the right way and close the post :):

    LabelID.Text = GetLocalResourceObject("LabelResource.Text").ToString();



    Thursday, July 26, 2012 1:56 PM