English 中文(简体)
每一 lo因
原标题:Foreach loop causes NullReferenceException

At the top of my code (before constructor) I write: String[] CAN = null;

这一阵列在另一项职能中得到了更新。

后来,我手法中就有了这样的预兆:

foreach (String str in CAN)
{
    if(str.Contains("18FA07FE"))
        cmdResult = true;
    else
        cmdResult = false;
}

我试图辩论,根据每次发言,我可以看到,加固阵列已经成功更新,现在包含1211个内容。

So I don t really know why it s giving me this exception.

最佳回答

如果问题仅与你所展示的代码有关,而且你所描述的代码的确含有内容,那么唯一可能的解释是,<<<<<<>>>>>>中的一项内容是无意义的,因此,放弃这一例外的是每一条之 for。

if (str.Contains(...))
    ^^^
     |
     +-- null
问题回答

或许,扼杀性价值之一是无效的。

string [] CAN =  { "first", "second", null, "fourth" };

            foreach ( string str in CAN ) {
                if ( str.Contains( "fourth" ) ) {
                    Console.WriteLine( "Success" );
                }
            }

你们在哪里获得Null Pointer? 它是 lo还是 if? 我猜测你的阵列含有一个丢弃本民族党的无效内容。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签