对于循环错误:'NullReferenceException未处理'请帮助 [英] For loop error: 'NullReferenceException was unhandled' please help

查看:62
本文介绍了对于循环错误:'NullReferenceException未处理'请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些for循环来确定所选位置(在此代码中,2个应用程序设置)与使用Haversine公式的大约5000个其他位置之间的距离(在此代码中,名为'Formula'的函数)一个名为'Haversine'的类文件。循环工作方式我想要它并且完全没问题,直到它到达文件中的倒数第二个记录,此时它停止并抛出错误。我想知道是否有人会帮我解决它。代码在下面但没有函数,因为它在一个单独的文件中。



  static   void  Main( string  [] args)
{
XmlDocument SiteList = new XmlDocument();
SiteList.Load( C:/Users/Henry/WeatherWatcher/Development/sitelist.xml);
List< string> list = new List< string>();
Console.WriteLine( HAVERSINE:BASE LA - + Properties.Settings.Default。 LATITUDE + LO - + Properties.Settings.Default.LONGITUDE);
for int x = 0 ; x < SiteList.SelectNodes( /位置/位置/ @ id)。计数; x ++)
{
double ListLO = Convert.ToDouble(SiteList.SelectNodes) ( / Locations / Location / @ longitude)[x] .InnerText);
for int y = 0 ; y < SiteList.SelectNodes( /地点/位置/ @纬度)。计数; x ++)
{

double ListLA = Convert.ToDouble (SiteList.SelectNodes( / Locations / Location / @ latitude)[x] .InnerText);
string Item = Convert.ToString(Haversine.Formula(Convert.ToDouble( 52 。< span class =code-digit> 2802588
),Convert.ToDouble(Properties.Settings.Default.LONGITUDE),ListLA,ListLO)+ < span class =code-string> --- + SiteList.SelectNodes( / Locations /位置/ @ ID)[X] .InnerText);
Console.WriteLine(Item);
list.Add(Convert.ToString(Item.ToString()));
Console.WriteLine( );
// TOTAL_ 5363
Console.WriteLine(x);
}
Console.ReadKey();
}
}





它正在读取的xml文件的结构如下:(有一个'< location'>

 <?  xml     version   =  1.0    encoding   =  ISO-8859-1  >  
- < 地点 >
< 位置 name = Rosehearty Samos 经度 = - 2.121 纬度 = 57.698 id = < span class =code-keyword> 3094 / >
< 位置 名称 = Strathallan 经度 = - 3.729 纬度 = 56.326 id = 3144 / >
< 位置 name = Edinburgh / Gogarbank 经度 = - 3.343 纬度 = 55.928 id = 3166 / >



...

 <  位置   名称  = 罗西里湾(海滩)   经度  =   -  4.2905   纬度  =  51.5707    id   =  354477   /  >  
< 位置 名称 = Perranporth - Village(Beach) 经度 = - 5.1535 纬度 = 50.3491 id = < span class =code-keyword> 354541 / >
< 位置 名称 = Perranuthnoe(Perran Sands)(海滩) 经度 = - 5.4411 纬度 = 50.1121 id = 354552 / >
< /地点 >





我要处理的位置数是5363(文件中的总数)但是它会停止并给出错误e控制台输出为:

...

5359

87.7415106490408 --- 354477



5360

218.280932624672 --- 354541



5361

244.281295790985 --- 354552



5362



它应该做的是处理所有5363个位置并显示它们然后关闭。它没有处理第二个最后位置和最后一个位置给出错误:



System.NullReferenceException未处理

对象引用未设置为对象的实例。



我不知道该怎么做所以我想知道你们中的一个是否可以帮助让循环工作。我写的是使用MS VS2010的C#。我希望这是有道理的,谢谢你的帮助。

解决方案

也许你可以开始更换



  for  int  y =  0 ; y <  SiteList.SelectNodes(  / Locations / Location / @ latitude)。Count; x ++)





with



  for  int  y =  0 ; y <  SiteList.SelectNodes(   / Locations / Location / @ latitude)。Count; y ++)





似乎更有意义。


