问题描述
- c#中未将对象引用设置到对象的实例。改了好久改不来 求大神指导
-
private void butkeyverify_Click(object sender, EventArgs e)
{try { byte a, b; string c = null; //生成密钥类型 { if (cmbkeytype.SelectedValue.ToString() == "密钥A") { a = 0x60; } else { a = 0x61; } } //生成扇区号 { if (cmbcardsection.SelectedValue.ToString() == "扇区0") { b = 0x00; } else if (cmbcardsection.SelectedValue.ToString() == "扇区1") { b = 0x01; } else if (cmbcardsection.SelectedValue.ToString() == "扇区2") { b = 0x02; } else if (cmbcardsection.SelectedValue.ToString() == "扇区3") { b = 0x03; } else if (cmbcardsection.SelectedValue.ToString() == "扇区4") { b = 0x04; } else if (cmbcardsection.SelectedValue.ToString() == "扇区5") { b = 0x05; } else if (cmbcardsection.SelectedValue.ToString() == "扇区6") { b = 0x06; } else if (cmbcardsection.SelectedValue.ToString() == "扇区7") { b = 0x07; } else if (cmbcardsection.SelectedValue.ToString() == "扇区8") { b = 0x08; } else if (cmbcardsection.SelectedValue.ToString() == "扇区9") { b = 0x09; } else if (cmbcardsection.SelectedValue.ToString() == "扇区10") { b = 0x10; } else if (cmbcardsection.SelectedValue.ToString() == "扇区11") { b = 0x11; } else if (cmbcardsection.SelectedValue.ToString() == "扇区12") { b = 0x12; } else if (cmbcardsection.SelectedValue.ToString() == "扇区13") { b = 0x13; } else if (cmbcardsection.SelectedValue.ToString() == "扇区14") { b = 0x14; } else { b = 0x15; } } //生成密钥 c = txtkey.Text; if (xf != null) { ErrorCode code = ErrorCode.Success; code = xf.KeyVerify(a, b, c); if (code == ErrorCode.Success) { tbReadLog.Text += "密钥验证成功! "; } else tbReadLog.Text += "密钥验证失败! "; } } catch (Exception err) { MessageBox.Show(err.Message); } }
解决方案
f5调试方式运行,看出错停在哪一行。
另外
if (cmbcardsection.SelectedValue.ToString() == "扇区0")
{
b = 0x00;
}
else if (cmbcardsection.SelectedValue.ToString() == "扇区1")
{
b = 0x01;
}
。。。
写出这么啰嗦的代码难道没有想过改进下么?
else if (cmbcardsection.SelectedValue.ToString() == "扇区10")
{
b = 0x10;
}
这个你确认么?按照合乎逻辑的设计,应该是
else if (cmbcardsection.SelectedValue.ToString() == "扇区A")
{
b = 0xA;
}
或者
else if (cmbcardsection.SelectedValue.ToString() == "扇区10")
{
b = 10;
}
当然你的程序也不怎么合乎逻辑。
解决方案二:
你贴出来的代码是完整的代码吗?
把string c = null;改成string c = string.Empty;试试
解决方案三:
C# 未将对象引用设置到对象的实例
c#出错:System.NullReferenceException: 未将对象引用设置到对象的实例