-3

I'm trying to access the value of resulterr in button clickevent in order to display it in the messagebox. I'm not getting the value inside the button click event despite setting resulterr list as public. I'm getting exception as object reference not set to an instance of an object. Please rectify the error.

   public List<ErrorMsgTemplate> resulterr;
   public PPFCWCFServiceClient ppfcObj = new PPFCWCFServiceClient();

    public Admin()
        : base("Administration")
    {
        SplashScreenManager.ShowDefaultWaitForm();
        InitializeComponent();

        GetSource();
        ErrorMsg();
        FillDivision();

        cmbDivision.SelectedIndex = -1;

        SplashScreenManager.CloseDefaultWaitForm();


    }

    public void ErrorMsg()
    {
        List<ErrorMsgTemplate> resulterr = ppfcObj.getErrorMsgTemplate("ADMIN");            
    }




    private void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (cmbDivision.SelectedIndex == -1) //naray added
            {
                MessageBox.Show(resulterr[0].ERROR_MESSAGE_TEMPLATE.ToString() + "," + resulterr[0].SCREEN_NM.ToString(), "PPFC");
            }
Naraykln
  • 17
  • 7
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – stakx - no longer contributing Jul 16 '15 at 06:40
  • 2
    Besides this being a duplicate question, the following statement of yours bothers me: _"Please rectify the error."_ No! This is *your* task. All we do here is to help you understand the problem and how to fix it, but people on Stack Overflow are not here to write your code for you. When your question is answered, you should not just have better working code; you should understand *why* it works. – stakx - no longer contributing Jul 16 '15 at 06:42
  • I debugged and found that resulterr values becomes null once it exits out of the ErrorMsg(). I need to hit the database only once but I've to use the resulterr at several places in a page wherever MessageBox is present. – Naraykln Jul 16 '15 at 06:48

2 Answers2

0

I found the issue. I've declared global variable as

public List<ErrorMsgTemplate> resulterr;

and reinstantiated again in ErrorMsg():

public void ErrorMsg()
{
    List<ErrorMsgTemplate> resulterr = ppfcObj.getErrorMsgTemplate("ADMIN");            
}

Instead, used:

public void ErrorMsg()
{
    resulterr = ppfcObj.getErrorMsgTemplate("ADMIN");            
}

Now I'm able to access resulterr in btnsave event.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Naraykln
  • 17
  • 7
0

That error comes when your variable value get blank or a space .you can track by null or space to prevent this error