在下面的循环中,你递增x而不是y



 for(int y = 0; y< 。SiteList.SelectNodes( /地点/位置/ @北纬)计数;  x ++ 


我在重新思考和重写算法后用自己的代码解决了这个问题:



 静态  void  Main( string  [] args)
{
XmlDocument SiteList = new XmlDocument();
SiteList.Load( C:/Users/Henry/WeatherWatcher/Development/sitelist.xml);
List< string> FinalList = new List< string>();
for int x = 0 ; x < SiteList.SelectNodes( /地点/位置/ @ id)。计数; x ++)
{
(x = 0 ; x < SiteList.SelectNodes( / Locations / Location / @ latitude)。Count; x ++)
{
for (x = 0 ; x < SiteList.SelectNodes( / Locations / Location / @ longitude)。Count; x ++)
{
Console.ForegroundColor = ConsoleColor 。白色;
Console.WriteLine( ID: + SiteList.SelectNodes( Locations / Location / @ id)[x] .InnerText);
Console.WriteLine( LA: + SiteList.SelectNodes( / Locations / Location / @ latitude)[x] .InnerText);
Console.WriteLine( LO: + SiteList.SelectNodes( / Locations / Location / @longitude)[x] .InnerText);
Console.WriteLine( HF: + Haversine.Formula(Convert.ToDouble(Properties) .Settings.Default.LATITUDE),Convert.ToDouble(Properties.Settings.Default.LONGITUDE),Convert.ToDouble(SiteList.SelectNodes( / Locations / Location / @ latitude)[x] .InnerText),Convert.ToDouble(SiteList.SelectNodes( /地点/位置/ @经度)[X] .InnerText)));
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine( AL: + Haversine.Formula(Convert.ToDouble(Properties) .Settings.Default.LATITUDE),Convert.ToDouble(Properties.Settings.Default.LONGITUDE),Convert.ToDouble(SiteList.SelectNodes( / Locations / Location / @ latitude)[x] .InnerText),Convert.ToDouble(SiteList.SelectNodes( / Locations / Location / @longitude)[x] .InnerText))+ --- + SiteList.SelectNodes( / Locations / Location / @ id)[x].InnerText);
FinalList.Add(Convert.ToString(Haversine.Formula(Convert.ToDouble(Properties.Settings.Default.LATITUDE)),Convert.ToDouble(Properties.Settings.Default.LONGITUDE),Convert.ToDouble(SiteList.SelectNodes( / Locations / Location / @ latitude)[x] .InnerText),Convert.ToDouble(SiteList) .SelectNodes( / Locations / Location / @ longitude)[x] .InnerText))+ < span class =code-string>
--- + SiteList.SelectNodes( / Locations / Location / @ id)[x] .InnerText));
Console.WriteLine(Environment.NewLine);
}
}
}
foreach object o in FinalList)
{
Console.WriteLine(o);
}
Console.ReadKey();
}
< / string > < / string > ;


I have a few for loops to determine the distances between a selected location (in this code, 2 application settings) and a list of around 5000 other locations using the Haversine formula (in this code, a function named 'Formula' in a class file named 'Haversine'). The loop works how I want it to and is completely fine until it gets to the second last 'record' in the file at which point it stops and throws an error. I was wondering if anyone would help me fix it. The code is below but without the function as it is in a separate file.

static void Main(string[] args)
        {            
            XmlDocument SiteList = new XmlDocument();
            SiteList.Load("C:/Users/Henry/WeatherWatcher/Development/sitelist.xml");
            List<string> list = new List<string>();
            Console.WriteLine("HAVERSINE: BASE LA-" + Properties.Settings.Default.LATITUDE + "LO-" + Properties.Settings.Default.LONGITUDE);
            for (int x = 0; x < SiteList.SelectNodes("/Locations/Location/@id").Count; x++)
            {
                double ListLO = Convert.ToDouble(SiteList.SelectNodes("/Locations/Location/@longitude")[x].InnerText);
                for (int y = 0; y < SiteList.SelectNodes("/Locations/Location/@latitude").Count; x++ )
                {

                    double ListLA = Convert.ToDouble(SiteList.SelectNodes("/Locations/Location/@latitude")[x].InnerText);                    
                    string Item = Convert.ToString(Haversine.Formula(Convert.ToDouble(52.2802588), Convert.ToDouble(Properties.Settings.Default.LONGITUDE), ListLA, ListLO) + "---" + SiteList.SelectNodes("/Locations/Location/@id")[x].InnerText);
                    Console.WriteLine(Item);
                    list.Add(Convert.ToString(Item.ToString()));
                    Console.WriteLine("");                    
                    //TOTAL_ 5363
                    Console.WriteLine(x);                  
                }
                Console.ReadKey();
            }
        }



