例外:对象引用未设置为对象的实例

时间:2012-09-13 11:15:53

标签: .net visual-c++ exception c++-cli

当我尝试执行以下代码时,我System.NullReferenceException Object reference not set to an instance of an object

String^ databasePath;
wchar_t* nativeDataPath;
try{
 if(folderBrowserDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
      {

         databasePath=folderBrowserDialog1->SelectedPath->ToString()+"\\DocData.db3"; 

      }
}
catch (System::IO::DirectoryNotFoundException^ ex){
    MessageBox::Show(ex->Message);

}

1 个答案:

答案 0 :(得分:1)

检查folderBrowserDialog1->SelectedPath的值是否为null

如果这是null,那么执行folderBrowserDialog1->SelectedPath->ToString()将抛出此异常。