Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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.

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 15-Apr-11 5:31am
Edited 15-Apr-11 5:34am
Tarun.K.S42.1K
v2
Comments
Olivier Levrey at 15-Apr-11 10:38am
   
What about the debugger?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
  Permalink  
Comments
shrikant003 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
Comments
John Simmons / outlaw programmer at 18-Jun-11 12:25pm
   
You're just spitting in the wind, but you get a 5 for stating the obvious.
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you should do this:
 
while (el != null && el.HasChildren)
{
    HtmlElementCollection elc = el.Children;
    ...
    ...
}
 
You can also do this:
 
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.
  Permalink  
Comments
shrikant003 at 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.
John Simmons / outlaw programmer at 18-Jun-11 12:26pm
   
Of course, you care certainly free to ignore my advice. Good luck with your project.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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!
  Permalink  
Comments
shrikant003 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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;                
 
            } 
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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.
 
        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");
        }
  Permalink  
v2
Comments
BillWoodruff at 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 :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

i think this it true
 
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");
            }
 
friv | friv 4
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 11

I am testing these codes, hope it work Big Grin | :-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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 BillWoodruff 108
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 21 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100