Basic C# - Object Reference Not Set To Instance of An Object
-
lol..ok, is there anyone ELSE that can help me. I know I'm close and I got this far by using books....but I'm taking it beyond what the book example is and that's why I'm on this forum. If anyone can share the knowledge to address this basic problem, I'd greatly appreciate it. Thank you anyway Alex... I'll be sure to share the answer with you once I figure it out. -Kay
got it...Here is the functioning code for all those who can benefit from the knowledge... Keep in mind it's just the basic functions and class.... I'm not including all the event... you'll have to fill in the holes...
private void Form1_Load(object sender, System.EventArgs e) { Patient.id[0] = 1; Patient.Lname[0] = "Smith"; Patient.Fname[0] = "John"; Patient.Mname[0] = "Allen"; } public class Patient { public static int[] id = new int[5]; public static string[] Lname = new string[5]; public static string[] Fname = new string[5]; public static string[] Mname = new string[5]; } private void ShowPatient(int id) { txt1.Text = Patient.id[id].ToString(); txt2.Text = Patient.Lname[id]; txt3.Text = Patient.Fname[id]; txt4.Text = Patient.Mname[id]; }
-Kay -- modified at 0:28 Friday 12th May, 2006 -
lol..ok, is there anyone ELSE that can help me. I know I'm close and I got this far by using books....but I'm taking it beyond what the book example is and that's why I'm on this forum. If anyone can share the knowledge to address this basic problem, I'd greatly appreciate it. Thank you anyway Alex... I'll be sure to share the answer with you once I figure it out. -Kay
Here is a good article: http://www.codeproject.com/csharp/quickcsharp.asp Best regards, Alexey.
-
Here is a good article: http://www.codeproject.com/csharp/quickcsharp.asp Best regards, Alexey.
-
private void Form1_Load(object sender, System.EventArgs e) { Patient PatientData = null; PatientData.id[0] = 0; //**ERROR PRODUCING LINE PatientData.Lname[0] = "Smith"; PatientData.Fname[0] = "John"; PatientData.Mname[0] = "Allen"; } public class Patient { public int[] id = new int[5]; public string[] Lname = new string[5]; public string[] Fname = new string[5]; public string[] Mname = new string[5]; }
The above code produces "Object Reference not set to an instance of an object". I simply want to be able to put 5 values in each variable array. I'm having problem with the first one. Can anyone help? P.S.- The code compiles fine... -Kay -- modified at 23:20 Thursday 11th May, 2006hi key, your code shows you didnt create instance of Patient class,only reference you declared which do not reserve any memory on jeap.simply solution is that first create instance of Patient class theh use medhods. like this private void Form1_Load(object sender, System.EventArgs e) { Patient PatientData = new Patient(); PatientData.id[0] = 0; PatientData.Lname[0] = "Smith"; PatientData.Fname[0] = "John"; PatientData.Mname[0] = "Allen"; } public class Patient { public int[] id = new int[5]; public string[] Lname = new string[5]; public string[] Fname = new string[5]; public string[] Mname = new string[5]; } it will work fine. nikesh nikesh
-
got it...Here is the functioning code for all those who can benefit from the knowledge... Keep in mind it's just the basic functions and class.... I'm not including all the event... you'll have to fill in the holes...
private void Form1_Load(object sender, System.EventArgs e) { Patient.id[0] = 1; Patient.Lname[0] = "Smith"; Patient.Fname[0] = "John"; Patient.Mname[0] = "Allen"; } public class Patient { public static int[] id = new int[5]; public static string[] Lname = new string[5]; public static string[] Fname = new string[5]; public static string[] Mname = new string[5]; } private void ShowPatient(int id) { txt1.Text = Patient.id[id].ToString(); txt2.Text = Patient.Lname[id]; txt3.Text = Patient.Fname[id]; txt4.Text = Patient.Mname[id]; }
-Kay -- modified at 0:28 Friday 12th May, 2006Hi! The suggestion to read some more about OOP is a good idea IMHO. For example, using
static
attributes for yourPatient
is plain wrong - you surely want to deal with more than 1 patient, don't you? If you do not understand what the keywordstatic
means or that you have to create new instances of objects before you can use them, you'll have a hard time figuring out why your code doesn't do what you want it to do. Regards, mav -- Black holes are the places where god divided by 0... -
lol..ok, is there anyone ELSE that can help me. I know I'm close and I got this far by using books....but I'm taking it beyond what the book example is and that's why I'm on this forum. If anyone can share the knowledge to address this basic problem, I'd greatly appreciate it. Thank you anyway Alex... I'll be sure to share the answer with you once I figure it out. -Kay
The value assigned to the text box is 0 because it is 0. You must initialize you variable first. And I agree to alex, you really have to read some books. Such silly questions simply flood the forum. I wish you luck, Pavel
-
The value assigned to the text box is 0 because it is 0. You must initialize you variable first. And I agree to alex, you really have to read some books. Such silly questions simply flood the forum. I wish you luck, Pavel
The variable is initialized and populated correctly. The value is 0 because the value is not carrying over through my application. Declaring it as static fixed this as my initilizing and value assigning was not only valid to the local procedure. Maybe I'm not the one who needs to read books here because I challenge you to make it work without using static. P.S.--Don't waste your whole day on it... it's not that big a deal... I'm just trying to prove a point. -Kay
-
Hi! The suggestion to read some more about OOP is a good idea IMHO. For example, using
static
attributes for yourPatient
is plain wrong - you surely want to deal with more than 1 patient, don't you? If you do not understand what the keywordstatic
means or that you have to create new instances of objects before you can use them, you'll have a hard time figuring out why your code doesn't do what you want it to do. Regards, mav -- Black holes are the places where god divided by 0...Mav, Do you understand what I've done? I now understand what static means(and it appears I understand it better than you) and it's not consistent with your statement. Allow me to enlighten you...
private void Form1_Load(object sender, System.EventArgs e) { Patient.id[0] = 1; Patient.Lname[0] = "Smith"; Patient.Fname[0] = "John"; Patient.Mname[0] = "Allen"; } public class Patient { public static int[] id = new int[5]; public static string[] Lname = new string[5]; public static string[] Fname = new string[5]; public static string[] Mname = new string[5]; }
This code means that I can have up to 5 values for each property in that class. This means I can write:Patient.id[1] = 1; Patient.Lname[1] = "Smith"; Patient.Fname[1] = "John"; Patient.Mname[1] = "Allen"; Patient.id[2] = 1; Patient.Lname[2] = "Barker"; Patient.Fname[2] = "Dave"; Patient.Mname[2] = "Barry"; Patient.id[3] = 1; Patient.Lname[3] = "James"; Patient.Fname[3] = "Rick"; Patient.Mname[3] = "Franklin"; Patient.id[4] = 1; Patient.Lname[4] = "Jones"; Patient.Fname[4] = "Paul"; Patient.Mname[4] = "Alexander";
And anytime I call my:private void ShowPatient(int id) { txt1.Text = Patient.id[id].ToString(); txt2.Text = Patient.Lname[id]; txt3.Text = Patient.Fname[id]; txt4.Text = Patient.Mname[id]; }
It will populate the correct patient based on the id being passed in. Hope that helps you understand it better... Also, Please don't HELP demean people by telling them to go read books. You don't know how many books that person has in front of them when they actually ask for help. You want to place an article...that's fine... but it appears that not everyone who says "Read Books" knows the answer. Truth is... I think "Read Books" is an easy way to not answer the question. This forum is for help... I respectfully ask that you save comments like that for yourself so that valueable messageboard threads aren't wasted on "Go Read a Book" replies... However, I do appreciate your willingness to aid. This is why I'm sharing my findings with you... -Kay -
Mav, Do you understand what I've done? I now understand what static means(and it appears I understand it better than you) and it's not consistent with your statement. Allow me to enlighten you...
private void Form1_Load(object sender, System.EventArgs e) { Patient.id[0] = 1; Patient.Lname[0] = "Smith"; Patient.Fname[0] = "John"; Patient.Mname[0] = "Allen"; } public class Patient { public static int[] id = new int[5]; public static string[] Lname = new string[5]; public static string[] Fname = new string[5]; public static string[] Mname = new string[5]; }
This code means that I can have up to 5 values for each property in that class. This means I can write:Patient.id[1] = 1; Patient.Lname[1] = "Smith"; Patient.Fname[1] = "John"; Patient.Mname[1] = "Allen"; Patient.id[2] = 1; Patient.Lname[2] = "Barker"; Patient.Fname[2] = "Dave"; Patient.Mname[2] = "Barry"; Patient.id[3] = 1; Patient.Lname[3] = "James"; Patient.Fname[3] = "Rick"; Patient.Mname[3] = "Franklin"; Patient.id[4] = 1; Patient.Lname[4] = "Jones"; Patient.Fname[4] = "Paul"; Patient.Mname[4] = "Alexander";
And anytime I call my:private void ShowPatient(int id) { txt1.Text = Patient.id[id].ToString(); txt2.Text = Patient.Lname[id]; txt3.Text = Patient.Fname[id]; txt4.Text = Patient.Mname[id]; }
It will populate the correct patient based on the id being passed in. Hope that helps you understand it better... Also, Please don't HELP demean people by telling them to go read books. You don't know how many books that person has in front of them when they actually ask for help. You want to place an article...that's fine... but it appears that not everyone who says "Read Books" knows the answer. Truth is... I think "Read Books" is an easy way to not answer the question. This forum is for help... I respectfully ask that you save comments like that for yourself so that valueable messageboard threads aren't wasted on "Go Read a Book" replies... However, I do appreciate your willingness to aid. This is why I'm sharing my findings with you... -KayThe root of your design problem is that you have declared a
Patient
class that holds an array of what would be properly conceived as individual patient instances. Your code has a severe readability issue and violates object-oriented design principles. If you're going to use that class to hold 5 static people, then a class calledPatientList
would indicate your intent more clearly. Save the obfuscation for later. An approach that would be more reasonable from an OOD standpoint would be to create a class calledPatient
that encapsulates each individual patient's ID, first, middle, and last names, and then use thePatientList
class to maintain an array ofPatient
objects. Also, your use ofstatic
is highly questionable. Unless your goal is to ensure that there is only one list of patients--ever--then there's no reason not to use member fields and instantiate a single patient list. Finally... yes, read books. I don't think the CP community is necessarily trying to insult or demean when making this suggestion. There's no reason to get started off in programming on the wrong foot. It's apparently obvious from the nature of the original question that you don't have a solid grasp on C#'s object fundamentals and object-oriented design. These basics are going to be necessary if you ever plan on moving past "Hello, World!" and becoming a leader. Leaders are successful and secure. Followers are a dime a dozen and can easily be replaced by someone willing to do the work for half the price. -- I've killed again, haven't I? -
The variable is initialized and populated correctly. The value is 0 because the value is not carrying over through my application. Declaring it as static fixed this as my initilizing and value assigning was not only valid to the local procedure. Maybe I'm not the one who needs to read books here because I challenge you to make it work without using static. P.S.--Don't waste your whole day on it... it's not that big a deal... I'm just trying to prove a point. -Kay
And you call this a challenge. There's nothing more simple than that. You have to do 4 things: 1. Remove the keyword static 2. Declare an instance of your class in the global scope of the class where you want to use it 3. Initialize the instance 4. Use it Really simple, isn't it :) P.S.: Take it easy, pal. Every programmer has been a beginner ;)
-
The root of your design problem is that you have declared a
Patient
class that holds an array of what would be properly conceived as individual patient instances. Your code has a severe readability issue and violates object-oriented design principles. If you're going to use that class to hold 5 static people, then a class calledPatientList
would indicate your intent more clearly. Save the obfuscation for later. An approach that would be more reasonable from an OOD standpoint would be to create a class calledPatient
that encapsulates each individual patient's ID, first, middle, and last names, and then use thePatientList
class to maintain an array ofPatient
objects. Also, your use ofstatic
is highly questionable. Unless your goal is to ensure that there is only one list of patients--ever--then there's no reason not to use member fields and instantiate a single patient list. Finally... yes, read books. I don't think the CP community is necessarily trying to insult or demean when making this suggestion. There's no reason to get started off in programming on the wrong foot. It's apparently obvious from the nature of the original question that you don't have a solid grasp on C#'s object fundamentals and object-oriented design. These basics are going to be necessary if you ever plan on moving past "Hello, World!" and becoming a leader. Leaders are successful and secure. Followers are a dime a dozen and can easily be replaced by someone willing to do the work for half the price. -- I've killed again, haven't I?I have no problem being told to read books... but if that's all you put in the post. Then it has an insulting demeanor. If it's accompanied by help...it feels different. That's just my opinion... As for your comments. I agree.... the Patient Class would've been better named as PatientList. You go on to say create a class to maintain another class's array Information. If I'm not mistaken the Code would looks something like PatientList.Patient.id[0] Either way, I thought you might find this funny.....sometimes "Hello World" isn't such a bad thing. Sometimes it makes the most sense.... -Kay
-
I have no problem being told to read books... but if that's all you put in the post. Then it has an insulting demeanor. If it's accompanied by help...it feels different. That's just my opinion... As for your comments. I agree.... the Patient Class would've been better named as PatientList. You go on to say create a class to maintain another class's array Information. If I'm not mistaken the Code would looks something like PatientList.Patient.id[0] Either way, I thought you might find this funny.....sometimes "Hello World" isn't such a bad thing. Sometimes it makes the most sense.... -Kay
If you use
PatientList
with an array ofPatient
objects, accessing the first patient's ID would be more along the lines ofPatientList.Patients[0].id
. It would also be more readable and nimble when doing multiple things to a single patient:Patient p0 = PatientList.Patients[0]; p0.id = "blah"; p0.middleName = "Blahblah"; ...
-- I've killed again, haven't I? -
If you use
PatientList
with an array ofPatient
objects, accessing the first patient's ID would be more along the lines ofPatientList.Patients[0].id
. It would also be more readable and nimble when doing multiple things to a single patient:Patient p0 = PatientList.Patients[0]; p0.id = "blah"; p0.middleName = "Blahblah"; ...
-- I've killed again, haven't I?Just thought I'd show you the progress I made based on your suggestions.... It doesn't quite look like the code you suggested (it didn't compile).. but with some minor changes it worked just the same... Thanks for the direction...
private void Form2_Load(object sender, System.EventArgs e) { ClientList C = new ClientList(); C.Client[0] = new Client(); C.Client[0].id = 1; C.Client[0].ClientNm = "John Allen"; C.Client[1] = new Client(); C.Client[1].id = 2; C.Client[1].ClientNm = "Michael Thomas"; for (int i=0;i<2;i++) { MessageBox.Show (C.Client[i].id.ToString()); MessageBox.Show (C.Client[i].ClientNm); } } public class Client:ClientList { public int id; public string ClientNm; } public class ClientList { public Client [] Client = new Client[5]; }
-Kay -- modified at 19:51 Saturday 13th May, 2006 -
Just thought I'd show you the progress I made based on your suggestions.... It doesn't quite look like the code you suggested (it didn't compile).. but with some minor changes it worked just the same... Thanks for the direction...
private void Form2_Load(object sender, System.EventArgs e) { ClientList C = new ClientList(); C.Client[0] = new Client(); C.Client[0].id = 1; C.Client[0].ClientNm = "John Allen"; C.Client[1] = new Client(); C.Client[1].id = 2; C.Client[1].ClientNm = "Michael Thomas"; for (int i=0;i<2;i++) { MessageBox.Show (C.Client[i].id.ToString()); MessageBox.Show (C.Client[i].ClientNm); } } public class Client:ClientList { public int id; public string ClientNm; } public class ClientList { public Client [] Client = new Client[5]; }
-Kay -- modified at 19:51 Saturday 13th May, 2006Good work; you're definitely off to a better start. One little detail is in how you define the
Client
class:public class Client:ClientList
{
public int id;
public string ClientNm;
}The
Client
class should not inherit fromClientList
. Inheritance supports the "is-a" relationship, butClientList
->Client
is a "has-a" relationship (more, see "Inheritance"[^]). The way it's set up now, each individualClient
also has its own array ofClient
s. If you simply remove the:ClientList
from that definition, it should behave just fine. Also, if the array ofClient
objects defined in ClientList were called "Clients", it would follow common convention and have a more natural reading flow, since arrays imply an intent to hold more than just one object. -- I've killed again, haven't I?