C
C#•3y 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
WaffenOP•3y ago
UPD: var stafftemp = temp[0]; returned me my item
Waffen
WaffenOP•3y ago
Some additional screenshots to prove, that i have right id in args.User.ID
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Waffen
WaffenOP•3y ago
I know, i did this variant for debug
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Waffen
WaffenOP•3y ago
System.NullReferenceException
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Waffen
WaffenOP•3y ago
Wait a second
Waffen
WaffenOP•3y ago
Wtf? It just worked now, but i didn't change anything
Waffen
WaffenOP•3y ago
But third line still not working
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Waffen
WaffenOP•3y ago
after i commented out three lines it broke down again :D
Waffen
WaffenOP•3y ago
How does it work?)
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
dancepanda42
dancepanda42•3y ago
Maybe the error is in the DatabaseContext class. How is the data provided there?
phaseshift
phaseshift•3y ago
are you sure args & User are not null?
dancepanda42
dancepanda42•3y 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
WaffenOP•3y 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
WaffenOP•3y ago
🤨
dancepanda42
dancepanda42•3y 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
WaffenOP•3y ago
ReSharper
dancepanda42
dancepanda42•3y ago
It looks to me like resharper shows the error but does not occur in runtime.
Waffen
WaffenOP•3y 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•3y 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
WaffenOP•3y ago
It's LinQ. It's can't contains ?
phaseshift
phaseshift•3y 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
WaffenOP•3y ago
Yes, i have a check staffMember == null and because of this error, this check is skipped
Waffen
WaffenOP•3y ago
Waffen
WaffenOP•3y ago
Because staffMember is not null it is just nothing Idk how does it work
phaseshift
phaseshift•3y ago
show the call stack of the exception
Waffen
WaffenOP•3y ago
I put this line in try catch but C# just skips this error and doesn't catch it
phaseshift
phaseshift•3y ago
then it's not an error Show the stack trace If you can't, you're chasing hallucinations
Waffen
WaffenOP•3y ago
How to get stack trace from this without try catch? Sorry i just really don't know
phaseshift
phaseshift•3y ago
$debug
MODiX
MODiX•3y 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•3y ago
the stack trace is a property on the exception
phaseshift
phaseshift•3y 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•3y ago
If there's an exception the debugger stops. You dont need try/catch to see it
Waffen
WaffenOP•3y ago
Still not working :(
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Waffen
WaffenOP•3y 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•3y ago
Message Not Public
Sign In & Join Server To View
Waffen
WaffenOP•3y 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•3y 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
WaffenOP•3y ago
But i see this 'debugger evaluation exception' only in this place and my variable staffMember is empty only here...
Waffen
WaffenOP•3y ago
phaseshift
phaseshift•3y ago
you dont know what the value is if the locals evaluation is not working What happens after the if (staffMember == null) line?
Waffen
WaffenOP•3y ago
Just an discord answer and it's just skip this line with this message:
phaseshift
phaseshift•3y ago
lets ignore all the orange stuff - I dont trust it. You're still on that line. What happens next?
Waffen
WaffenOP•3y ago
This if just skipped
phaseshift
phaseshift•3y ago
so it's not null So theres no problem
Waffen
WaffenOP•3y ago
I don't see what's inside this variable
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Waffen
WaffenOP•3y ago
Ok, but why i can get and see this staffMember in another part of my code
Waffen
WaffenOP•3y 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•3y ago
well if it works in one place, but not in another, then look at the differences
Waffen
WaffenOP•3y ago
But there is no diffrerence...
Waffen
WaffenOP•3y ago
Doesn't work:
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
phaseshift
phaseshift•3y ago
it doesnt throw in actuality It's just the debugger evaluation
Unknown User
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
phaseshift
phaseshift•3y ago
Anyway, I'm bored of saying the same thing and waffle showing the same 'problem' that has been explained
Want results from more Discord servers?
Add your server