question

Quantum7-1813 avatar image
0 Votes"
Quantum7-1813 asked ·

Visual C++ 2017 TextBox Error

I'm testing my compiling software and the tester is detecting an error with the following code:

     String^ in1;
     System::Windows::Forms::TextBox^ textBox1;
     textBox1->Text = in1;

Here is the error details:

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=QMB
StackTrace:
at QMB.Form1.button1_Click(Object sender, EventArgs e) in c:\users\quant\desktop\quantum metaphysics bridge\qmb\form1.h:line 266
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Main(String[] args) in c:\users\quant\desktop\quantum metaphysics bridge\qmb\form1.cpp:line 11

What should I do?

c++
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cheong00 avatar image
1 Vote"
cheong00 answered ·

You have to instantiate the object instance before using it.

      String^ in1;
      System::Windows::Forms::TextBox^ textBox1 = gcnew System::Windows::Forms::TextBox;
      textBox1->Text = in1;
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered ·

Maybe you should write one line only:

String ^in1 = textBox1->Text;

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JeanineZhang-MSFT avatar image
0 Votes"
JeanineZhang-MSFT answered ·

Hi,

I suggest you could try the following code:

 String^ in1;
  textBox1->Text = in1;

When you add controls to the form, vs will automatically add the following statements:

 private: System::Windows::Forms::TextBox^  textBox1;

 this->textBox1 = (gcnew System::Windows::Forms::TextBox());

So you didn’t need to repeat by yourself :

 System::Windows::Forms::TextBox^ textBox1;

Best Regards,

Jeanine



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.