C
C#•2y ago
Waffen

NullReferenceException on FirstorDefault()

var db = new DatabaseContext();

var staffMembers = db.StaffMember; // here i get right item 100%
var temp = staffMembers.Where(x => x.Id == args.User.Id).ToList(); // <-- this returns me my item
var staffMember = temp.FirstOrDefault(); // <-- System.NullReferenceException
var stafftemp = temp[0]; // <-- null without any reason
var db = new DatabaseContext();

var staffMembers = db.StaffMember; // here i get right item 100%
var temp = staffMembers.Where(x => x.Id == args.User.Id).ToList(); // <-- this returns me my item
var staffMember = temp.FirstOrDefault(); // <-- System.NullReferenceException
var stafftemp = temp[0]; // <-- null without any reason
Any ideas how to solve it?
62 Replies
Waffen
Waffen•2y ago
UPD: var stafftemp = temp[0]; returned me my item
Waffen
Waffen•2y ago
Some additional screenshots to prove, that i have right id in args.User.ID
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Waffen
Waffen•2y ago
I know, i did this variant for debug
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Waffen
Waffen•2y ago
System.NullReferenceException
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Waffen
Waffen•2y ago
Wait a second
Waffen
Waffen•2y ago
Wtf? It just worked now, but i didn't change anything
Waffen
Waffen•2y ago
But third line still not working
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Waffen
Waffen•2y ago
after i commented out three lines it broke down again :D
Waffen
Waffen•2y ago
How does it work?)
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
dancepanda42
dancepanda42•2y ago
Maybe the error is in the DatabaseContext class. How is the data provided there?
phaseshift
phaseshift•2y ago
are you sure args & User are not null?
dancepanda42
dancepanda42•2y ago
Yes, this could also cause it since temp was not yet executed at this point and the iteration is not executed until FirtstOrDefault. For testing just set args?.User?.Id
Waffen
Waffen•2y ago
Yes, I have previously uploaded screenshots showing that args.user and staff stats is not null I'm pretty sure the problem isn't with the database
Waffen
Waffen•2y ago
🤨
dancepanda42
dancepanda42•2y ago
This is a very strange behavior. Ohhh wait a minute. I just noticed that your code continues to run without problems past the NullReferenceException point. Which Visual Studio extension shows you this info on the side? Probably just the extension can not create a reference at development time and comes for this reason to the exception. If you make a try catch block around your code. Does it run into the catch block at all at some point?
Waffen
Waffen•2y ago
ReSharper
dancepanda42
dancepanda42•2y ago
It looks to me like resharper shows the error but does not occur in runtime.
Waffen
Waffen•2y ago
VS without resharper runs this code with same result. It's just skip this error (also with try cath) and i can see this error only in debug
dancepanda42
dancepanda42•2y ago
The last thing I can think of is that an element in the StaffMember is null. Try to enter a ? after the x. x?.Id == args.User.Id Otherwise, I have no further idea.
Waffen
Waffen•2y ago
It's LinQ. It's can't contains ?
phaseshift
phaseshift•2y ago
linq can Are you actually getting a problem? ie you can see an exception in the locals window, not just in those 'lens' stuff
Waffen
Waffen•2y ago
Yes, i have a check staffMember == null and because of this error, this check is skipped
Waffen
Waffen•2y ago
Waffen
Waffen•2y ago
Because staffMember is not null it is just nothing Idk how does it work
phaseshift
phaseshift•2y ago
show the call stack of the exception
Waffen
Waffen•2y ago
I put this line in try catch but C# just skips this error and doesn't catch it
phaseshift
phaseshift•2y ago
then it's not an error Show the stack trace If you can't, you're chasing hallucinations
Waffen
Waffen•2y ago
How to get stack trace from this without try catch? Sorry i just really don't know
phaseshift
phaseshift•2y ago
$debug
MODiX
MODiX•2y ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
phaseshift
phaseshift•2y ago
the stack trace is a property on the exception
phaseshift
phaseshift•2y ago
Visual Studio - Break On All Exceptions
Read about breaking on all exceptions in Visual Studio. This enables you to see the exact details of an exception instead of ignoring handled exceptions
phaseshift
phaseshift•2y ago
If there's an exception the debugger stops. You dont need try/catch to see it
Waffen
Waffen•2y ago
Still not working :(
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Waffen
Waffen•2y ago
Because i did it for debug
using System.Diagnostics.CodeAnalysis;

namespace StaffStatsBeta.Database;

public class StaffMembers
{
public ulong Id { get; set; }
public DateTime JoinDate { get; set; }

public List<OnlineLogs> OnlineLogs { get; set; }
public List<MainStats> MainStats { get; set; }
}
using System.Diagnostics.CodeAnalysis;

namespace StaffStatsBeta.Database;

public class StaffMembers
{
public ulong Id { get; set; }
public DateTime JoinDate { get; set; }

public List<OnlineLogs> OnlineLogs { get; set; }
public List<MainStats> MainStats { get; set; }
}
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Waffen
Waffen•2y ago
public class MainStats
{
public int Id { get; set; }

public DateTime Date { get; set; }

public StaffMembers StaffMembers { get; set;}

public int Tickets { get; set; }
public int Violations { get; set; }
public int Mutes { get; set; }
public int Warns { get; set; }
public int Bans { get; set; }
}

public class StaffMembers
{
public ulong Id { get; set; }
public DateTime JoinDate { get; set; }

public List<OnlineLogs> OnlineLogs { get; set; }
public List<MainStats> MainStats { get; set;}
}

public class OnlineLogs
{
public int Id { get; set; }
public StaffMembers StaffMembers { get; set; }

public DateTime EnterTime { get; set; }
public DateTime? OutTime { get; set; }

}

public class DatabaseContext : DbContext
{
public DbSet<StaffMembers> StaffMember { get; set; }
public DbSet<OnlineLogs> OnlineLogs { get; set; }
public DbSet<MainStats> MainStats { get; set; }

public DatabaseContext()
{
Database.EnsureCreated();
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

// TODO: redo this with json configuration and connectionStringBuilder
optionsBuilder.UseNpgsql("Host=localhost;Database=testdb;Username=postgres;Password=root");
}
}
public class MainStats
{
public int Id { get; set; }

public DateTime Date { get; set; }

public StaffMembers StaffMembers { get; set;}

public int Tickets { get; set; }
public int Violations { get; set; }
public int Mutes { get; set; }
public int Warns { get; set; }
public int Bans { get; set; }
}

public class StaffMembers
{
public ulong Id { get; set; }
public DateTime JoinDate { get; set; }

public List<OnlineLogs> OnlineLogs { get; set; }
public List<MainStats> MainStats { get; set;}
}

public class OnlineLogs
{
public int Id { get; set; }
public StaffMembers StaffMembers { get; set; }

public DateTime EnterTime { get; set; }
public DateTime? OutTime { get; set; }

}

public class DatabaseContext : DbContext
{
public DbSet<StaffMembers> StaffMember { get; set; }
public DbSet<OnlineLogs> OnlineLogs { get; set; }
public DbSet<MainStats> MainStats { get; set; }

public DatabaseContext()
{
Database.EnsureCreated();
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

// TODO: redo this with json configuration and connectionStringBuilder
optionsBuilder.UseNpgsql("Host=localhost;Database=testdb;Username=postgres;Password=root");
}
}
This is whole my databasecontext I am using EF Core for PostgreSQL btw
phaseshift
phaseshift•2y ago
that is showing a 'debugger evaluation exception'. It is not an exception from the code that ran... but since you're there, please expand args
Waffen
Waffen•2y ago
But i see this 'debugger evaluation exception' only in this place and my variable staffMember is empty only here...
Waffen
Waffen•2y ago
phaseshift
phaseshift•2y ago
you dont know what the value is if the locals evaluation is not working What happens after the if (staffMember == null) line?
Waffen
Waffen•2y ago
Just an discord answer and it's just skip this line with this message:
phaseshift
phaseshift•2y ago
lets ignore all the orange stuff - I dont trust it. You're still on that line. What happens next?
Waffen
Waffen•2y ago
This if just skipped
phaseshift
phaseshift•2y ago
so it's not null So theres no problem
Waffen
Waffen•2y ago
I don't see what's inside this variable
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Waffen
Waffen•2y ago
Ok, but why i can get and see this staffMember in another part of my code
Waffen
Waffen•2y ago
Oh, i've read again and you mean staffMembers. I am not trying to check what inside staffMembers, i am trying to check what inside staffMember which i get with FirstOrDefault()
phaseshift
phaseshift•2y ago
well if it works in one place, but not in another, then look at the differences
Waffen
Waffen•2y ago
But there is no diffrerence...
Waffen
Waffen•2y ago
Doesn't work:
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
phaseshift
phaseshift•2y ago
it doesnt throw in actuality It's just the debugger evaluation
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
phaseshift
phaseshift•2y ago
Anyway, I'm bored of saying the same thing and waffle showing the same 'problem' that has been explained