Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

I am wondering this code(exe having this code) is working fine on my local machine, but when i use to run this code(exe having this code) on other machine I am getting this exception at run time.
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.<br />

Any help will be greatly appreciated.

here is my code:

do
       {
           HtmlElementCollection elc = el.Children;

               Id.CodeData = elc[0].InnerText;
               Id.StockData = elc[1].InnerText;
               Id.TypeData = elc[2].InnerText;
               Id.QuantityData = elc[3].InnerText;
               Id.PartData = elc[4].InnerText;
               Idata.Add(Id);
               el = el.NextSibling;

       } while (el!= null);
Posted
Updated 15-Apr-11 4:34am
v2
Comments
Olivier Levrey 15-Apr-11 10:38am    
What about the debugger?

Four possibilities:
1) Idata is not set to anything
2) Id is not set to anything
3) el is not set to anything
4) el has no children.

Assuming you have checked Idata, Id, and el, and that they are all valid, then re-code it a bit more defensively:
do
       {
           HtmlElementCollection elc = el.Children;
           if (elc != null)
               {
               Id.CodeData = elc[0].InnerText;
               Id.StockData = elc[1].InnerText;
               Id.TypeData = elc[2].InnerText;
               Id.QuantityData = elc[3].InnerText;
               Id.PartData = elc[4].InnerText;
               Idata.Add(Id);
               }
               el = el.NextSibling;

       } while (el!= null);
 
Share this answer
 
Comments
shrikant003 15-Apr-11 11:12am    
I tried my all possibilities, If supose el is havig 50 childrens and if I iterate up to 49 its executing fine , but its failing after the last children . i m wondering its not executing on other machine on my local machine everything is executing fine.
Please stop all this pointless guesswork. Run it under debugger and you will see very clearly what's wrong.
Also, there is no chance that it works differently on different machine; you need to reproduce things correctly, that's it. Also, stop your trial-and-error approach shown by your "I tried my all possibilities" note. You goal now is not make it working but reproduce how it is not working, to understand why. Use just two or three note.

The problem looks like very trivial. Get to work, set a breakpoint.

—SA
 
Share this answer
 
Comments
#realJSOP 18-Jun-11 12:25pm    
You're just spitting in the wind, but you get a 5 for stating the obvious.
Sergey Alexandrovich Kryukov 20-Jun-11 1:31am    
Thank you.
Obvious to some, apparently not for others; for some it's so unusual advice so it really makes it spitting in (against, I would say) the wind, you're right. :-)
--SA
you should do this:

C#
while (el != null && el.HasChildren)
{
    HtmlElementCollection elc = el.Children;
    ...
    ...
}


You can also do this:

C#
while (el != null && el.Children.Count > 0)
{
    HtmlElementCollection elc = el.Children;
    ...
    ...
}


To retrieve the children, you should REALLY use key names instead of relying on there always being five of them, and in the expected order. You're doomed to fail if the data changes significantly.
 
Share this answer
 
Comments
shrikant003 15-Apr-11 11:13am    
Count will remain same as it is a table i m parsing. I tried my all possibilities, If supose el is havig 50 childrens and if I iterate up to 49 its executing fine , but its failing after the last children . i m wondering its not executing on other machine on my local machine everything is executing fine.
#realJSOP 18-Jun-11 12:26pm    
Of course, you care certainly free to ignore my advice. Good luck with your project.
Probably the HTML source isn't available on the other machine.

For situations where the condition can fail immediately you should use while. If something must execute at least once you can use do..while.
while (el!= null)
     {
         HtmlElementCollection elc = el.Children;

             Id.CodeData = elc[0].InnerText;
             Id.StockData = elc[1].InnerText;
             Id.TypeData = elc[2].InnerText;
             Id.QuantityData = elc[3].InnerText;
             Id.PartData = elc[4].InnerText;
             Idata.Add(Id);
             el = el.NextSibling;
      };

Good luck!
 
Share this answer
 
Comments
shrikant003 15-Apr-11 11:11am    
I tried my all possibilities, If supose el is havig 50 childrens and if I iterate up to 49 its executing fine , but its failing after the last children . i m wondering its not executing on other machine on my local machine everything is executing fine.
E.F. Nijboer 15-Apr-11 11:59am    
Now you are still assuming that elc[] has 0..4 entries. You can check this out with the debugger using some breakpoints. It also looks like the data on the other machine is different that on your local machine you can also check this out. But you should make your code more robust and check array length as well before trying to accessing. For now, have a go with the debugger to check things out.
I suspect that this is happening when you index elc. You have hardcoded to index elc from 0 to 4, but you are not checking that elc.count > 4. Your code should look something like this:

while (el!= null && el.Children.Count > 4)
{
        HtmlElementCollection elc = el.Children;

        Id.CodeData = elc[0].InnerText;
        Id.StockData = elc[1].InnerText;
        Id.TypeData = elc[2].InnerText;
        Id.QuantityData = elc[3].InnerText;
        Id.PartData = elc[4].InnerText;
        Idata.Add(Id);
        el = el.NextSibling;

}
 
Share this answer
 
v2
Hi
I found the solution I put my code in try and catch block where I caught the exception and did nothing in catch block , now its working fine.

C#
try{
    do
    {
        HtmlElementCollection elc = el.Children;

            Id.CodeData = elc[0].InnerText;
            Id.StockData = elc[1].InnerText;
            Id.TypeData = elc[2].InnerText;
            Id.QuantityData = elc[3].InnerText;
            Id.PartData = elc[4].InnerText;
            Idata.Add(Id);
            el = el.NextSibling;

    } while (el!= null);

}
catch (Exception exp)
{
    //Console.WriteLine(exp.Message + "Failed here");
}
 
Share this answer
 
v2
Comments
BillWoodruff 28-Aug-11 1:33am    
That's not a solution, I am afraid: that's sweeping the problem "under the rug." You got lots of great advice on this thread on how to figure out what's wrong: use it :)
i think this it true

C#
try{
                  do
                  {
                        HtmlElementCollection elc = el.Children;
                              Id.CodeData = elc[0].InnerText;
                              Id.StockData = elc[1].InnerText;
                              Id.TypeData = elc[2].InnerText;
                              Id.QuantityData = elc[3].InnerText;
                              Id.PartData = elc[4].InnerText;
                              Idata.Add(Id);
                              el = el.NextSibling;
                  } while (el!= null);
            }
            catch (Exception exp)
            {
                  //Console.WriteLine(exp.Message + &quot;Failed here&quot;);
            }


friv | friv 4
 
Share this answer
 
I am testing these codes, hope it work :d

while (el!= null && el.Children.Count > 4)
{
        HtmlElementCollection elc = el.Children;

        Id.CodeData = elc[0].InnerText;
        Id.StockData = elc[1].InnerText;
        Id.TypeData = elc[2].InnerText;
        Id.QuantityData = elc[3].InnerText;
        Id.PartData = elc[4].InnerText;
        Idata.Add(Id);
        el = el.NextSibling;

}


Friv | Friv.com
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900