Ends in:
0 DAY
16 HRS
26 MIN
37 SEC
Ends in:
0 D
16 H
26 M
37 S
Open Closed

System.NullReferenceException: 'Object reference not set to an instance of an object.' #2301


User avatar
0
Rrader30 created
  • ABP Framework version: v4.3
  • UI type: Angular
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): Seperated
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

I have a background job where I am attempting to select records from a trap table. When the code gets to _list = query.ToList() I get the error System.NullReferenceException: 'Object reference not set to an instance of an object.' in List.cs. What am I doing wrong? Any ideas of why this error is occuring?

foreach (var row in values)
                {
                    IQueryable<Trap> queryable = await _trapsSheet.GetQueryableAsync();
                    var query = queryable.Where(x => x.Asset.Equals(ConvertInt64(row[0].ToString())));
                   
                    if(query.Any())
                    {
                        try
                        {
                            var _c1 = MapValues(row);

                            List<Trap> _list = new List<Trap>();
                            _list = query.ToList();
                            Trap _result = ObjectMapper(_c1, _list[0]);
                            mTrapExist.Add(_result);
                        }catch(Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                        }
                       
                    }
                    else
                    {
                        try
                        {
                            Trap _result = MapValues(row);
                            System.Diagnostics.Debug.WriteLine(_result.Asset.ToString());
                            mTrap.Add(_result);
                        
                        }catch(Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.ToString());
                        }
                        
                    }
                }

18 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Can you share the full error stack?

  • User Avatar
    0
    Rrader30 created

    This is all I get

    System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=Microsoft.EntityFrameworkCore.Relational StackTrace: at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable`1.Enumerator.Dispose()

  • User Avatar
    0
    Rrader30 created

    Maliming. I did. That is all it is giving me.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Can you check this (x => x.Asset.Equals(ConvertInt64(row[0].ToString())))

    Try to use constant expressions.

  • User Avatar
    1
    Rrader30 created

    I did and I am still getting the error.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Maybe the x.Asset is null. Can you try to include it? or ignore it?

  • User Avatar
    0
    Rrader30 created

    So I check the database and the records exist. Here is a snippet of the table.

  • User Avatar
    1
    Rrader30 created

    The funny part is, I can insert records just fine with this code. This is within the same class for the background job.

    try { Trap _result = MapValues(row); System.Diagnostics.Debug.WriteLine(_result.Asset.ToString()); mTrap.Add(_result);

    }catch(Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }

    if(mTrap.Count > 0) { await _trapsSheet.InsertManyAsync(mTrap, true); await uow.SaveChangesAsync(); }

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    https://docs.abp.io/en/abp/5.0/Entity-Framework-Core#explicit-lazy-loading https://docs.microsoft.com/en-us/ef/core/querying/related-data/eager

  • User Avatar
    0
    Rrader30 created

    Not sure I am understanding. Here is what I have

    private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IRepository<Trap, Guid> _trapsSheet; private readonly IObjectMapper _objectMapper; private readonly IEmailSender _emailSender; private readonly ITemplateRenderer _templateRenderer; private static readonly string[] Scopes = { SheetsService.Scope.Spreadsheets };

    public TrapLoader(IUnitOfWorkManager unitOfWorkManager, IRepository<Trap, Guid> traps, IObjectMapper objectMapper, IEmailSender emailSender, ITemplateRenderer templateRenderer) { _unitOfWorkManager = unitOfWorkManager; ** _trapsSheet = traps**; _objectMapper = objectMapper; _emailSender = emailSender; _templateRenderer = templateRenderer; }

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You need to include your navigation entity.

    https://support.abp.io/QA/Questions/2301#answer-c47f204b-9a58-626b-5c6a-3a00e7a19105

  • User Avatar
    0
    Rrader30 created

    I'll give that a try in the AM. I'll tell you know what happens.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    👍

  • User Avatar
    0
    Rrader30 created

    I went in and attempted to use .Include, but it appears .Include is not part of the IQueryable. A little background. I am using the IQueryable because when I inject the service and attempt to use the service directly in a background job I get Access denied because the user isnt authenticated, so instead I am accessing the data using the IRepository<Trap, Guid>. I have done this in other background jobs without any issues, but this one is throwing an exception and not sure why.

  • User Avatar
    0
    Rrader30 created

    So I regenerated the table with a different name with ABP SUITE and now things seem to be working. What I would like to understand is why did the original not work properly as it was generated with ABP Suite?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You can compare the code to see. I'm not sure.

  • User Avatar
    0
    Rrader30 created

    Since this is mainly ABP code. Where should I look and compare?

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You can check the difference in git. before and after.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19