Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reference: Object reference not set to an instance of an object.

Reference: Object reference not set to an instance of an object.

Scheduled Pinned Locked Moved C#
databasemysqlsql-serversysadmin
16 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M MemberDotNetting

    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;
            }
        }
    
    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    Did you create a MySql-connection? ConnexionMysql would be empty according to the message.

    Bastard Programmer from Hell :suss:

    M 1 Reply Last reply
    0
    • L Lost User

      Did you create a MySql-connection? ConnexionMysql would be empty according to the message.

      Bastard Programmer from Hell :suss:

      M Offline
      M Offline
      MemberDotNetting
      wrote on last edited by
      #3

      yes i created mysql connexion in a class //if (ConnexionMysql.getConnexion(datasource))

      L 1 Reply Last reply
      0
      • M MemberDotNetting

        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;
                }
            }
        
        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #4

        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

        M 1 Reply Last reply
        0
        • M MemberDotNetting

          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;
                  }
              }
          
          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #5

          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!

          M 1 Reply Last reply
          0
          • M MemberDotNetting

            yes i created mysql connexion in a class //if (ConnexionMysql.getConnexion(datasource))

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            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:

            M 1 Reply Last reply
            0
            • L Lost User

              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:

              M Offline
              M Offline
              MemberDotNetting
              wrote on last edited by
              #7

              the connexionmysql is a static class witch contain the connection with database with the method"getconnexion" and function "getdatabasenames"

              L 1 Reply Last reply
              0
              • M MemberDotNetting

                the connexionmysql is a static class witch contain the connection with database with the method"getconnexion" and function "getdatabasenames"

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                Can you post the code of that method? That's where the error will be originating from :)

                Bastard Programmer from Hell :suss:

                P 1 Reply Last reply
                0
                • L Lost User

                  Can you post the code of that method? That's where the error will be originating from :)

                  Bastard Programmer from Hell :suss:

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #9

                  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

                  L M 2 Replies Last reply
                  0
                  • P Pete OHanlon

                    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

                    M Offline
                    M Offline
                    MemberDotNetting
                    wrote on last edited by
                    #10

                    combdb.SelectedItem=null

                    P 1 Reply Last reply
                    0
                    • M MemberDotNetting

                      combdb.SelectedItem=null

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #11

                      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

                      M 1 Reply Last reply
                      0
                      • P Pete OHanlon

                        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

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        I don't see anything difficuly in creating a connection, but apparently, it is. I could say that he should be reading the documentation, but most people don't :)

                        Bastard Programmer from Hell :suss:

                        1 Reply Last reply
                        0
                        • P Pete OHanlon

                          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

                          M Offline
                          M Offline
                          MemberDotNetting
                          wrote on last edited by
                          #13

                          I stick a breakpoint on the line that's raising the exception, and i get combobox item=null

                          1 Reply Last reply
                          0
                          • P Pete OHanlon

                            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

                            M Offline
                            M Offline
                            MemberDotNetting
                            wrote on last edited by
                            #14

                            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

                            1 Reply Last reply
                            0
                            • B Bernhard Hiller

                              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!

                              M Offline
                              M Offline
                              MemberDotNetting
                              wrote on last edited by
                              #15

                              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

                              L 1 Reply Last reply
                              0
                              • M MemberDotNetting

                                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

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #16

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

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups