目前正在建立一个数据库'基于2D数组的程序,我目前正在尝试搜索数组以在接受输入之前搜索重复项。我尝试过两种不同的方法,但都会导致同样的错误:
未处理的类型' System.NullReferenceException'发生在WindowsFormsApplication5.exe
中附加信息:未将对象引用设置为对象的实例。
1)
stringToCheck = playerIgNameBox.Text;
foreach (string x in playerArray)
{
if (x.Contains(stringToCheck))
{
MessageBox.Show("Duplicate Player!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
还
2)。
for (int i = 0; i < playerArray.GetUpperBound(0); i++ )
{
for (int j = 0; j < playerArray.GetUpperBound(1); j++)
{
if (playerArray[i,j].Contains(playerFirstNameBox.Text) && (playerArray[i,j].Contains(playerIgNameBox.Text) && playerArray[i,j].Contains(playerSecondNameBox.Text)))
{
MessageBox.Show("Player Duplicate!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); //Validation failed, display error message//
}
}
}
完整代码:
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;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
positionLabel.Text = "Current position:" + (currentIndex + 1).ToString() + "/20";
}
string[,] playerArray = new string[20, 8]; // Initial player array is declared, this is where the players are stored//
int currentIndexPlayer = 0;
int currentIndex = 0;
string stringToCheck;
private void addNewPlayerBtn_Click(object sender, EventArgs e)
{
if (addNewPlayerBtn.Text == "Add New Player")
{
playerFirstNameBox.ReadOnly = false; //Unlock all fields for input//
playerIgNameBox.ReadOnly = false;
playerSecondNameBox.ReadOnly = false;
contactStreetBox.ReadOnly = false;
contactTownBox.ReadOnly = false;
contactPostcodeBox.ReadOnly = false;
contactEmailBox.ReadOnly = false;
contactTelephoneBox.ReadOnly = false;
addNewPlayerBtn.Text = "Confirm";
}
else if (addNewPlayerBtn.Text == "Confirm")
{
if ((playerFirstNameBox.Text != "") && (playerIgNameBox.Text != "") && (playerSecondNameBox.Text != "") && (currentIndexPlayer < 20)) // Validation check to see if all main fields were entered
{
for (int i = 0; i < playerArray.GetUpperBound(0); i++ )
{
for (int j = 0; j < playerArray.GetUpperBound(1); j++)
{
if (playerArray[i,j].Contains(playerFirstNameBox.Text) && (playerArray[i,j].Contains(playerIgNameBox.Text) && playerArray[i,j].Contains(playerSecondNameBox.Text)))
{
MessageBox.Show("Player Duplicate!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); //Validation failed, display error message//
}
}
}
playerArray[currentIndexPlayer, 0] = playerFirstNameBox.Text;
playerArray[currentIndexPlayer, 1] = playerIgNameBox.Text;
playerArray[currentIndexPlayer, 2] = playerSecondNameBox.Text;
playerArray[currentIndexPlayer, 3] = contactStreetBox.Text;
playerArray[currentIndexPlayer, 4] = contactTownBox.Text;
playerArray[currentIndexPlayer, 5] = contactPostcodeBox.Text;
playerArray[currentIndexPlayer, 6] = contactEmailBox.Text;
playerArray[currentIndexPlayer, 7] = contactTelephoneBox.Text;
if (currentIndexPlayer < 20)
{
currentIndexPlayer++;
}
}
else
{
if ((playerFirstNameBox.Text != "") && (playerIgNameBox.Text != "") && (playerSecondNameBox.Text != ""))
{
MessageBox.Show("Database full!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); //Validation failed, display error message//
addNewPlayerBtn.Text = "Confirm";
}
else
{
MessageBox.Show("Fields missing!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); //Validation failed, display error message//
addNewPlayerBtn.Text = "Confirm";
}
}
addNewPlayerBtn.Text = "Add New Player";
playerFirstNameBox.ReadOnly = true; //lock all fields for input again//
playerIgNameBox.ReadOnly = true;
playerSecondNameBox.ReadOnly = true;
contactStreetBox.ReadOnly = true;
contactTownBox.ReadOnly = true;
contactPostcodeBox.ReadOnly = true;
contactEmailBox.ReadOnly = true;
contactTelephoneBox.ReadOnly = true;
}
}
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 searchForm = new Form2();
searchForm.Show();
}
private void showPreviousBtn_Click(object sender, EventArgs e)
{
if(currentIndex == 0)
{
currentIndex = 19;
}
else
{
currentIndex--;
}
playerFirstNameBox.Text = playerArray[currentIndex, 0];
playerIgNameBox.Text = playerArray[currentIndex, 1];
playerSecondNameBox.Text = playerArray[currentIndex, 2];
contactStreetBox.Text = playerArray[currentIndex, 3];
contactTownBox.Text = playerArray[currentIndex, 4];
contactPostcodeBox.Text = playerArray[currentIndex, 5];
contactEmailBox.Text = playerArray[currentIndex, 6];
contactTelephoneBox.Text = playerArray[currentIndex, 7];
positionLabel.Text = "Current position:" + (currentIndex+1).ToString() + "/20";
}
private void showNextBtn_Click(object sender, EventArgs e)
{
if (currentIndex == 19)
{
currentIndex = 0;
}
else
{
currentIndex++;
}
playerFirstNameBox.Text = playerArray[currentIndex, 0];
playerIgNameBox.Text = playerArray[currentIndex, 1];
playerSecondNameBox.Text = playerArray[currentIndex, 2];
contactStreetBox.Text = playerArray[currentIndex, 3];
contactTownBox.Text = playerArray[currentIndex, 4];
contactPostcodeBox.Text = playerArray[currentIndex, 5];
contactEmailBox.Text = playerArray[currentIndex, 6];
contactTelephoneBox.Text = playerArray[currentIndex, 7];
positionLabel.Text = "Current position:" + (currentIndex + 1).ToString() + "/20";
}
}
}