【发布时间】:2021-04-24 21:32:35
【问题描述】:
我正在尝试根据组合框中选择的内容进行一些计算,但我不断收到 System.NullReferenceException: 'Object reference not set to an instance of an object。作为一个错误。
这是我的代码,错误出现在if语句的那一行:
private void TokenWorthtxtBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (BookCondition.SelectedItem == null || BookGenre.SelectedItem == null || BookFormat.SelectedItem == null || BookExamBoard.SelectedItem == null)//this is where the error appears
{
errormessage.Text = "Please select values from the drop down boxes.";
}
else
{
int tokenPrice = BookPriceCalculation(BookTitleTxtBox.Text, BookCondition.SelectedItem.ToString(), BookGenre.SelectedItem.ToString(), BookFormat.SelectedItem.ToString(), BookExamBoard.SelectedItem.ToString());
DisplaySuggestedTokentxtBlock.Text = $"Suggested Token: {tokenPrice}";
}
}
【问题讨论】:
-
您是否在构造函数中调用
InitializeComponents()之前设置/访问这些值? -
我已经关闭了您之前的问题,作为经典问题"What is a NullReferenceException and how I fix it" 的副本。我不想对您的问题重复相同的操作,但实际上,您对重复项有什么不明白的地方?
-
不,但我正在访问私有函数中的值
-
这能回答你的问题吗? What is a NullReferenceException, and how do I fix it?@Steve 我会为你做的