我是C#.NET的初学者,我们的任务是创建一个在线银行系统,其中事务和登录数据存储在一个类数组中。虽然我正在慢慢掌握整个类的概念,但似乎我仍然坚持将登录数据保存到类的数组中。这是我的计划:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BankTransaction;
namespace LabExam1
{
public partial class Registration : Form
{
Transactions trans = new Transactions();
int number = 10000;
int x = 0;
const int size = 100;
Transactions[] loginData = new Transactions[size];
public void saveLoginData()
{
loginData[x].Username = trans.createUserName(txtFname.Text, txtLname.Text, txtMi.Text);
loginData[x].Password = txtPass.Text;
x++;
}
public Registration()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cboType.SelectedIndex = 0;
}
private void btnRegister_Click(object sender, EventArgs e)
{
if (txtFname.Text == "" || txtLname.Text == ""|| txtMi.Text == "" || txtPass.Text == "")
{
MessageBox.Show("Please fill up all required fields!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (cboType.SelectedIndex == 0)
{
number++;
loginData[x] = new Transactions();
saveLoginData();
MessageBox.Show("Generated Username: " + loginData[x].Username + number + "\n" + "Please do not share this Information to anyone!");
this.Hide();
Transaction tr = new Transaction();
tr.ShowDialog();
}
if (cboType.SelectedIndex == 1)
{
if (nudDeposit.Value < 2500.00m)
{
MessageBox.Show("The initial deposit for your account type is insufficient", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
number++;
loginData[x] = new Transactions();
saveLoginData();
MessageBox.Show("Generated Username: " + loginData[x].Username + number + "\n" + "Please do not share this Information to anyone!");
this.Hide();
Transaction tr = new Transaction();
tr.ShowDialog();
}
}
if (cboType.SelectedIndex == 2)
{
if (nudDeposit.Value < 3000.00m)
{
MessageBox.Show("The initial deposit for your account type is insufficient", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
number++;
loginData[x] = new Transactions();
saveLoginData();
MessageBox.Show("Generated Username: " + loginData[x].Username + number + "\n" + "Please do not share this Information to anyone!");
this.Hide();
Transaction tr = new Transaction();
tr.ShowDialog();
}
}
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
我在这部分引发错误,引发nullReferenceException
:
MessageBox.Show("Generated Username: " + loginData[x].Username + number + "\n" + "Please do not share this Information to anyone!");
这是我的班级定义:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BankTransaction
{
public class Transactions
{
String username, password;
#region forArrayOfClassLogin
public String Password
{
get { return password; }
set { password = value; }
}
public String Username
{
get { return username; }
set { username = value; }
}
#endregion
public String createUserName(String fname, String lname, String mi)
{
String firstString = fname[0].ToString();
String secondString = mi[0].ToString();
String thirdString = lname[0].ToString();
String uname = firstString + secondString + thirdString;
return uname;
}
public void setBalance(String type, decimal initialBalance)
{
}
public String getUserName()
{
return username;
}
public String setPassword(String pass)
{
return password = pass;
}
public String getPassword()
{
return password;
}
}
}
非常感谢任何帮助。提前谢谢。
答案 0 :(得分:1)
您需要使用loginData[x -1]
,因为您增加了x
saveLoginData()
的值
MessageBox.Show("Generated Username: " + loginData[x -1].Username + number + "\n" + "Please do not share this Information to anyone!");
答案 1 :(得分:1)
这个问题似乎符合“x ++”的要求。在函数saveLoginData()。
中您将X声明为成员级别变量,X = 0。
当你在“saveLoginData()”函数中递增x的值时,它变为X = 1,然后在尝试访问父函数中的数组时,X的值现在为1,但是你的数组没有任何值为该索引存储的值,这会导致异常。
答案 2 :(得分:0)
String firstString = fname[0].ToString();
String secondString = mi[0].ToString();
String thirdString = lname[0].ToString();
而不是上面的行尝试
String firstString = fname;
String secondString = mi;
String thirdString = lname;
请参阅以下链接了解更多详情。 http://msdn.microsoft.com/en-us/library/ms228362.aspx
答案 3 :(得分:0)
最后在saveLoginData
x++
,
当您在邮件中引用loginData[x].Username
时,x
与设置loginData[x] = new Transactions();
时的loginData[x]
不同,因此此时{{1}}将为空。
答案 4 :(得分:0)
Think是每当我们获得空引用时......它意味着它没有内存... 所以调试每一行你可以找到解决方案..否则在使用属性put null check condition
之前答案 5 :(得分:0)
loginData[x] = new Transactions();
saveLoginData();
MessageBox.Show("Generated Username: " + loginData[x].Username + number + "\n" + "Please do not share this Information to anyone!");
这里的问题是对saveLoginData()
的调用会增加x的值,所以当你显示MessageBox时,你指向loginData
中未初始化的下一个元素。< / p>
这是一个很好的说明,为什么你应该警惕使用全局变量。 saveLoginData
修改x
并不明显,这可能就是你首先犯错误的原因。
另外,如果这不是练习,我建议使用List<Transactions>
而不是数组,因为这样可以更容易地添加元素;你不必自己跟踪元素的数量。
最后,此片段在您的代码中出现多次:
number++;
loginData[x] = new Transactions();
saveLoginData();
MessageBox.Show("Generated Username: " + loginData[x].Username + number + "\n" + "Please do not share this Information to anyone!");
this.Hide();
Transaction tr = new Transaction();
tr.ShowDialog();
最好把它变成一个你可以多次调用的方法。