对象数组中的 NullReferenceException
NullReferenceException in array of objects
我正在用 c# 开发一个程序,其中我有一个 class 的对象数组。我在 class 中有一个构造函数,它应该为附加的字符串赋值 "" 以确保它们为空,以便可以轻松传递信息。然后我像这样创建了数组:
participants[8][8] grid = new participants[8][];
但是我被抛出 NullReferenceExcetption 错误。这是我的代码供您参考。
using System;
namespace TreasurehuntCsharp
{
class square
{
public string strX = "";
public int y = 0;
public int x = 0;
}
class participants
{
public string name = "";
public string contact = "";
public participants()
{
name = "";
contact = "";
}
}
class Program
{
//Method for user to choose a square
static void Input(square Coord)
{
//Variables
bool correct = false;
//Inputs
Console.WriteLine("Please enter the coordinates of the square you would like to select: \r\n");
do
{
//X coordinate
Console.WriteLine("X: ");
Coord.strX = Console.ReadLine().ToLower();
//Convert letter to array coordinate
switch (Coord.strX)
{
case "a":
Coord.x = 0;
correct = true;
break;
case "b":
Coord.x = 1;
correct = true;
break;
case "c":
Coord.x = 2;
correct = true;
break;
case "d":
Coord.x = 3;
correct = true;
break;
case "e":
Coord.x = 4;
correct = true;
break;
case "f":
Coord.x = 5;
correct = true;
break;
case "g":
Coord.x = 6;
correct = true;
break;
case "h":
Coord.x = 7;
correct = true;
break;
default:
Console.WriteLine("Please enter a letter from A to H");
correct = false;
break;
}
} while (correct != true);
correct = false;
do
{
//Y coordinate
Console.WriteLine("Y: ");
Coord.y = Convert.ToInt32(Console.ReadLine());
if (Coord.y >= 1 && Coord.y <= 7)
{
correct = true;
}
else
{
Console.WriteLine("Please input an integer value from 1 to 7.");
correct = false;
}
} while (correct != true);
}
static void ParticipantDetails(participants User)
{
Console.WriteLine("Please input your name and Contact number: ");
//User name input
Console.WriteLine("Name: ");
User.name = Console.ReadLine();
//User contact number input
Console.WriteLine("Number: ");
User.contact = Console.ReadLine();
}
static void Main(string[] args)
{
//Objects
square Coord = new square();
participants User = new participants();
//Initialise 2D array
participants[][] grid = new participants[8][];
//Variables
bool correct = false;
do
{
//Methods
Input(Coord);
ParticipantDetails(User);
//Input data to array
if (grid[Coord.x][Coord.y].name == "" && grid[Coord.x][Coord.y].contact == "")
{
grid[Coord.x][Coord.y].name = User.name;
grid[Coord.x][Coord.y].contact = User.contact;
Console.WriteLine(grid[Coord.x][Coord.y]);
correct = true;
}
else
{
Console.WriteLine("That square is already filled. Please try again.");
correct = false;
}
} while (correct == false);
}
}
您没有初始化数组的所有维度。
//Initialise 2D jagged array
participants[][] grid = new participants[8][];
// You can do that in a for loop
grid[0] = new participants[8];
grid[1] = new participants[8];
grid[2] = new participants[8];
...
grid[7] = new participants[8];
// For 循环等价物
for (int i = 0; i < grid.Length; i++)
{
grid[i] = new participant[8];
}
对你的情况更好的方法(因为所有的子数组有相同数量的元素),将使用 multidimensional-arrays
participants[,] grid = new participants[8,8];
// and access like this
grid[Coord.x, Coord.y]
您创建了一个数组数组。
participants[][] grid = new participants[8][];
这是一个有8个条目的数组。每个条目都应该是一组参与者,但尚未创建。
你需要做的是创建8个数组并将它们分配给网格数组的元素。
participant[][] grid = new participant[8][];
for (int i = 0; i < grid.Length; i++)
grid[i] = new participant[8];
这里我假设网格数组的每个元素也将包含 8 个元素,但这取决于您。
我正在用 c# 开发一个程序,其中我有一个 class 的对象数组。我在 class 中有一个构造函数,它应该为附加的字符串赋值 "" 以确保它们为空,以便可以轻松传递信息。然后我像这样创建了数组:
participants[8][8] grid = new participants[8][];
但是我被抛出 NullReferenceExcetption 错误。这是我的代码供您参考。
using System;
namespace TreasurehuntCsharp
{
class square
{
public string strX = "";
public int y = 0;
public int x = 0;
}
class participants
{
public string name = "";
public string contact = "";
public participants()
{
name = "";
contact = "";
}
}
class Program
{
//Method for user to choose a square
static void Input(square Coord)
{
//Variables
bool correct = false;
//Inputs
Console.WriteLine("Please enter the coordinates of the square you would like to select: \r\n");
do
{
//X coordinate
Console.WriteLine("X: ");
Coord.strX = Console.ReadLine().ToLower();
//Convert letter to array coordinate
switch (Coord.strX)
{
case "a":
Coord.x = 0;
correct = true;
break;
case "b":
Coord.x = 1;
correct = true;
break;
case "c":
Coord.x = 2;
correct = true;
break;
case "d":
Coord.x = 3;
correct = true;
break;
case "e":
Coord.x = 4;
correct = true;
break;
case "f":
Coord.x = 5;
correct = true;
break;
case "g":
Coord.x = 6;
correct = true;
break;
case "h":
Coord.x = 7;
correct = true;
break;
default:
Console.WriteLine("Please enter a letter from A to H");
correct = false;
break;
}
} while (correct != true);
correct = false;
do
{
//Y coordinate
Console.WriteLine("Y: ");
Coord.y = Convert.ToInt32(Console.ReadLine());
if (Coord.y >= 1 && Coord.y <= 7)
{
correct = true;
}
else
{
Console.WriteLine("Please input an integer value from 1 to 7.");
correct = false;
}
} while (correct != true);
}
static void ParticipantDetails(participants User)
{
Console.WriteLine("Please input your name and Contact number: ");
//User name input
Console.WriteLine("Name: ");
User.name = Console.ReadLine();
//User contact number input
Console.WriteLine("Number: ");
User.contact = Console.ReadLine();
}
static void Main(string[] args)
{
//Objects
square Coord = new square();
participants User = new participants();
//Initialise 2D array
participants[][] grid = new participants[8][];
//Variables
bool correct = false;
do
{
//Methods
Input(Coord);
ParticipantDetails(User);
//Input data to array
if (grid[Coord.x][Coord.y].name == "" && grid[Coord.x][Coord.y].contact == "")
{
grid[Coord.x][Coord.y].name = User.name;
grid[Coord.x][Coord.y].contact = User.contact;
Console.WriteLine(grid[Coord.x][Coord.y]);
correct = true;
}
else
{
Console.WriteLine("That square is already filled. Please try again.");
correct = false;
}
} while (correct == false);
}
}
您没有初始化数组的所有维度。
//Initialise 2D jagged array
participants[][] grid = new participants[8][];
// You can do that in a for loop
grid[0] = new participants[8];
grid[1] = new participants[8];
grid[2] = new participants[8];
...
grid[7] = new participants[8];
// For 循环等价物
for (int i = 0; i < grid.Length; i++)
{
grid[i] = new participant[8];
}
对你的情况更好的方法(因为所有的子数组有相同数量的元素),将使用 multidimensional-arrays
participants[,] grid = new participants[8,8];
// and access like this
grid[Coord.x, Coord.y]
您创建了一个数组数组。
participants[][] grid = new participants[8][];
这是一个有8个条目的数组。每个条目都应该是一组参与者,但尚未创建。
你需要做的是创建8个数组并将它们分配给网格数组的元素。
participant[][] grid = new participant[8][];
for (int i = 0; i < grid.Length; i++)
grid[i] = new participant[8];
这里我假设网格数组的每个元素也将包含 8 个元素,但这取决于您。