0

i have this code :

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;

 namespace ConsoleApplication2
 {
     class Program
     {
         static void Main(string args)
         {
             string TheFormText;
             string TheFormName;
             string TheFormTag;
             Form activeForm = Form.ActiveForm;
             TheFormText = activeForm.ActiveMdiChild.Text.ToString();          
             TheFormName = activeForm.ActiveMdiChild.Name.ToString();
             try
             {
                 TheFormTag = activeForm.ActiveMdiChild.Tag.ToString();
             }
             catch
             {
                 TheFormTag = "<none>";
             }
         }
     }
 }

i try to run in but when i do i get this error An unhandled exception of type 'System.NullReferenceException' occurred in ConsoleApplication2.exe

in TheFormText = activeForm.ActiveMdiChild.Text.ToString();

can someone help me make it work ?

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
Xozu
  • 81
  • 1
  • 2
  • 10
  • `NullReferenceException` is a common situation for beginner programmers. The link provided should help you understand the problem. Then use the debugger to find what/where/when you have a variable that is `null`. – Soner Gönül Mar 05 '15 at 08:12
  • Form activeForm = Form.ActiveForm; Form is not instantianted . Try Form activeForm = New Form.ActiveForm; – CristiC777 Mar 05 '15 at 11:49
  • @CristiC777 - `Form.ActiveForm` is not a class, it is a static property. You cannot use it with `new`. `Form.ActiveForm` returns the currently active form. The OP code seems like it is a console application, so there probably aren't any forms at all. – Chris Dunaway Mar 05 '15 at 14:58

0 Answers0