none
what does object reference not set to an instance of an object mean? RRS feed

  • 問題

  • what is wrong with my code?

    i want it to comes up a New from for editing Car Detail.

    the message comes up and says object reference not set to an instance of an object



            private void btnAdd_Click(object sender, EventArgs e)
            {
                try
                {

                    string lcReply = new InputBox(clsCar.FACTORY_PROMPT).Answer;
                    if (!string.IsNullOrEmpty(lcReply)) // not cancelled?
                    {
                        clsCar lcCar = clsCar.NewCar(lcReply[0]);
                        if (lcCar != null)
                        {
                            if (txtBrandName.Enabled)
                            {
                                txtBrandName.Enabled = false;
                            }
                            lcCar.BrandName = _Brand.BrandName;
                            lcCar.EditDetails();
                            if (!string.IsNullOrEmpty(lcCar.Name)) // not cancelled?
                            {
                                refreshFormFromDB(_Brand.BrandName);
                                frmMain.Instance.UpdateDisplay();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    2013年6月27日 上午 02:59

所有回覆

  • 下中斷點進行偵錯,看程式執行到哪一行出現Exception 跳到 catch,就可抓出錯誤
    2013年6月27日 上午 03:06
  • The [object reference not set to an instance of an object] means that you use an object before actually create it.
    2013年6月27日 上午 04:04
  • 首先,这里是中文区域,请直接用中文提问即可。

    此外,请删除Try……Catch语句,直接保留这些:

     string lcReply = new InputBox(clsCar.FACTORY_PROMPT).Answer;
                     if (!string.IsNullOrEmpty(lcReply))
    // not cancelled?
                     {
                         clsCar lcCar = clsCar.NewCar(lcReply[0]);
                         if (lcCar != null)
                         {
                             if (txtBrandName.Enabled)
                             {
                                 txtBrandName.Enabled = false;
                             }
                             lcCar.BrandName = _Brand.BrandName;
                             lcCar.EditDetails();
                             if (!string.IsNullOrEmpty(lcCar.Name))
    // not cancelled?
                             {
                                 refreshFormFromDB(_Brand.BrandName);
                                 frmMain.Instance.UpdateDisplay();
                             }
                         }
                     }
    F5断点调试看看情况如何?哪句抛出异常了?

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月27日 上午 04:59