# False positive on C# - Null pointers should not be dereferenced

**URL:** <https://community.sonarsource.com/t/false-positive-on-c-null-pointers-should-not-be-dereferenced/14940>\
**Category:** Report False-positive / False-negative...\
**Tags:** csharp\
**Created:** [October 2, 2019, 7:20am UTC](https://community.sonarsource.com/t/false-positive-on-c-null-pointers-should-not-be-dereferenced/14940 "2019-10-02T07:20:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jose](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/jose/32/5064_2.png) [@Jose](https://community.sonarsource.com/u/Jose)\
**Post date:** [October 2, 2019, 7:20am UTC](https://community.sonarsource.com/t/false-positive-on-c-null-pointers-should-not-be-dereferenced/14940/1 "2019-10-02T07:20:16Z")

</div>

- SonarQube 7.9.1, SonarC# 7.16

It says that userEntity is null on at least one execution path

```auto
var userEntity = _usersDao.GetUsers(new List<string> { user.Email });
foreach (var storeCode in user.StoreCodes)
{
	var storeId = _userStoreDao.GetStoreId(storeCode);
	if (storeId > 0 && userEntity != null && userEntity.Count > 0)
	{
		_userStoreDao.InsertUserStore(userEntity.FirstOrDefault().IdUser, storeId);
		continue;
	}
	_userStoreDao.InsertPendingUserStore(ProjectId, user.Email, storeCode);
}

```

---

<div class="post-metadata">

**Author:** ![Andrei\_Epure](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/andrei_epure/32/77402_2.png) [@Andrei\_Epure](https://community.sonarsource.com/u/Andrei_Epure)\
**Post date:** [October 3, 2019, 11:46am UTC](https://community.sonarsource.com/t/false-positive-on-c-null-pointers-should-not-be-dereferenced/14940/2 "2019-10-03T11:46:04Z")

</div>

hi @Jose and welcome to the forum!

I cannot seem to repro your issue - on what line is it raising a problem?

What MSBuild version are you using?

---

<div class="post-metadata">

**Author:** ![Jose](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/jose/32/5064_2.png) [@Jose](https://community.sonarsource.com/u/Jose)\
**Post date:** [October 3, 2019, 1:32pm UTC](https://community.sonarsource.com/t/false-positive-on-c-null-pointers-should-not-be-dereferenced/14940/3 "2019-10-03T13:32:44Z")

</div>

Hello Andrei,

Versions used:  
SonarScanner for MSBuild 4.7.1

MSBuild 15.9

Please find attached the screenshot of the issue.

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/2X/5/53bf7c6cfd3adc2b8ff4148669311ba0a41a69d9.png)

---

<div class="post-metadata">

**Author:** ![Andrei\_Epure](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/andrei_epure/32/77402_2.png) [@Andrei\_Epure](https://community.sonarsource.com/u/Andrei_Epure)\
**Post date:** [October 3, 2019, 2:28pm UTC](https://community.sonarsource.com/t/false-positive-on-c-null-pointers-should-not-be-dereferenced/14940/4 "2019-10-03T14:28:09Z")

</div>

thanks! Could you please let me know what’s the type of `userEntity`?

---

<div class="post-metadata">

**Author:** ![Jose](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/jose/32/5064_2.png) [@Jose](https://community.sonarsource.com/u/Jose)\
**Post date:** [October 3, 2019, 2:45pm UTC](https://community.sonarsource.com/t/false-positive-on-c-null-pointers-should-not-be-dereferenced/14940/5 "2019-10-03T14:45:38Z")

</div>

Yeah, the type is: `IList<UserEntity>`

```auto
public class UserEntity
{
    [Column(DatabaseStructure.FieldUserIdUser)]
    public int IdUser { get; set; }

    [Column(DatabaseStructure.FieldUserCsIdUser)]
    public string CsIdUser { get; set; }

    [Column(DatabaseStructure.FieldUserIdRole)]
    public int IdRole { get; set; }
}

```

---

<div class="post-metadata">

**Author:** ![costin.zaharia](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/costin.zaharia/32/62600_2.png) [@costin.zaharia](https://community.sonarsource.com/u/costin.zaharia)\
**Post date:** [November 12, 2019, 7:31am UTC](https://community.sonarsource.com/t/false-positive-on-c-null-pointers-should-not-be-dereferenced/14940/7 "2019-11-12T07:31:48Z")

</div>

Hi @Jose

I’ve also tried to reproduce the problem with the following snippet but without any luck

```
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace Test
{
    public class S2259
    {
        public void Test(IUserStoreDao _userStoreDao)
        {
            var userEntity = GetUsers();
            foreach (var storeCode in new List<string> { "" })
            {
                var storeId = _userStoreDao.GetStoreId(storeCode);
                
                if (storeId > 0 && userEntity != null && userEntity.Count > 0)
                {
                    _userStoreDao.InsertUserStore(userEntity.FirstOrDefault().IdUser, storeId);
                    continue;
                }

                _userStoreDao.InsertPendingUserStore(1, "email", storeCode);
            }
        }

        private List<UserEntity> GetUsers() => new List<UserEntity>();
    }

    public interface IUserStoreDao
    {
        int GetStoreId(string code);

        void InsertUserStore(int userId, int storeId);

        void InsertPendingUserStore(int id, string email, string storeCode);
    }

    public class UserEntity
    {
        [Column("FieldUserIdUser")]
        public int IdUser { get; set; }

        [Column("FieldUserCsIdUser")]
        public string CsIdUser { get; set; }

        [Column("FieldUserIdRole")]
        public int IdRole { get; set; }
    }
}

```

Do you think it’s possible to create a small test project which reproduces the false positive and attach it to this thread?

Thank you,  
Costin

---

<div class="post-metadata">

**Author:** ![Jose](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/jose/32/5064_2.png) [@Jose](https://community.sonarsource.com/u/Jose)\
**Post date:** [November 13, 2019, 8:36am UTC](https://community.sonarsource.com/t/false-positive-on-c-null-pointers-should-not-be-dereferenced/14940/8 "2019-11-13T08:36:09Z")

</div>

Hello Andrei,

I tried to make an small project with the same code, but in that project the issue is not raising. Any suggestions?

Thanks!

---

<div class="post-metadata">

**Author:** ![Andrei\_Epure](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/andrei_epure/32/77402_2.png) [@Andrei\_Epure](https://community.sonarsource.com/u/Andrei_Epure)\
**Post date:** [November 28, 2019, 9:50am UTC](https://community.sonarsource.com/t/false-positive-on-c-null-pointers-should-not-be-dereferenced/14940/9 "2019-11-28T09:50:40Z")

</div>

> [@Jose](#):
>
> I tried to make an small project with the same code, but in that project the issue is not raising.

I am afraid that if you cannot have a reproducer, it is hard to make a suggestion or to act upon feedback.