The xml file it is reading is structured like this: (with one '<location'>

<?xml version="1.0" encoding="ISO-8859-1"?>
-<Locations>
    <Location name="Rosehearty Samos" longitude="-2.121" latitude="57.698" id="3094"/>
    <Location name="Strathallan" longitude="-3.729" latitude="56.326" id="3144"/>
    <Location name="Edinburgh/Gogarbank" longitude="-3.343" latitude="55.928" id="3166"/>


...

    <Location name="Rhossili Bay (Beach)" longitude="-4.2905" latitude="51.5707" id="354477"/>
    <Location name="Perranporth - Village (Beach)" longitude="-5.1535" latitude="50.3491" id="354541"/>
    <Location name="Perranuthnoe (Perran Sands) (Beach)" longitude="-5.4411" latitude="50.1121" id="354552"/>
</Locations>



The number of locations I want to process is 5363 (the total number in the file) however it stops and give an error leaving the console output as:
...
5359
87.7415106490408---354477

5360
218.280932624672---354541

5361
244.281295790985---354552

5362

What it should do is process all 5363 locations and display them then close. It hasn't processed the second last location and the whole of the last one giving the error:

System.NullReferenceException was unhandled
Object reference not set to an instance of an object.

I don't know what to do really so I was wondering if one of you could help get the loops working. I am writing is C# using MS VS2010. I hope this makes sense and thank you for any help.

解决方案

Maybe you could start replacing

for (int y = 0; y < SiteList.SelectNodes("/Locations/Location/@latitude").Count; x++ )



with

for (int y = 0; y < SiteList.SelectNodes("/Locations/Location/@latitude").Count; y++ )



It seems making more sense.


In below loop you incrementing x instead of y

for (int y = 0; y < SiteList.SelectNodes("/Locations/Location/@latitude").Count; x++ )


I solved it myself with this code after rethinking and rewriting the algorithm:

static void Main(string[] args)
        {
            XmlDocument SiteList = new XmlDocument();
            SiteList.Load("C:/Users/Henry/WeatherWatcher/Development/sitelist.xml");
            List<string> FinalList = new List<string>();
            for (int x = 0; x < SiteList.SelectNodes("/Locations/Location/@id").Count; x++)
            {               
                for (x = 0; x < SiteList.SelectNodes("/Locations/Location/@latitude").Count; x++)
                {
                    for (x = 0; x < SiteList.SelectNodes("/Locations/Location/@longitude").Count; x++)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.WriteLine("ID: " + SiteList.SelectNodes("Locations/Location/@id")[x].InnerText);
                        Console.WriteLine("LA: " + SiteList.SelectNodes("/Locations/Location/@latitude")[x].InnerText);
                        Console.WriteLine("LO: " + SiteList.SelectNodes("/Locations/Location/@longitude")[x].InnerText);                       
                        Console.WriteLine("HF: " + Haversine.Formula(Convert.ToDouble(Properties.Settings.Default.LATITUDE), Convert.ToDouble(Properties.Settings.Default.LONGITUDE), Convert.ToDouble(SiteList.SelectNodes("/Locations/Location/@latitude")[x].InnerText), Convert.ToDouble(SiteList.SelectNodes("/Locations/Location/@longitude")[x].InnerText)));
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("AL: " + Haversine.Formula(Convert.ToDouble(Properties.Settings.Default.LATITUDE), Convert.ToDouble(Properties.Settings.Default.LONGITUDE), Convert.ToDouble(SiteList.SelectNodes("/Locations/Location/@latitude")[x].InnerText), Convert.ToDouble(SiteList.SelectNodes("/Locations/Location/@longitude")[x].InnerText)) + "---" + SiteList.SelectNodes("/Locations/Location/@id")[x].InnerText);
                        FinalList.Add(Convert.ToString(Haversine.Formula(Convert.ToDouble(Properties.Settings.Default.LATITUDE), Convert.ToDouble(Properties.Settings.Default.LONGITUDE), Convert.ToDouble(SiteList.SelectNodes("/Locations/Location/@latitude")[x].InnerText), Convert.ToDouble(SiteList.SelectNodes("/Locations/Location/@longitude")[x].InnerText)) + "---" + SiteList.SelectNodes("/Locations/Location/@id")[x].InnerText));
                        Console.WriteLine(Environment.NewLine);
                    }
                }               
            }
            foreach (object o in FinalList)
            {
                Console.WriteLine(o);
            }
            Console.ReadKey();
        }
</string></string>


这篇关于对于循环错误:'NullReferenceException未处理'请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