我在这里用List做错了什么 [英] what I am doing wrong here with List
问题描述
List<int> processedList = new List<int>();
processedList = (List<int>) Session["processedUsers"];
我在这里获取参数null引用异常吗???
I am getting argument null reference exception here????
if(processedList.Count != 0)
{
Boolean exists = processedList.Contains(i);
if (!exists)
{
}
我甚至收到Null参考异常
布尔值存在= processingList.Exists(element => element == i)
会话状态列表可以为空.我仍然进行了此测试.
请告知.
I am getting Null reference exception even
Boolean exists = processedList.Exists(element => element ==i)
The session state list can be empty. Still I have run this test.
Please advise.
推荐答案
您用Session
中的值覆盖了processedList
实例.因此,如果会话中没有列表实例,则会出现此异常.
因此,您可能没有正确初始化Session["processedUsers"]
,因此会遇到异常.
You overwrite yourprocessedList
instance with a value fromSession
. So if there is no list instance in the session, you will get this exception.
So you probably don''t have correctly initializedSession["processedUsers"]
and thus you have exception.
简单地放置
Put simply,
List<int> processedList = new List<int>();
创建处理列表并为其分配一个新的空列表.
Creates processedList and assignes a new, empty list to it.
processedList = (List<int>) Session["processedUsers"];
用强制转换返回的内容替换空列表.
Replaces the empty list with whatever the cast returns.
if(processedList.Count != 0)
使用处理列表中包含的列表来检查条目数.
因此,如果Session["processedUSers"]
不返回列表,则:
如果返回非列表项,则将引发无法转换"异常.
如果返回null,则processedUsers
将变为null,并且分配给您的空列表将被丢弃.
如果processedUsers
为null,则尝试访问任何元素,即使项目计数也将给出null引用异常.
因此:在尝试使用Session["processedUSers"]
之前,先检查Session["processedUSers"]
返回的内容,并确保processedItems
是有效列表.
Uses the list contained in processedList to check the number of entries.
So if Session["processedUSers"]
does not return a list then:
if it returns a non-list item it will throw a "cannot convert" exception.
if it returns a null, processedUsers
will become null, and the empty list you assigned will be discarded.
If processedUsers
is null, then attempting to access any element, even the items count, will give a null reference exception.
So: Check what Session["processedUSers"]
is returning, and make sure that processedItems
is a valid list before you try and use3 it.
这篇关于我在这里用List做错了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!