?
Solved

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

Posted on 2004-08-18
15
Medium Priority
?
316 Views
Last Modified: 2010-04-15
Hello All

I am getting the infamous "Error:Object reference not set to an instance of an object" error for this piece of code:

textNote.Text=((Pref)Prefs.HList["TreatmentPlanNote"]).ValueString;

 And I have no idea why. Here is the full code for the Prefs class in hopes someone may spot my problem. Sorry if a bit long. Thanks in advance.

namespace OpenDental{
      public struct Pref{
            public string PrefName;
            public string ValueString;
      }

      public class Prefs:DataClass{
            public static Hashtable HList;
            public static Pref Cur;

      
      public static bool ConvertDB(){
            ExitApplicationNow ExitApplicationNow2=new ExitApplicationNow();
            ClassConvertDatabase ClassConvertDatabase2=new ClassConvertDatabase();
            if(ClassConvertDatabase2.Convert(((Pref)HList["DataBaseVersion"]).ValueString)){
               return true;
            }
            else{
               MessageBox.Show(Lan.g("Pref","Conversion unsuccessful"));
               ExitApplicationNow2.ExitNow();
               return false;
                }
            }

      public static void Refresh(){
            HList=new Hashtable();
            Pref tempPref = new Pref();
            cmd.CommandText =
                  "SELECT * from preference";
                  FillTable();
                  for(int i=0;i<table.Rows.Count;i++){
                        tempPref.PrefName=PIn.PString(table.Rows[i][0].ToString());
                        tempPref.ValueString=PIn.PString(table.Rows[i][1].ToString());
                        HList.Add(tempPref.PrefName,tempPref);
                  }
            }

            public static bool TryToConnect(){
               try{
                  con.Open();
                  cmd.CommandText="update preference set valuestring = '0' where valuestring = '0'";
                  int rowsUpdated = cmd.ExecuteNonQuery();
                  con.Close();
                                          }
              catch{
                             return false;
               }
                  return true;
            }

         public static bool DBExists(){
            try{
              con.Open();
              con.Close();
                                     }
            catch{
              return false;
            }
               return true;
            }

      public static void UpdateCur(){
            cmd.CommandText = "UPDATE preference SET "
            +"valuestring = '"  +POut.PString(Cur.ValueString)+"'"
            +" WHERE prefname = '"+POut.PString(Cur.PrefName)+"'";
            NonQ(false);
      }

      public static void FlushAndLock(){
            try{
                  con.Open();
                  cmd.CommandText="FLUSH TABLES WITH READ LOCK";
                  int rowsUpdated = cmd.ExecuteNonQuery();
            }
            catch{
                MessageBox.Show(Lan.g("Pref","Error in FlushAndLock"));
                 }
            }

            public static void Unlock(){
                  con.Close();
            }            
      }
0
Comment
Question by:dean3740
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11838766
It's not the code for the Pref's class we need, but the code around the place that the error is generated from.

My guess is that either your "TreatmentPlanNote" object does not exist in the collection, or you have not initialized your Pref's object (from which you reference Prefs.Hlist) OR you have you initialized your textNote TextBox.  They must be declared, or else the code won't compile, but if you have not set any of them to some value before you used them, then you will get this error.

-Eric
0
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11838774
Oh, one more thing it could be.  You could be retreving the TreatmentPlanNote correctly, but it's member, ValueString, might not be initialized when you try and use it.

When your code breaks, do a Quick Watch of the Perfs object and see if any of the members you are using are null.

-Eric
0
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11838793
The more I look at this, the more I think I know where your problem is.

You have the struct:
public struct Pref{
          public string PrefName;
          public string ValueString;
}

you have to be careful when you are using objects in a struct, as they are not expressly initialized.  When you declare this struct, such as by doing:
Pref p;

Make sure the next thing you do is:
p.ValueString = "blah";
p.PrefName = "name";

Otherwise, if you do something like:
Pref p;
string s = p.ValueString;

You will get the exception you are mentioning.

-Eric
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:dean3740
ID: 11838875
Thanks a lot Eric. Let me look more into the code. Your assistance has been wonderful so far. I'll keep you updated shortly unless you see something before i do.

Dean
0
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11838963
If you could post all your code from where you declare your Prefs object, to where you get the error, that would be helpful.

-Eric
0
 

Author Comment

by:dean3740
ID: 11838974
One thing I probably should've mentioned is that this error didn't arise until i switched my database from mysql to mssql. All was working fine until then but I needed to switch for personal preferences. So I'm not sure why this error is appearing but I do know its in conjunction with the database switch.
0
 

Author Comment

by:dean3740
ID: 11839027
I'll post the code.
0
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11839033
Well, if the MsSQL database contains some null values, then attempting to use the null values returned might generate that error.  Again, this is all speculation as I have not seen your code.

-Eric
0
 
LVL 3

Accepted Solution

by:
bigjim2000 earned 2000 total points
ID: 11839172
Well, I need to head to bed (but it's only 1:43! *grin* )  I'll take a look at this thread in about 5 mins thogh.  If the code is here, I'll take a quick stab at it, otherwise it'll be on my ToDo list for tomorrow morning ;-)

Things I'd check are:

1) Make sure when you are doing the database query, to check for null values that you store in the ValueString members.
2) If you ARE getting null values, then change the DB design to NOT allow null values in that column.... it makes things safer, and requires you to enter valid data to begin with!  Either that, or specify a default value like "NOT_SET" or something, if you MUST allow null values.
3)  Add a watch on your Perf object, then add a breakpoint when you declare that variable.  Step through your code and watch for null values in your object (especially when you THOUGHT you set that value), and even more especially, when you start to USE that value again!

Hope this helps.

-Eric
0
 

Author Comment

by:dean3740
ID: 11839173
I think that is exactly whats happening. I'm really going to have to dig deep into this project. I think I may know where the problem lies. but i tell you what, i'll give you the points because you have been great help with me. Although i'm not sure when I'll have the code posted (for this is a large project but most likely sometime with a few hours or so), just check back with me. I'll keep you posted. Thanks.
0
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11839188
Thanks.

If you need any more feedback on this, feel free to ask.  I'm on MSN at schoof_eas@hotmail.com pretty much ALL the time, that is, if I'm not actually... what's the word.... working ;-)

-Eric
0
 

Author Comment

by:dean3740
ID: 11839197
OK dude. thanks alot!
0
 

Author Comment

by:dean3740
ID: 11848572
Sorry for the delay. There seems to be some other issues with this project that I need to look into. Thanks for all your help however and if I have any further inquiries i'll keep you posted.
0
 

Author Comment

by:dean3740
ID: 11848857
BTW Eric, would you mind if i just send you the file to take a look at? There are no errors when you compile but the "Error:Object reference not set to an instance of an object" error is driving me insane. Maybe you can find something that I am overlooking. I would really appreciate it man. You can download the file here http://www.theredn.com (it is a heavy 12mb) and the file is called odsource. Also in the zip is a MSI installer which will give you fulltrust to access the database from the site. You can find it in the setup1 folder, respectively. I don't have msn, however my cousin does have a windows messenger account at t_barrolle@hotmail.com. I'll be on it tonite in case u have any questions. thanks a mil!
0
 
LVL 3

Expert Comment

by:bigjim2000
ID: 11849910
In case anyone else was wondering, the error was comming from the MS SQL Database.  Not all the entries from the MySQL Database were being copied.  When we tried to access the index "TreatmentPlanNote" in the Prefs HashTable, it didn't exist, and was thus throwing the exception.

-Eric
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question