-5

I have two Class MyObjectA and MyObjectB

public class MyObjectA    
{
    private int A_ID {get; set;}
    private int B_ID {get; set;}
    .
    .
    .
    .        
}

public class MyObjectB    
{
    private int B_ID {get; set;}
    .
    .
    .
    .

    public MyObjectB (int B_ID )
    {            
        DAL.GetMyObjectBFromDatabase(B_ID );
    }
}

Now what I am trying to do in my function is

public void DoSomething()
{

    List<MyObjectA> ListA = new List<MyObjectA>();
    List<MyObjectB> ListB = new List<MyObjectB>();

    ListA  = DAL.GetMyObjectAListFromDatabase();  

I get an error in the line below for null exception in x.B_ID. If i debug through it. ListA has count > 0 and every x.B_ID has values . but the minute the below code is executed ListA becomes null not sure why.

I REPEAT THERE IS NO NULL IN THE ListA. WHEN THE FOREACH IS EXECUTED ListA BECOMES NULL. I WASDoing THIS TO REPLACE C# FOREACH. MY LIST IS CORRECT FOR FOREACH TO WORK. sO MY QUESTION WAS WHY THIS IS HAPPENING

    ListA.ForEach(x => {
            ListB.Add(new MyObjectB(x.B_ID));
    });
}

All I am trying to do is looping through ListA to get B_ID and using B_ID to get ListB. As done in this linq. Linq style "For Each"

Please help me understand where I am going wrong

Community
  • 1
  • 1
