Click here to Skip to main content
15,885,098 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am getting null reference exception wheneevr i am trying to click on button or trying to insert the data from windows form to sql. This problem is being occured in each form.Plz provide me solution asap.
Posted
Comments
tumbledDown2earth 18-Apr-13 10:53am    
Your question is quite vague unless you discuss with some code. There could be a zillion reason what goes on from you buttonclick to the data going in the db.
Sergey Alexandrovich Kryukov 18-Apr-13 10:57am    
You are right. Nevertheless, even if inquirers give more detail, I often answer the same way. This is because the debugging of such cases is much easier than others. So, I answered even in this case.

Please see my answer.
—SA
OriginalGriff 18-Apr-13 10:56am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Show us the code fragment that generates the exception!
Use the "Improve question" widget to edit your question and provide better information.
vishvadeepak 18-Apr-13 11:02am    
actually when i am running this on visual studio its working fine but when i am deploying this on another machine i am getting this exception. and the exception is like below :
------------------------

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at PAIINDIA.Account.CreateCSCMaster.comboBox1_SelectedIndexChanged(Object sender, EventArgs e)
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
PAIINDIA
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Program%20Files/Vaishnodevi%20Technologies/PaiIndiaSoftware/PAIINDIA.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Data
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Data.DataSetExtensions
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Data.DataSetExtensions/v4.0_4.0.0.0__b77a5c561934e089/System.Data.DataSetExtensions.dll
----------------------------------------
System.Numerics
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
----------------------------------------
System.Transactions
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
CodeBase: file:///C:/Win

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

[EDIT]

I understand the difficulty explained in the comments to this answer. In more complex cases, there is a more radical technique.

Catch all exceptions of the very top stack frame of each thread. In other words, catch everything. When handle the exception (only one place per thread), get the exceptions stack trace: http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

This is just a string. Write this string into the system log using System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

After running the application, read the log. The exception trace will show you all the path where the exception was propagated. Get to the code again, to find ends.

So far, it always worked out.

Good luck,
—SA
 
Share this answer
 
v2
Comments
vishvadeepak 18-Apr-13 11:04am    
actually it comes only when i deploy this project in another system it doesn't come till then when i am running in visual studio,and it is coming in entire project so i dont think so that this gonna be solved to use the breakpoint
tumbledDown2earth 18-Apr-13 11:10am    
Its really not possible unless at lest we see the code inside "comboBox1_SelectedIndexChanged" .. Please make a dummy class to reproduce the problem and paste the code in your question.

Sergey's suggesstion above could also help if you try
Sergey Alexandrovich Kryukov 18-Apr-13 11:34am    
Thank you. There is more radical way to find ends. Please see my updated answer, after [EDIT].
—SA
Sergey Alexandrovich Kryukov 18-Apr-13 11:28am    
Of course it can be, but you have to write your code in reasonable way.
—SA
vishvadeepak 18-Apr-13 11:13am    
if (comboBox1.Text != "")
{
textBoxState.Text = comboBox1.SelectedValue.ToString();
string str = comboBox1.Text;
int i = str.Length;
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["PAIINDIA"].ConnectionString;
con.Open();
SqlCommand cmd = con.CreateCommand();
SqlCommand cmd1 = con.CreateCommand();
int coutn1=0;
try
{
cmd.CommandText = "select count(Distric) from CreateCSC where NameOfFirm='" + nameoffirm + "' and FinancialYear='" + financialyear + "' and Distric='" + comboBox1.Text + "'";
string count = cmd.ExecuteScalar().ToString();
coutn1 = Convert.ToInt32(count);
coutn1 = coutn1 + 1;
}
catch (Exception ex)
{

}
if (i <= 3)
{
textBoxCSCCode.Text = str + "0" +Convert.ToString(coutn1);

}
else
{
textBoxCSCCode.Text = str.Substring(0, 4) +"0"+coutn1.ToString();

}
}
this is my code
In your code, I see two positions which are likely candidates for such an error:
1) textBoxState.Text = comboBox1.SelectedValue.ToString();
When the combobox has a text, it does not mean that it has a selected value. When you change the text, two events happen: one with no value being selected, and then an event with the new value selected.
2) con.ConnectionString = ConfigurationManager.ConnectionStrings["PAIINDIA"].ConnectionString
Did you provide that connection string on the other machine also? If not, ConfigurationManager.ConnectionStrings["PAIINDIA"] will be null and calling its ConnectionString property causes a NullReferenceException.
Also note that catch (Exception ex) { do nothing } is bad - you ought to log the exception at least to know what went wrong.
 
Share this answer
 

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