Object reference not set to an instance of an object [duplicate]
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 开发者_JAVA百科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;
}
}
}
You're making a separate local variable in the constructor.
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];
}
Process[] ActiveLogins = new Process[loginCount];
is being re-declared as a local variable. Change it to:
ActiveLogins = new Process[loginCount];
精彩评论