在 C# 应用程序中获取 NullReferenceException 并且看不到取消引用为 null 的原因
Getting NullReferenceException in a C# App and cant see reason why dereference is null
我编写了一些简单的代码,但由于某些我不知道的原因导致 NullReferenceException 而无法正常工作。
这是简单的应用程序
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private string[] IPToCheck;
private List<string> IPRange;
private bool CorrectNetwork = false;
public MainPage()
{
this.InitializeComponent();
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
if (hn.IPInformation != null &&
(hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71
|| hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
{
IPToCheck = hn.DisplayName.Split(new char[] { '.' });
if (IPToCheck.Count() == 4)
{
Debug.WriteLine("Correct");
CorrectNetwork = true;
}
if (CorrectNetwork)
{
Debug.WriteLine("{0}.{1}.{2}.",IPToCheck);
GenerateIPs(IPToCheck);
break;
}
}
}
}
private void GenerateIPs(string[] IPToCheck)
{
for (int i = 0; i < 255; i++)
{
Debug.WriteLine(IPToCheck[0] + "." + IPToCheck[1] + "." + IPToCheck[2] + "." + i.ToString());
IPRange.Add(IPToCheck[0] + "." + IPToCheck[1] + "." + IPToCheck[2] + "." + i.ToString());
}
}
}
}
当 运行 时,我得到以下输出:
Correct
192.168.10.
192.168.10.0
A first chance exception of type 'System.NullReferenceException' occurred in App1.Windows.exe
突出显示:
IPRange.Add(IPToCheck[0] + "." + IPToCheck[1] + "." + IPToCheck[2] + "." + i.ToString());
似乎第一个 "ip" 是在 GenerateIPs 方法的 for 循环中生成的。
为什么会出现 NullReferenceException?谢谢!
您忘记添加:
this.IPRange = new List<string>();
在您的 MainPage
构造函数中。
我编写了一些简单的代码,但由于某些我不知道的原因导致 NullReferenceException 而无法正常工作。
这是简单的应用程序
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private string[] IPToCheck;
private List<string> IPRange;
private bool CorrectNetwork = false;
public MainPage()
{
this.InitializeComponent();
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
if (hn.IPInformation != null &&
(hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71
|| hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
{
IPToCheck = hn.DisplayName.Split(new char[] { '.' });
if (IPToCheck.Count() == 4)
{
Debug.WriteLine("Correct");
CorrectNetwork = true;
}
if (CorrectNetwork)
{
Debug.WriteLine("{0}.{1}.{2}.",IPToCheck);
GenerateIPs(IPToCheck);
break;
}
}
}
}
private void GenerateIPs(string[] IPToCheck)
{
for (int i = 0; i < 255; i++)
{
Debug.WriteLine(IPToCheck[0] + "." + IPToCheck[1] + "." + IPToCheck[2] + "." + i.ToString());
IPRange.Add(IPToCheck[0] + "." + IPToCheck[1] + "." + IPToCheck[2] + "." + i.ToString());
}
}
}
}
当 运行 时,我得到以下输出:
Correct
192.168.10.
192.168.10.0
A first chance exception of type 'System.NullReferenceException' occurred in App1.Windows.exe
突出显示:
IPRange.Add(IPToCheck[0] + "." + IPToCheck[1] + "." + IPToCheck[2] + "." + i.ToString());
似乎第一个 "ip" 是在 GenerateIPs 方法的 for 循环中生成的。
为什么会出现 NullReferenceException?谢谢!
您忘记添加:
this.IPRange = new List<string>();
在您的 MainPage
构造函数中。