Reference: Object reference not set to an instance of an object.
-
Hi, I have an exception in my application if I click the button Mysql the combobox loaded with mysql databases names and I select name of the database if I click the button Sql server the combobox loaded with sql server databases names and I select name of the database when I click on the button mysql the connection is done and when I change the click on the button sql server, i have an exception this is the code:
//IMPORT DATA FROM Mysql DataBase
private void btnMySQL\_Click(object sender, RoutedEventArgs e) { mode = 1; // i used this var to defferentiate between btnMySQL and btnSQLserver combdb.ItemsSource = ConnexionMysql.GetDataBasesNames(); }
//IMPORT DATA FROM SqlServer DataBase
private void btnSQLserver_Click(object sender, RoutedEventArgs e)
{
mode = 2;
combdb.ItemsSource = ConnexionSqlServer.GetDataBasesNames();
}private void combdb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (mode)
{
case 1://Connexion with Mysql database
{
datasource = combdb.SelectedItem.ToString();if (ConnexionMysql.getConnexion(datasource))// Object reference not set to an instance of an object. { traitement...... } else { MessageBox.Show("erreur"); } } break; case 2://Connexion with Sql Server database { datasource = combdb.SelectedItem.ToString(); if (ConnexionSqlServer.getConnexion(datasource)) { ................. } else { MessageBox.Show("erreur"); } } break; } }
-
Hi, I have an exception in my application if I click the button Mysql the combobox loaded with mysql databases names and I select name of the database if I click the button Sql server the combobox loaded with sql server databases names and I select name of the database when I click on the button mysql the connection is done and when I change the click on the button sql server, i have an exception this is the code:
//IMPORT DATA FROM Mysql DataBase
private void btnMySQL\_Click(object sender, RoutedEventArgs e) { mode = 1; // i used this var to defferentiate between btnMySQL and btnSQLserver combdb.ItemsSource = ConnexionMysql.GetDataBasesNames(); }
//IMPORT DATA FROM SqlServer DataBase
private void btnSQLserver_Click(object sender, RoutedEventArgs e)
{
mode = 2;
combdb.ItemsSource = ConnexionSqlServer.GetDataBasesNames();
}private void combdb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (mode)
{
case 1://Connexion with Mysql database
{
datasource = combdb.SelectedItem.ToString();if (ConnexionMysql.getConnexion(datasource))// Object reference not set to an instance of an object. { traitement...... } else { MessageBox.Show("erreur"); } } break; case 2://Connexion with Sql Server database { datasource = combdb.SelectedItem.ToString(); if (ConnexionSqlServer.getConnexion(datasource)) { ................. } else { MessageBox.Show("erreur"); } } break; } }
-
Did you create a MySql-connection? ConnexionMysql would be empty according to the message.
Bastard Programmer from Hell :suss:
yes i created mysql connexion in a class //if (ConnexionMysql.getConnexion(datasource))
-
Hi, I have an exception in my application if I click the button Mysql the combobox loaded with mysql databases names and I select name of the database if I click the button Sql server the combobox loaded with sql server databases names and I select name of the database when I click on the button mysql the connection is done and when I change the click on the button sql server, i have an exception this is the code:
//IMPORT DATA FROM Mysql DataBase
private void btnMySQL\_Click(object sender, RoutedEventArgs e) { mode = 1; // i used this var to defferentiate between btnMySQL and btnSQLserver combdb.ItemsSource = ConnexionMysql.GetDataBasesNames(); }
//IMPORT DATA FROM SqlServer DataBase
private void btnSQLserver_Click(object sender, RoutedEventArgs e)
{
mode = 2;
combdb.ItemsSource = ConnexionSqlServer.GetDataBasesNames();
}private void combdb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (mode)
{
case 1://Connexion with Mysql database
{
datasource = combdb.SelectedItem.ToString();if (ConnexionMysql.getConnexion(datasource))// Object reference not set to an instance of an object. { traitement...... } else { MessageBox.Show("erreur"); } } break; case 2://Connexion with Sql Server database { datasource = combdb.SelectedItem.ToString(); if (ConnexionSqlServer.getConnexion(datasource)) { ................. } else { MessageBox.Show("erreur"); } } break; } }
Stick a breakpoint on the line that's raising the exception. Run the application, and follow the steps necessary to get to that point. When the debugger breaks on that line, hover the mouse over ConnectionMysql and see if it's null or not. If it's not null, step into getConnexion and step through that code. This is debugging 101. How come you haven't tried this yourself?
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Hi, I have an exception in my application if I click the button Mysql the combobox loaded with mysql databases names and I select name of the database if I click the button Sql server the combobox loaded with sql server databases names and I select name of the database when I click on the button mysql the connection is done and when I change the click on the button sql server, i have an exception this is the code:
//IMPORT DATA FROM Mysql DataBase
private void btnMySQL\_Click(object sender, RoutedEventArgs e) { mode = 1; // i used this var to defferentiate between btnMySQL and btnSQLserver combdb.ItemsSource = ConnexionMysql.GetDataBasesNames(); }
//IMPORT DATA FROM SqlServer DataBase
private void btnSQLserver_Click(object sender, RoutedEventArgs e)
{
mode = 2;
combdb.ItemsSource = ConnexionSqlServer.GetDataBasesNames();
}private void combdb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
switch (mode)
{
case 1://Connexion with Mysql database
{
datasource = combdb.SelectedItem.ToString();if (ConnexionMysql.getConnexion(datasource))// Object reference not set to an instance of an object. { traitement...... } else { MessageBox.Show("erreur"); } } break; case 2://Connexion with Sql Server database { datasource = combdb.SelectedItem.ToString(); if (ConnexionSqlServer.getConnexion(datasource)) { ................. } else { MessageBox.Show("erreur"); } } break; } }
When the selection in a ComboBox changes, you get TWO (2) SelectionChanged events: - the originally selected item gets unselected, and nothing is selected - the new item gets selected Therefore always check that
combdb.SelectedItem
is not nothing before you use it! -
yes i created mysql connexion in a class //if (ConnexionMysql.getConnexion(datasource))
Not in the ConnexionMysql class, I hope? The
ConnexionMysql
object needs to exist (new ConnexionMysql) before you call any non-static methods. I suggest you create the connection when it's needed, using the standard pattern; create a IDbConnection, create an IDbCommand, open, execute.Bastard Programmer from Hell :suss:
-
Not in the ConnexionMysql class, I hope? The
ConnexionMysql
object needs to exist (new ConnexionMysql) before you call any non-static methods. I suggest you create the connection when it's needed, using the standard pattern; create a IDbConnection, create an IDbCommand, open, execute.Bastard Programmer from Hell :suss:
the connexionmysql is a static class witch contain the connection with database with the method"getconnexion" and function "getdatabasenames"
-
the connexionmysql is a static class witch contain the connection with database with the method"getconnexion" and function "getdatabasenames"
-
Can you post the code of that method? That's where the error will be originating from :)
Bastard Programmer from Hell :suss:
He should be debugging it, not posting it. It's not a difficult thing for him to do.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Stick a breakpoint on the line that's raising the exception. Run the application, and follow the steps necessary to get to that point. When the debugger breaks on that line, hover the mouse over ConnectionMysql and see if it's null or not. If it's not null, step into getConnexion and step through that code. This is debugging 101. How come you haven't tried this yourself?
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
combdb.SelectedItem=null
-
combdb.SelectedItem=null
And there's your problem. If this is null, don't attempt to execute the code.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
He should be debugging it, not posting it. It's not a difficult thing for him to do.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
He should be debugging it, not posting it. It's not a difficult thing for him to do.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
I stick a breakpoint on the line that's raising the exception, and i get combobox item=null
-
And there's your problem. If this is null, don't attempt to execute the code.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
i Stick a breakpoint on the line combdb.ItemsSource = ConnexionSqlServer.GetDataBasesNames() wich is the method that fill the combobox, i get the exception on combobox although the method returns the names of the database when I test every single button while it works but the problem is when I migrate from one button to another
-
When the selection in a ComboBox changes, you get TWO (2) SelectionChanged events: - the originally selected item gets unselected, and nothing is selected - the new item gets selected Therefore always check that
combdb.SelectedItem
is not nothing before you use it!i Stick a breakpoint on the line combdb.ItemsSource = ConnexionSqlServer.GetDataBasesNames() wich is the method that fill the combobox, i get the exception on combobox although the method returns the names of the database when I test every single button while it works but the problem is when I migrate from one button to another
-
i Stick a breakpoint on the line combdb.ItemsSource = ConnexionSqlServer.GetDataBasesNames() wich is the method that fill the combobox, i get the exception on combobox although the method returns the names of the database when I test every single button while it works but the problem is when I migrate from one button to another
When you update the contents of a ComboBox, you cannot always assume that the SelectedIndex will get set to a valid item; often it winds up set to -1; depending on the circumstances. You need some defensive code; e.g.
if ( cb.Count > 0 ){
if ( cb.SelectedIndex < 0 || cb.SelectedIndex >= cb.Count ) {
cb.SelectedIndex = 0;
} else {
... // Bail out.
}
}