0

I am having the list X with some string and null value . I am iterating the foreach loop to bind the value to the textbox. If I get any null values in my list X the foreach loop get terminated and getting the null exception how to handle it.

I am checking the condition inside the foreach loop, but I think it's not correct logically.

SPList _listObj = web.Lists[new Guid(listID)];
            SPListItem item = _listObj.GetItemById(Convert.ToInt32(itemID));
           foreach (SPField field in _listObj.Fields)
            {
                if (field.Title != Null)
                {  //do some code}}
4
  • 2
    can you be more precise... the code your provide is not a valid C# code... I don't understand what goes wrong Commented Aug 4, 2011 at 7:06
  • please paste in the exact exception message, just saying "I get an exception" is not enough information. Commented Aug 4, 2011 at 7:21
  • yeah x is null how to handle it. Commented Aug 4, 2011 at 7:28
  • Code above has the last 2 brackets commented out. Obviously shouldn't be the case or it wouldn't compile. We'll just assume you didn't want to paste the irrelevancies, though, and know this. Commented Feb 6, 2020 at 19:11

7 Answers 7

5

Try below code:

foreach(var x in Lists.Where(x => x.fiels != null))
{

}
Sign up to request clarification or add additional context in comments.

Comments

3

Why don't you use it like this with null-coalescing operator

   foreach (var item in feeList ?? new List<FeeBusiness>())
   {
           // your code
   }

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

Comments

0

That code looks pretty suspicious to me.

Firstly, do you really have a list of lists? If so, I'd imagine you have to iterate over each element in the inner list as well:

foreach(List list in Lists)
{
    foreach (var x in list)
    {
        if (x.fields != null)
            // blah
        else
            // blah
    }
}

Secondly, are you sure that the Lists variable doesn't contain any nulls? Possibly it's actually x which is null, and that's the cause of your Null Reference Exception:

foreach(List x in Lists)
{
    if (x != null && x.fields != null)
        // blah
    else
        // blah
}

Comments

0

The code provided is not correct. I suppose you want to check X for Null in foreach loop. If this is logically correct or not, instead only you may know as logic goes beyond the code provided and depends on where you actually use it.

I personally don't find nothing bad to check for nulls in foreach loop.

You also, for example, can use Linq to query first for Null values and after Non Null values. The matter of design choice.

Regards.

Comments

0

List x in Lists? You probably mean to do:

foreach(string x in listvar){
    if(x != null)
       // do something
}

And are the strings actually null or just empty? That is a difference.

foreach(string x in listvar){
    if(x != "")
       // do something
}

I suspect that the problem is in your incorrect implementation of the foreach loop which causes to pop null errors as the objects inside the loop do not exist.

Comments

0
string delimitedvalues = null;//"11,22,33";
foreach(var str in (delimitedvalues?? string.Empty).split(','))
{
    string testvalue = "Test Value" + str;
}

Hope the above construct is useful!

Comments

0

You have to make sure that your object you are getting doesn't come back as null (your list, _listObj) before you ever iterate its fields. Even if you are certain the GUID you are passing in matches the list you are trying to get, you should be checking that object for being null, and checking for the number of fields, and if you get an item for the ID you are passing in:

SPList _listObj = web.Lists[new Guid(listID)];
if (_listObj != null)  // do we have a list object?
{
    if (_listObj.Fields.Count > 0) // do we have list columns on the list?
    {
        SPListItem item = _listObj.GetItemById(Convert.ToInt32(itemID));
        if (item != null) // did our item ID get a hit?
        {
            foreach (SPField field in _listObj.Fields)
            {
                if (field.Title != null)  // need lower case null, here
                {  
                    //do some code
                }
            }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.