Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I developed an windows application.I used an background worker process in my code.
When i was working on my application suddenly it throws an error in program.cs file
Null reference exception was unhandled
Object reference not set to an instance of an object.

in application.run(); line how to fix this error.

[Edit- OPs code from his comment]
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;


namespace AISQBTool
{
    static class Program
    {
        ///
        /// The main entry point for the application.
        ///
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Login());
        }
    }
}
Posted
Updated 19-Feb-13 3:13am
v2
Comments
Richard MacCutchan 19-Feb-13 3:40am    
Find the reference that has not been initialised and correct the code. Without seeing the actual lines of code we cannot begin to guess what may be happening.
Jibesh 19-Feb-13 3:45am    
It's not the Application.Run. it might be somewhere in your Form class. Error like these are easy to identify and resolve. debug your application and check which part of the code these exceptions are throwing.
OriginalGriff 19-Feb-13 3:47am    
Without seeing the code which generated the error, we cannot help you.
Use the "Improve question" widget to edit your question and provide better information.
S. M. Ahasan Habib 19-Feb-13 4:35am    
Just post your code. You may debug the code and find out the line/object where object reference null found. If you identify object which is throwing null reference exception, hope you can solve it by your own.
[no name] 19-Feb-13 6:20am    
can you provide your program.cs code ovrr here..??

Hi Catch the exception and see where is the issue actually occured.

C#
[STAThread] 
static void Main() 
{ 
    try
    {
        Application.EnableVisualStyles(); 
        Application.SetCompatibleTextRenderingDefault(false); 
        Application.Run(new Login()); 
    }
    Catch (Exception ex)
    {
        Console.Out.WriteLine(ex.StackTrace);
    }
}


The stcktrace will tell you at which line in which file the code failed.

Regards
Jegan
 
Share this answer
 
Comments
Jegan Thiyagesan 20-Feb-13 9:08am    
Hithe stack trace is telling you that some unsafe native method is being called.

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 System.Windows.Forms.Application.Run(Form mainForm) at AISQBTool.Program.Main() in C:\Users\AISC01\Desktop\AISQBTool_11-2-13_integrated\AISQBTool_11-2-13_integrated\AISQBTool\Program.cs:line 22

Can you post some code from your "Login" class?
karteekboddu 20-Feb-13 9:28am    
This was the code from Login class

private void btnLogin_Click(object sender, EventArgs e)
{
try
{

string user = txtUser.Text.ToString();

//Constructing connection string from the inputs
if (user == "SYS" || user == "sys" || user == "sys as sysdba")
{
StringBuilder Con = new StringBuilder("Data Source=");
Con.Append(txtTns.Text);
Con.Append(";User ID=");
Con.Append(txtUser.Text);
Con.Append(";Password=");
Con.Append(txtPassword.Text);
Con.Append(";DBA Privilege=SYSDBA");
string strCon = Con.ToString();
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["con"].ConnectionString = strCon;
connectionStringsSection.ConnectionStrings["newcon"].ConnectionString = strCon;
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
// updateConfigFile(strCon);

ConfigurationManager.RefreshSection("connectionStrings");

}
else
{
StringBuilder Con1 = new StringBuilder("Data Source=");
Con1.Append(txtTns.Text);
Con1.Append(";User ID=");
Con1.Append(txtUser.Text);
Con1.Append(";Password=");
Con1.Append(txtPassword.Text);

string strCon1 = Con1.ToString();
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["con"].ConnectionString = strCon1;
connectionStringsSection.ConnectionStrings["newcon"].ConnectionString = strCon1;
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");

}
//QBHomep home = new QBHomep();
QueryBuilder home = new QueryBuilder();
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
home.Show();
this.Hide();
}
catch (Exception E)
{
MessageBox.Show(ConfigurationManager.ConnectionStrings["con"].ToString() + ".This is invalid connection", "Incorrect server/Database");
}
}

public void updateConfigFile(string con)
{
//updating config file
XmlDocument XmlDoc = new XmlDocument();
//Loading the Config file
string path = "..\\..\\App.config";
XmlDoc.Load(path);
foreach (XmlElement xElement in XmlDoc.DocumentElement)
{
if (xElement.Name == "connectionStrings")
{
//setting the coonection string
xElement.FirstChild.Attributes[1].Value = con;
xElement.LastChild.Attributes[1].Value = con;

}
}
//writing the connection string in config file
XmlDoc.Save(path);


}

private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}


//protected override void OnPaintBackground(PaintEventArgs e)
//{
// using (LinearGradientBrush brush = ne
Jegan Thiyagesan 20-Feb-13 9:35am    
Where is the Login() constructor?, the code above looks like missing half the page.
karteekboddu 20-Feb-13 9:40am    
Sorry,
public Login()
{

InitializeComponent();
this.txtPassword.KeyPress += new KeyPressEventHandler(CheckKeys);
}

private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{

try
{
string user = txtUser.Text.ToString();

//Constructing connection string from the inputs
if (user == "SYS" || user == "sys" || user == "sys as sysdba")
{
StringBuilder Con = new StringBuilder("Data Source=");
Con.Append(txtTns.Text);
Con.Append(";User ID=");
Con.Append(txtUser.Text);
Con.Append(";Password=");
Con.Append(txtPassword.Text);
Con.Append(";DBA Privilege=SYSDBA");
string strCon = Con.ToString();
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["con"].ConnectionString = strCon;
connectionStringsSection.ConnectionStrings["newcon"].ConnectionString = strCon;
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
// updateConfigFile(strCon);

ConfigurationManager.RefreshSection("connectionStrings");

}
else
{
StringBuilder Con1 = new StringBuilder("Data Source=");
Con1.Append(txtTns.Text);
Con1.Append(";User ID=");
Con1.Append(txtUser.Text);
Con1.Append(";Password=");
Con1.Append(txtPassword.Text);

string strCon1 = Con1.ToString();
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["con"].ConnectionString = strCon1;
connectionStringsSection.ConnectionStrings["newcon"].ConnectionString = strCon1;
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");

}
//QBHomep home = new QBHomep();
QueryBuilder home = new QueryBuilder();
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
home.Show();
this.Hide();
}
catch (Exception E)
{
MessageBox.Show(ConfigurationManager.ConnectionStrings["con"].ToString() + ".This is invalid connection", "Incorrect server/Database");
}
}
Jegan Thiyagesan 20-Feb-13 9:52am    
Can you put a break point in "InitialiseComponent()" and run the app, If it does stops at that break point, press the F11 key to step into that method and continue F11 until the exception throws, note down the line that the exception throwed. If the Break point doesn't get hit, then let us know.
We're getting no where fast here without seeing any code. Try the following...

1. Create a new application
2. Add a Login form - do not use the Login form from the program you're having the problem with
3. Add all of the controls you need onto that form but do not add any other code
4. Run the new program and confirm that it does not throw the exception
5. Add any other code you have in the original Login form one bit at a time
6. After adding each code segment run the program to confirm it does not throw the exception
- If it does throw an exception then you know exactly which piece of code is causing the problem.
7. If it doesn't throw an exception then you need to compare the Form1.Designer.cs files from both projects (and possibly Form1.cs also) - when you find the differences then you will know where the problem lies and should be able to solve the problem
 
Share this answer
 
Check Class file of your database, Check variable Spellings and Datatype of Varaible with its respective property.

Or another way is,
you must be passing null values into the database which is NOT NULL.
 
Share this answer
 
Comments
CHill60 20-Feb-13 8:38am    
I don't think he would be getting an exception mentioning DataGridView if it was a database issue?

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