Aan
Aan

Reputation: 12890

FileDialog exception

I am trying to use OpenFileDialog as in the code below, but the compiler shows the exception System::NullReferenceException, once I put the the code between try and catch block, no exception is thrown, but now dialog is shown!?

try 
{ 
    if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
    { 
         this->textBox18->Text=openFileDialog1->FileName->ToString();  

    } 
} 
catch(System::NullReferenceException^ e){ e->Message;}

Upvotes: 0

Views: 228

Answers (1)

Nasreddine
Nasreddine

Reputation: 37838

You must create an instance of the OpenFileDialog class before using it. In this code segment:

private: System::Windows::Forms::OpenFileDialog^ openFileDialog1; 

You are just declaring a variable of the type OpenFileDialog (initialized with null) but you need to instantiate it before being able to use it with

OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

Upvotes: 2

Related Questions