Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 18-Apr-13 5:51am
Comments
tumbledDown2earth at 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 at 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 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
vishvadeepak at 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 at 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 at 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 at 18-Apr-13 11:28am
   
Of course it can be, but you have to write your code in reasonable way.
—SA
vishvadeepak at 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
tumbledDown2earth at 18-Apr-13 12:13pm
   
Wait ... my psychic gut feeling is itching to ask you this ..
 
Why haven't you checked "comboBox1.SelectedValue" for null in the first line (if nothing is selected this will be null and comboBox1.SelectedValue.ToString() will throw null reference exception) ?
tumbledDown2earth at 18-Apr-13 12:15pm
   
try this :
 
if (comboBox1.SelectedValue != null && comboBox1.Text != "")
...
 
If you are still getting the same exception follow the advice by Sergey to get some logs
Sergey Alexandrovich Kryukov at 18-Apr-13 12:33pm
   
Please, not "", but string.Empty. The same results (strings are using intern pool), but any immediate constants are evil...
—SA
vishvadeepak at 18-Apr-13 12:39pm
   
ok but i am unable to understand, actually that the same code is running when i am debugging it in visual studio
Sergey Alexandrovich Kryukov at 18-Apr-13 12:43pm
   
Did you use my advice, in the updated answer, after [EDIT]? Do this, and you will see what's going on without Visual Studio.
—SA
vishvadeepak at 18-Apr-13 11:33am
   
see this code plz
// this is to detect that the item is selected or not
if (comboBox1.Text != "")
{
textBoxState.Text = comboBox1.SelectedValue.ToString();// i am filling the value of State according to city
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;
}// in this process actually i want to generate the next index for Customer Service Center (CSC) cause it is starting like this ETAW02 where ETAW are the first four letters of City and no. 2 is which i got after the increament in last index for new CSC
catch (Exception ex)
{
 
}
if (i <= 3)
{
textBoxCSCCode.Text = str + "0" +Convert.ToString(coutn1);

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

}
}
CPallini at 18-Apr-13 13:06pm
   
5.
Sergey Alexandrovich Kryukov at 18-Apr-13 15:31pm
   
Thank you, Carlo.
(Interesting, where OP with the problem is, at the moment? :-)
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 275
2 BillWoodruff 260
3 Shweta N Mishra 244
4 Deepu S Nair 230
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,818
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,755


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100