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. unresolved hashtable problem [modified]

unresolved hashtable problem [modified]

Scheduled Pinned Locked Moved C#
helpcsharpdatabasevisual-studio
15 Posts 3 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.
  • J Jassim Rahma

    I a trying to have a hashtable class to populate all the variables from my database to be saved and used in my application as public variables. this is my public class

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;

    namespace cure_hospital_management
    {
    class public_class
    {
    public Hashtable _sysem_parameters_hash;

        public void get\_system\_parameters()
        {
            \_sysem\_parameters\_hash = new Hashtable();
            \_sysem\_parameters\_hash.Add("Name", "Jassim Rahma");
        }
    }
    

    }

    In my MainFom_Load I am calling:

    public_var.get_system_parameters();
    public_var._sysem_parameters_hash.Add("Age", "36");

    then I am trying to read the hashtable value like this:

    MessageBox.Show(public_var._sysem_parameters_hash("Name").ToString());

    but getting this error on the reading line (the MessageBox): System.NullReferenceException was unhandled by user code Message="Object reference not set to an instance of an object." Source="cure" StackTrace: at cure_hospital_management.frmDoctorDashboard.frmDoctorDashboard_Load(Object sender, EventArgs e) in C:\Users\Jassim\Documents\Visual Studio 2008\Projects\Cure\Cure\doctor_dashboard.cs:line 2050 at System.EventHandler.Invoke(Object sender, EventArgs e) at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Form.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ContainerControl.WndProc(Message& m) at System.Windows.Forms.Form.WmShowWindow(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at DevExpress.XtraEditors.XtraForm.WndProc(Message& msg) at cure_hospital_management.frmDoctorDashboard.WndProc(Message& m) in C:\Users\Jassim\Documents\Visual Studio 2008\Projects\Cure\Cure\doctor_dashboard.cs:line 84 at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(In

    S Offline
    S Offline
    Som Shekhar
    wrote on last edited by
    #3

    jrahma wrote:

    unresolved hashtable problem

    Are't all problems unresolved? once they are resolved, they become references ;)

    J 1 Reply Last reply
    0
    • S Som Shekhar

      jrahma wrote:

      unresolved hashtable problem

      Are't all problems unresolved? once they are resolved, they become references ;)

      J Offline
      J Offline
      Jassim Rahma
      wrote on last edited by
      #4

      this is the up-to-date statu:! Unresolved hashtable problem[^]

      S 1 Reply Last reply
      0
      • J Jassim Rahma

        this is the up-to-date statu:! Unresolved hashtable problem[^]

        S Offline
        S Offline
        Som Shekhar
        wrote on last edited by
        #5

        Ok. I went through the whole thing. So far, someone else hasn't been able to repeat your error. Everytime, binarycoder is answering your specific questions that you ask. Could you post your complete code that shows error? If it shows error then it can be solved. May be some mistake is happening somewhere.

        J 1 Reply Last reply
        0
        • S Som Shekhar

          Ok. I went through the whole thing. So far, someone else hasn't been able to repeat your error. Everytime, binarycoder is answering your specific questions that you ask. Could you post your complete code that shows error? If it shows error then it can be solved. May be some mistake is happening somewhere.

          J Offline
          J Offline
          Jassim Rahma
          wrote on last edited by
          #6

          The full code is already uploaded at sky drive as mentioned in my MSDN forum thread public_class.cs[^] main_form.cs[^] doctor_dashboard.cs[^]

          S 1 Reply Last reply
          0
          • J Jassim Rahma

            The full code is already uploaded at sky drive as mentioned in my MSDN forum thread public_class.cs[^] main_form.cs[^] doctor_dashboard.cs[^]

            S Offline
            S Offline
            Som Shekhar
            wrote on last edited by
            #7

            Hi jrahma, I cannot compile this code for obvious reasons coz there are so many links in it. Can't you strip down this code to bare minimum and see just post the problem area? Basically public_class with minimum requirements and two basic forms with calling public_class? if the problem doesn't happen in that form, it is not a problem related to hashtable. Som

            J 1 Reply Last reply
            0
            • S Som Shekhar

              Hi jrahma, I cannot compile this code for obvious reasons coz there are so many links in it. Can't you strip down this code to bare minimum and see just post the problem area? Basically public_class with minimum requirements and two basic forms with calling public_class? if the problem doesn't happen in that form, it is not a problem related to hashtable. Som

              J Offline
              J Offline
              Jassim Rahma
              wrote on last edited by
              #8

              as I have mentioned in my MSDN thread, I have no problme compiling the code because no errors dring the build. The error pops up a run time. I als don't have problem when moving the line: MessageBox.Show(public_var._sysem_parameters_hash("Name").ToString()); to main_form.cs

              S 1 Reply Last reply
              0
              • J Jassim Rahma

                as I have mentioned in my MSDN thread, I have no problme compiling the code because no errors dring the build. The error pops up a run time. I als don't have problem when moving the line: MessageBox.Show(public_var._sysem_parameters_hash("Name").ToString()); to main_form.cs

                S Offline
                S Offline
                Som Shekhar
                wrote on last edited by
                #9

                What I understood from the complete QA is that you face an error when you access a member of hashtable. I cannot go through each line of your code to identify where the problem happens till the time i cannot compile the complete code. If you can provide a stripped down version of three cs files which i can compile and find the error during run time, i could help. You could otherwise try to add a breakpoint just before the line where error happens and look into contents of hashtable. may be you can write a method to do just this. The following method will show the contents of the hashtable in the output window. When you see the error in the messagebox line, u can see if the content is available in the hashtable or not.

                private void PostDataIntoDebug(Hashtable _sysem_parameters_hash)
                {
                #if DEBUG
                foreach (var a in _sysem_parameters_hash.Keys)
                {
                Debug.WriteLine(string.Format("{0} - {1}", a, _sysem_parameters_hash[a]));
                }
                #endif
                }

                J 1 Reply Last reply
                0
                • S Som Shekhar

                  What I understood from the complete QA is that you face an error when you access a member of hashtable. I cannot go through each line of your code to identify where the problem happens till the time i cannot compile the complete code. If you can provide a stripped down version of three cs files which i can compile and find the error during run time, i could help. You could otherwise try to add a breakpoint just before the line where error happens and look into contents of hashtable. may be you can write a method to do just this. The following method will show the contents of the hashtable in the output window. When you see the error in the messagebox line, u can see if the content is available in the hashtable or not.

                  private void PostDataIntoDebug(Hashtable _sysem_parameters_hash)
                  {
                  #if DEBUG
                  foreach (var a in _sysem_parameters_hash.Keys)
                  {
                  Debug.WriteLine(string.Format("{0} - {1}", a, _sysem_parameters_hash[a]));
                  }
                  #endif
                  }

                  J Offline
                  J Offline
                  Jassim Rahma
                  wrote on last edited by
                  #10

                  I am getting: Object reference not set to an instance of an object. on this line:

                  foreach (var a in public_var._sysem_parameters_hash.Keys)

                  from your code... you might say because hashtable is not initilized or somkething similar.. but why? that's my question... Thanks...

                  S 1 Reply Last reply
                  0
                  • J Jassim Rahma

                    I am getting: Object reference not set to an instance of an object. on this line:

                    foreach (var a in public_var._sysem_parameters_hash.Keys)

                    from your code... you might say because hashtable is not initilized or somkething similar.. but why? that's my question... Thanks...

                    S Offline
                    S Offline
                    Som Shekhar
                    wrote on last edited by
                    #11

                    jrahma wrote:

                    you might say because hashtable is not initilized or somkething similar.. but why? that's my question...

                    Well, this is the answer not the question. You need to go through your code step by step and find out where the hashtable was initialized. I went through the complete discussion once again on MSDN. What i understand is that you are utilizing the same hashtable from different forms but you are initializing them separately. I suggest making a small change of making the class and variable static and see if this solves your purpose:

                    static class public_class
                    {
                    public static Hashtable _sysem_parameters_hash;

                        public static void get\_system\_parameters()
                        {
                            \_sysem\_parameters\_hash = new Hashtable();
                            \_sysem\_parameters\_hash.Add("Name", "Jassim Rahma");
                        }
                    }
                    
                    J 1 Reply Last reply
                    0
                    • S Som Shekhar

                      jrahma wrote:

                      you might say because hashtable is not initilized or somkething similar.. but why? that's my question...

                      Well, this is the answer not the question. You need to go through your code step by step and find out where the hashtable was initialized. I went through the complete discussion once again on MSDN. What i understand is that you are utilizing the same hashtable from different forms but you are initializing them separately. I suggest making a small change of making the class and variable static and see if this solves your purpose:

                      static class public_class
                      {
                      public static Hashtable _sysem_parameters_hash;

                          public static void get\_system\_parameters()
                          {
                              \_sysem\_parameters\_hash = new Hashtable();
                              \_sysem\_parameters\_hash.Add("Name", "Jassim Rahma");
                          }
                      }
                      
                      J Offline
                      J Offline
                      Jassim Rahma
                      wrote on last edited by
                      #12

                      Now I cleaned the whole application an created a new application just for the hashtable issue and I am still getting the same error. Hashtable project The above file has a hastable project with three files.. public_class.cs, Form1.cs and Form2.cs, you can see that no issue reading the value in Form1 load but when you click on Form2 it will throw the error... it's seems the hashtable was no initilized but i don't know what to do to fix this in Form2...

                      modified on Monday, April 26, 2010 9:54 AM

                      S 1 Reply Last reply
                      0
                      • J Jassim Rahma

                        Now I cleaned the whole application an created a new application just for the hashtable issue and I am still getting the same error. Hashtable project The above file has a hastable project with three files.. public_class.cs, Form1.cs and Form2.cs, you can see that no issue reading the value in Form1 load but when you click on Form2 it will throw the error... it's seems the hashtable was no initilized but i don't know what to do to fix this in Form2...

                        modified on Monday, April 26, 2010 9:54 AM

                        S Offline
                        S Offline
                        Som Shekhar
                        wrote on last edited by
                        #13

                        Well it was easy to catch. Thanks for the effort of stripping it down. Just change

                        Form2 myForm = new Form2();

                        to

                        Form2 myForm = new Form2(public_var);

                        and it works. You created the Constructor but didn't use it.

                        J 1 Reply Last reply
                        0
                        • S Som Shekhar

                          Well it was easy to catch. Thanks for the effort of stripping it down. Just change

                          Form2 myForm = new Form2();

                          to

                          Form2 myForm = new Form2(public_var);

                          and it works. You created the Constructor but didn't use it.

                          J Offline
                          J Offline
                          Jassim Rahma
                          wrote on last edited by
                          #14

                          Problem solved :) Thanks Som.... Thanks everyone...

                          S 1 Reply Last reply
                          0
                          • J Jassim Rahma

                            Problem solved :) Thanks Som.... Thanks everyone...

                            S Offline
                            S Offline
                            Som Shekhar
                            wrote on last edited by
                            #15

                            :) Cool.

                            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