-3

I have one main form and a static class when i access static class member it gives me nullreference error . Previously it was working fine donot know what happend . Any one can suggest what is wrong.

code snap:

namespace MyNamespace
{
    public partial class myForm : Form
    {
        public myForm()
        {
            InitializeComponent();
        }

        private void myForm_Load(object sender, EventArgs e)
        {
            My_Static_Data_Class.player_name="Demo Player"
        }
    } 

    public static class My_Static_Data_Class
    {
        public static string player_name = "";
    }
}

please help?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Albert
  • 11
  • 3
  • 2
    Please make more effort to format your code. The indentation is all over the place at the moment. The code you've provided won't give a NullReferenceException, either. Please show your full stack trace. – Jon Skeet Jul 09 '15 at 08:15
  • 1
    You are missing `;` behind `"Demo Player"`. – Dzienny Jul 09 '15 at 08:16
  • 2
    Provide compiling code that demonstrates the issue reproducibly. It sounds like nitpicking, but it's also important to follow [capitalization-](https://msdn.microsoft.com/en-us/library/vstudio/ms229043(v=vs.100).aspx)(f.e. uppercase first letter in `player_name`) and [naming-conventions](https://msdn.microsoft.com/en-us/library/ms229045(v=vs.110).aspx)(f.e. no underscores). – Tim Schmelter Jul 09 '15 at 08:24
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Stephen Kennedy Feb 18 '18 at 12:21

1 Answers1

0

You're either accessing the static class member before it was set to "Demo Player". For example, you are trying accessing My_Static_Data_Class.player_name in Program.cs code before it calls the main form from the Main[] method. Alternatively you're might be setting My_Static_Data_Class.player_name to null elsewhere in code and then accessing it.

Check all the references in code editor and follow it up. To do this, right click on My_Static_Data_Class.player_name in Visual Studio editor, and choose Find All References menu item.

Alex
  • 937
  • 3
  • 20
  • 44