2

I had this working yesterday but I must of changed something now ListActiveLogins.ActiveLogins is null, what did i do?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        new ListLogin(2);
        Console.WriteLine(ListLogin.LoginList.Length);
        Console.WriteLine(ListLogin.loginC);
        new ListActiveLogins(2);
        Console.WriteLine(ListActiveLogins.ActiveLogins.Length);
    }
}

public class ListLogin
{
    public static int loginC;
    public static string[,] LoginList;
    public ListLogin(int loginCount)
    {
        LoginList = new string[loginCount, 3];
        loginC = loginCount;
    }
    public int LoginCount
    {
        get { return loginC; }
    }
    public string this[int row, int col]
    {
        get
        {
            return LoginList[row, col];
        }
        set
        {
            LoginList[row, col] = value;
        }
    }
}

public class ListActiveLogins
{
    public static Process[] ActiveLogins;
    public ListActiveLogins(int loginCount)
    {
        Process[] ActiveLogins = new Process[loginCount];
    }
    public Process this[int i]
    {
        get
        {
            return ActiveLogins[i];
        }
        set
        {
            ActiveLogins[i] = value;
        }
    }
}
Tommy
  • 23
  • 4
  • 3
    This is a very poor design. What is it supposed to do? – SLaks Jun 30 '11 at 22:46
  • I only started a week ago, and yeah i messed up the copy i put here too lol, public ListActiveLogins(int loginCount) is static in my real code dunno how i managed cut it out. – Tommy Jun 30 '11 at 23:05
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 15 '14 at 02:55

3 Answers3

3

You're making a separate local variable in the constructor.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

The problem is that you declared a local variable in your ListActiveLogins constructor.

This should fix it.

public class ListActiveLogins
{
    public static Process[] ActiveLogins;
    public ListActiveLogins(int loginCount)
    {
        ActiveLogins = new Process[loginCount];
    }
agent-j
  • 27,335
  • 5
  • 52
  • 79
  • bah how did i add the extra Process[], its obvious now u pointed it out, everything works again thx – Tommy Jun 30 '11 at 22:55
  • I'm glad we could help. Here at stack overflow, a nice way to show gratitude is to accept the most helpful answer, (and upvote helpful answers when you can). It also lets other helpful people know that your problem is solved. Cheers! – agent-j Jun 30 '11 at 22:59
1
Process[] ActiveLogins = new Process[loginCount];

is being re-declared as a local variable. Change it to:

ActiveLogins = new Process[loginCount];
Justin M. Keyes
  • 6,679
  • 3
  • 33
  • 60