C
C#2y ago
SupsXito

✅ C# null exception with SQL

I was trying to deploy my application to another computer to try it, and I got an error where it couldn't located the attached mdf file so i fixed the connection string in visual studio and I started getting "Object reference not set to an instance of an object" in the application and the error : System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=DoumaClientDatabase StackTrace: at Douma_Client_Database.MainWindow.FillDataGrid()MainWindow.xaml.cs:line 82 my code should be here : https://paste.mod.gg/uipqjqrtgsij/0
BlazeBin - uipqjqrtgsij
A tool for sharing your source code with the world!
52 Replies
Pobiega
Pobiega2y ago
Please post your code on $code instead
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
SupsXito
SupsXitoOP2y ago
I posted it on https://paste.mod.gg/uipqjqrtgsij/0 because its a little too long
BlazeBin - uipqjqrtgsij
A tool for sharing your source code with the world!
Pobiega
Pobiega2y ago
Ok,try setting a breakpoint in FillDataGrid and stepping through
SupsXito
SupsXitoOP2y ago
now the exception was thrown here
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string CmdString = string.Empty;
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string CmdString = string.Empty;
Pobiega
Pobiega2y ago
Ok, likely it's that the indexing failed Ie, your configuration didn't find any connStr with that name ConfigurationMmanager always makes me think of .net framework
SupsXito
SupsXitoOP2y ago
why now? lol i didn't modify a single thing except the connection string when connecting to the database
Pobiega
Pobiega2y ago
Well there isn't much else than could be null in that scenario
SupsXito
SupsXitoOP2y ago
any suggested solution?
Pobiega
Pobiega2y ago
Try splitting that instruction into two parts to verify So we know for sure that's the problem
SupsXito
SupsXitoOP2y ago
can you elaborate?
Pobiega
Pobiega2y ago
On phone so hard to write code, do var constr = configmanager.connstr[...]; Then access the .connectionString property in a second instruction That lets you step through and see what the configmanager returned
SupsXito
SupsXitoOP2y ago
No description
SupsXito
SupsXitoOP2y ago
i got this in the previous window which is the login
Pobiega
Pobiega2y ago
Did you try to step over? The problem is in that class, so I'm not sure why you have a breakpoint outside it
SupsXito
SupsXitoOP2y ago
still the same error I think that's just the exception sign
Pobiega
Pobiega2y ago
But I asked you to put a breakpoint in FillDataGrid Did you do that?
SupsXito
SupsXitoOP2y ago
yes
Pobiega
Pobiega2y ago
That means it's something else throwing your null exceptio Set a breakpoint in the constructor Step through the entire thing until you find it
SupsXito
SupsXitoOP2y ago
it keeps referencing the constructor and the filldatagrid in the call stack thats it i've been at it for almost 5 hours now and couldn't find a specific cause
Pobiega
Pobiega2y ago
That's why I'm telling you to step through with the debugger
SupsXito
SupsXitoOP2y ago
No description
Pobiega
Pobiega2y ago
Nah, you're not Listening
SupsXito
SupsXitoOP2y ago
i keep stepping through and it keeps showing me those 3 :' ) i press F10 and it cycles through them
Pobiega
Pobiega2y ago
Thats when the exception has already been thrown You need to find where it is null before you access it Ie, at what line where you when you pressed F10 and it threw Thats the line we care about
SupsXito
SupsXitoOP2y ago
the constructor
Pobiega
Pobiega2y ago
So set a breakpoint in the ctor You're clearly jumping over the entire thing, and something in it is throwing
SupsXito
SupsXitoOP2y ago
man, sorry i'm exhausted and I have to go back home from work.
Pobiega
Pobiega2y ago
Im at a computer now, if you have 5 more min I'm sure we can fix it if you screenshare
SupsXito
SupsXitoOP2y ago
yeah sure! that would be a huge help
Pobiega
Pobiega2y ago
#dev-vc-1
SupsXito
SupsXitoOP2y ago
just a sec
Pobiega
Pobiega2y ago
would you be able to undeafen? so I can speak or should we do text
SupsXito
SupsXitoOP2y ago
I neither have a headphone nor a microphone right now : c
Pobiega
Pobiega2y ago
ok just screenshare then
SupsXito
SupsXitoOP2y ago
for some reason its not allowing me to would you be able to call in an hour?
Pobiega
Pobiega2y ago
¯\_(ツ)_/¯ maybe, maybe not
SupsXito
SupsXitoOP2y ago
sorry I dont know why its not letting me and we're kind of closing the building so i have to leave
Pobiega
Pobiega2y ago
not a problem I'm just not comitting to being here in an hour :p Ping me and if I am, I'll help if not.. well, then I'm not 🙂
SupsXito
SupsXitoOP2y ago
alright, thank you, and i apologise
joren
joren2y ago
@mammothcrytie@pobiega He's pressing F10 which is stepping over, instead of F11 which would step into the place where the exception is
Pobiega
Pobiega2y ago
well yes, but if there was a BP inside the ctor, it would break there too which is what I was saying
joren
joren2y ago
Yeah, that'd be easier if he cant figure out how to step through the code properly
SupsXito
SupsXitoOP2y ago
@pobiega hey there!
Pobiega
Pobiega2y ago
heya
SupsXito
SupsXitoOP2y ago
are you available ?
Pobiega
Pobiega2y ago
sure for a few minutes
SupsXito
SupsXitoOP2y ago
alright sure i can hear you but I can't seem to find a microphone
Pobiega
Pobiega2y ago
its fine
SupsXito
SupsXitoOP2y ago
Okay thank you so much and im sorry for bothering you, good night!
Pobiega
Pobiega2y ago
$close 🙂
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered

Did you find this page helpful?