-1

How to fix Null Reference Exception occurred between two classes ?
•I'm using Visual Studio 2012 on Windows 7.

i'm newbie with c# but i started working on my project two days ago.
And i'm facing this problem (Null Reference Exception occurred), all the day long trying to fix it,searched stackoverflow and google a lot but still can't find a solution.
i have simplified my error code.

Here is my code:

Form1

namespace test1  
{  
    using System;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
    using System.Windows.Forms;  

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            new Class1().Message();
        }

    }
}  

Class1

namespace test1  
{  
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  

    public class Class1
    {
        public static Form1 MainWindow;
        public void Message()
        {
            Mess("asdasd");
        }
        public void Mess(string msg)
        {
                Class1.MainWindow.globalchat.AppendText(msg);
        }
    }
}

The line which give the error : Class1.MainWindow.globalchat.AppendText(msg); Thanks in advance for every one gonna try to help.

user3725506
  • 155
  • 1
  • 1
  • 7

1 Answers1

0

Your MainWindow property not initalized.

If used your code, then put in constructor of Form1

public Form1()
{
    InitializeComponent();
    Class1.MainWindow = this;
}
Fabio
  • 31,528
  • 4
  • 33
  • 72