DotNetBeginner
  • 412
  • 2
  • 15
  • 34
  • Either `x` is null or `x.B_ID` is null and the error is bubbling up from the `MyObjectB` constructor. Use the debugger to find out which is the case. – D Stanley Jan 13 '15 at 17:04
  • @RahulSingh: While obviously a solution, the problem here is the distinction between `ListA` being `null` vs. `x` being `null`. – Guvante Jan 13 '15 at 17:05
  • 1
    Without additional information (i.e. exceptions have call stack information...) there is no way to suggest anything short of standard "what is nullreff exception" duplicate. – Alexei Levenkov Jan 13 '15 at 17:10
  • "WHEN THE FOREACH IS EXECUTED ListA BECOMES NULL" That is nonsensical. Calling that method cannot possibly result in ListA being set to null. The only way that could happen is if you were doing a non-debug build and you were never using ListA again (and even then I would be skeptical). ForEach has no way to control the variable that contains a pointer to its parent. – Guvante Jan 13 '15 at 17:12
  • When I use C# foreach it works fine. but i was looking forward to not using foreach and use linq. Then i noticed this behaviour. hence the question why this is happening – DotNetBeginner Jan 13 '15 at 17:14
  • @RahulSingh what in my question was duplicate. Did you really understand the question or were you sitting in your high horse being a know it all.But in reality downright dumb. I suggest you reverse the damage you did – DotNetBeginner Jan 13 '15 at 17:22
  • @AlexeiLevenkov what in my question was duplicate. Did you really understand the question or were you sitting in your high horse being a know it all.But in reality downright dumb. I suggest you reverse the damage you did – DotNetBeginner Jan 13 '15 at 17:22
  • Unless `B_ID` is a reference object and `MyObjectB` uses it and set it to null, voiding `ListA` ?somehow??? you have framework issue because it's impossible. – Franck Jan 13 '15 at 17:32
  • @DotNetBeginner Note that even setting `ListA` to null immediately after `ListA.ForEach` call started (i.e. inside the delegate handling iteration) *does not* cause the exception. – Alexei Levenkov Jan 13 '15 at 17:36
  • (@DotNetBeginner and I'm sorry, I'm really dumb - I can't figure out how code you have not shown works). – Alexei Levenkov Jan 13 '15 at 17:39
  • @AlexeiLevenkov I believ i did explain what was going on. I do know that listA.foreach does not change ListA. And so i posted the questio where I explained what was going on here "I get an error in the line below for null exception in x.B_ID. If i debug through it. ListA has count > 0 and every x.B_ID has values . but the minute the below code is executed ListA becomes null not sure why." but i had to ALLCAPS when it was not clear to some ofyou u – DotNetBeginner Jan 13 '15 at 17:40
  • @DotNetBeginner - Do you think it would be significantly better to close the question as "does not provide enough information"? Since you know that `.ForEach` does not change value of list it is iterating (and I assume you understand that it uses setting variable to `null` will have no impact on `.ForEach`) I see no reason why you highlighting the fact that `ListA` at some point (clearly *after* `.ForEach` started) is becoming `null`. – Alexei Levenkov Jan 13 '15 at 17:49
  • Hold on, are your checking the count while the breakpoint is INSIDE the ForEach by any chance ? because that is an issue we get here as well but as soon as breakpoint pass linQ foreach (or any other linq queries) collection and other property are visible again. but this issue is very rare. once or twice a week tops – Franck Jan 13 '15 at 18:06
  • @DotNetBeginner Incredible. Lashing out and arguing with people who are trying to get enough info to answer the question. Some people are just more interested in arguing than they are with getting their question answered... you do realize everybody here is actually trying to help you, right? – tnw Jan 13 '15 at 18:27
  • @Franck I just started working on this new project. this project framework has certain limitation. linq being one of them. For some reason linq here fails all the time. I am not very familiar with the framework as I just started working. I was informed about it now. Guvante suggested solution worked but then it started breaking as well. so I am back to foreach again after loosing my wits for about 5 hours. – DotNetBeginner Jan 13 '15 at 20:36
  • Let me guess that `DAL.GetMyObjectAListFromDatabase()` is entity framework am i right ? if so add the tag entity to your question. I don't know entity. Also put your cursor on the `foreach` word and press F12 it will bring you to the definition. Make sure the class on top state `System.Collections.Generic.List` inside `mscorlib.dll`. If you do not have that you could end up on an extension someone custom built on `List<>` and that would be problematic. – Franck Jan 13 '15 at 20:51
  • @Franck No we are using plain old ADO.net. I never had issues such as these when working with Entity framework. I haven't looked at the framework very closely yet, as i have only been on this project for 3 weeks. I was discussing this with my team and I think its about time this project coding standards meets 2015. – DotNetBeginner Jan 13 '15 at 21:05

1 Answers1

1

Your NullReferenceException is caused by x being null, that means a member of ListA is null, not that ListA itself is null.

For instance if you had called the following 3 lines instead of ListA = DAL.GetMyObjectAListFromDatabase();.

ListA.Add(new MyObjectA());
ListA.Add(null);
ListA.Add(new MyObjectA());

EDIT:

Looking at what you are doing again the correct tool for what you are doing is Select.

ListB = ListA.Select(x => new MyObject(x.B_ID)).ToList();
Guvante
  • 18,775
  • 1
  • 33
  • 64
  • There is no nulls in the listA. every element in listA has values. After executing foreach ListA becomes null. – DotNetBeginner Jan 13 '15 at 17:05
  • @DotNetBeginner: Then your attempt to anonymize the code is incorrect or the location of the `NullReferenceException` is wrong. Especially since [List.ForEach](http://msdn.microsoft.com/en-us/library/bwabdf9z\(v=vs.110\).aspx) does not modify the underlying list in any way. – Guvante Jan 13 '15 at 17:10
  • I dont understand. When I use C# foreach it works fine. but i was looking forward to not using foreach and use linq – DotNetBeginner Jan 13 '15 at 17:12
  • 3
    @DotNetBeginner: If you want to avoid `foreach` avoid it completely, use `ListB = ListA.Select(x => new MyObject(x.B_ID)).ToList();`. – Guvante Jan 13 '15 at 17:14
  • this works perfect. Thanks you. I am not sure why linq foreach was not working. what was messing it up – DotNetBeginner Jan 13 '15 at 17:20