Click here to Skip to main content
15,844,155 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Assistant_Accountant_V1.0
  StackTrace:
   at Assistant_Accountant_V1._0.General.ApplicationSetting.ConnectionString() in E:\C# Project\Assistant_Accountant_V1.0\Assistant_Accountant_V1.0\General\ApplicationSetting.cs:line 13
   at Assistant_Accountant_V1._0.LoginForm.LoginButton_Click(Object sender, EventArgs e) in E:\C# Project\Assistant_Accountant_V1.0\Assistant_Accountant_V1.0\Screens\LoginForm.cs:line 30
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Assistant_Accountant_V1._0.Program.Main() in E:\C# Project\Assistant_Accountant_V1.0\Assistant_Accountant_V1.0\Program.cs:line 18


What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Assistant_Accountant_V1._0.General
{
public class ApplicationSetting
{
public static string ConnectionString()
{
return ConfigurationManager.ConnectionStrings["rbx"].ConnectionString;
}

}
}
Posted
Updated 1-Jul-19 5:00am

Check for null

C#
public static string ConnectionString()
{
  var cs = ConfigurationManager.ConnectionStrings["rbx"].ConnectionString;
  if (cs != null) { return cs.ToString(); }
  else { throw new Exception("Connection string not found"); }
}
 
Share this answer
 
v2
Comments
zakir7dipu 29-Jun-19 11:20am    
In the app.config file I do <add name="rbx" connectionstring="">
and then In the ApplicationSetting class I writ the previous code. But when I run that program in the debugger at VS It shows the error on This line "return ConfigurationManager.ConnectionStrings["rbx"].ConnectionString;". now what will your solution. And your given answer also is not working. and showing the same ERROR
MadMyche 1-Jul-19 9:34am    
Try the "Improve Question" widget and edit your question to also include your app.config file...
I did notice I had a typo in my answer; missed the semi-colon key. Has been updated
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

In this case it's almost certainly that ConfigurationManager.ConnectionStrings["rbx"] is returning null to indicate that there is no string called "rbx" in the store, so you will need to find out why. If may be that you never added one, it may be that you spelled it wrong - we can't tell from here!
 
Share this answer
 
Comments
zakir7dipu 29-Jun-19 11:14am    
In the app.config file I do <add name="rbx" connectionstring="">
and then In the ApplicationSetting class I writ the previous code. But when I run that program in the debugger at VS It shows the error on This line "return ConfigurationManager.ConnectionStrings["rbx"].ConnectionString;". now what will your solution.
OriginalGriff 29-Jun-19 11:24am    
As I said: use the debugger. I can't use it for you - it needs your code running along with your config file to work out the problem, and we have no access to either.
From the last line of your included code - the beginning (often) of the error that propagates through all those other references:
at Assistant_Accountant_V1._0.Program.Main() in E:\C# Project\Assistant_Accountant_V1.0\Assistant_Accountant_V1.0\Program.cs:line 18
This reference may well show you what has the problem at runtime. Did you give this a look?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900