简体   繁体   English

如何查看清单 <string> 一片空白

[英]how to check List<string> is null

I wanna to check the values (its type is list) and if it is null, it starts again from at the beginning of the loop and if it is not, counter++. 我想检查值(它的类型是list),如果它为null,它会从循环的开头重新开始,如果不是,则是counter ++。 I mean if values is like: "values": [] the compile back at the beginning of loop. 我的意思是,如果值就像: "values": []在循环开始时重新编译。

I tried with if (id==null) and also I used this one (id.Contains("")) but the error happens. 我尝试了if (id==null) and also I used this one (id.Contains(""))但是发生了错误。 Error:Index was out of range. 错误:索引超出范围。 Must be non-negative and less than the size of the collection.Parameter name: index (Argument out of range exception was unhandled). 必须为非负数且小于集合的大小。参数名称:index(未处理参数超出范围的异常)。

Data that I want to check (when the values is null) 我要检查的数据(当值为空时)

{
  "cid": "241",
  "fnames": [
    "id",
    "name",
    "hash",
    "score",
    "rank"
  ],
  "values": [],
  "tal": 0,
  "sion": "v3"
}

Full data (when the values is not null) 完整数据(值不为null时)

{
  "cid": "64",
  "fnames": [
    "id",
    "name",
    "hash",
    "score",
    "rank"
  ],
  "values": [
    [
      "126",
      "126",
      "126",
      "1",
      "2.77"
    ],
      "tal": 0,
      "sion": "v3"
    }

Code

foreach (DataRow row in dt.Rows)
{
string url = "http://hgsfe/<uid>?groups=<uid>
var test = url.Replace("<uid>", Convert.ToString(row[uid]));
System.Diagnostics.Process.Start(test);
string client = (new WebClient()).DownloadString(test);
var pi = JsonConvert.DeserializeObject<read_json>(client);
List<string> id = pi.values[0];
if (id==null)         //also I used this one (id.Contains(""))   
continue;
if (id.Contains(Convert.ToString(row[tid])))
{
counter++;
}

尝试检查pi.Countpi.Length以获取列表中的项目数。

You have to check both: 您必须同时检查:

 pi == null
 pi.values[0] == null

because you try the assignment, but pi doesn't event exist (probably) 因为您尝试分配,但是pi不存在事件(可能)

When you debug your code, you can clearly see, where the application crashes. 在调试代码时,您可以清楚地看到应用程序崩溃的位置。

Hope i could help you with that 希望我能帮你

Try the following 尝试以下

List<string> id = pi.values.ToList();

And after this just check the Count property of the list 然后,只需检查列表的Count属性

if(id.Count<1)
    continue;
...
...

you did provide code with a lot of 'unknowns', but here is what could be wrong and you should check it to make your code safer: 您确实为代码提供了很多“未知数”,但是这可能是错误的,应该检查一下以使代码更安全:

  • what is uid ? 什么是uid You are accessing row[uid] . 您正在访问row[uid] row is of type DataRow where accessing item which does not exist (specified by uid ) throws exception row类型为DataRow ,其中不存在的访问项(由uid指定)将引发异常
  • what is pi ? pi是什么? what is pi.values ? pi.valuespi.values Maybe you should check if pi is null, pi.values is null and you should definately check if pi.values contains any items (so if item values[0] is present). 也许您应该检查pi是否为null, pi.values是否为null,并且应该确定检查pi.values包含任何项目(因此,如果item values[0]存在)。 If its empty then values[0] will throw exception you mentioned. 如果其为空,则values[0]将引发您提到的异常。
  • pi.values[0] may be present, but it again may be assigned null. 可能存在pi.values[0] ,但可以再次将其分配为null。 So your line if (id.Contains... may fail on that you are trying to call id.Contains on null item id . Check if its not null before call. 所以您的行if (id.Contains...可能会因为您尝试调用id 。而id.Contains null项目id而失败。请在调用之前检查其是否不为null。
  • what is tid in row[tid] ? 什么是tidrow[tid] Again, check if such item is present in the row so you won't get DataRow exception. 再次,检查该项目是否存在于行中,这样就不会出现DataRow异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM