Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将字符串数组转换为Int32_C# - Fatal编程技术网

C# 无法将字符串数组转换为Int32

C# 无法将字符串数组转换为Int32,c#,C#,我的代码: basketsub.Properties.ColorId = Convert.ToInt32(itemspliters[3] != null ? itemspliters[3].ToString() : "0"); 错误: basketsub.Properties.ColorId = Convert.ToInt32(itemspliters[3] != null ? itemspliters[3].ToString() : "0"); 对象引用未设置为对象

我的代码

basketsub.Properties.ColorId = Convert.ToInt32(itemspliters[3] != null 
  ? itemspliters[3].ToString() 
  : "0");
错误

basketsub.Properties.ColorId = Convert.ToInt32(itemspliters[3] != null 
  ? itemspliters[3].ToString() 
  : "0");
对象引用未设置为对象的实例


你能告诉我我做错了什么吗?谢谢。

您还应该检查itemspliters!=空的

basketsub.Properties.ColorId = Convert.ToInt32(itemspliters != null && itemspliters.Length >= 4 && itemspliters[3] != null 
  ? itemspliters[3].ToString() 
  : "0");

您是否调试了所选IDE中的代码以确定该代码行中的多个成员中的哪一个为空?寻求调试帮助的问题(“此代码为什么不工作?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现该代码所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅。检查
itemspillers
basketsub
basketsub.Properties
是否有
null
?如果数组本身为空,则此代码将通过NRE。您应该首先确保这些值不是空的,而不是试图处理空值。代码也需要相当多的清理。如果
itemspilters
包含字符串,则不需要
ToString()。如果没有,它包含什么?在任何情况下,您都可以使用null传播运算符安全地获取值,甚至在出现问题时不尝试转换,例如
var value=itemspillers?[3]?.ToString();basketsub.Properties.ColorId=(值==null)?0:int.Parse(值)