Solved

array of classes and setting property on class gives me a nullexception

Posted on 2013-01-24
2
512 Views
Last Modified: 2013-01-25
I have a method on wcf service and throws a nullexception when I try to assign a value to a property on my class. Here is my method code.

The data comes back from my call to my business object, and I can see the records in the list, what I am trying to do is build out the array, unless there  anyone knows of a different way to handle this. As I just want to call this and return a json object of my results.

 public PatientRecords[] GetPatientName()
        {

            //Will pull back the patient list for the careplans that belong to the site.
            Patients patient_data = new Patients();
            IList<sidwservices.sidw_ef.patient> ReturnValue = patient_data.GetPatientList();
            //Free that object
            patient_data = null;
            PatientRecords[] returns = new PatientRecords[ReturnValue.Count];
            int x = 0;
            for (int i = 0; i < ReturnValue.Count; ++i)
            {
                //Log error message
                ErrorHandler.LogElmahMessage("GetPatientName1", "SidwServices-Careplan");
                //Free error handler object
                ErrorHandler = null;

                //FAILS HERE WITH A NULL EXCEPTION
                returns[x].id = ReturnValue[i].id;
                returns[x].mrn = ReturnValue[i].mrn;

                //Log error message
                ErrorHandler.LogElmahMessage("GetPatientName3", "SidwServices-Careplan");
                //Free error handler object
                ErrorHandler = null;
                x = x + 1;
                
            }
            //Log error message
            ErrorHandler.LogElmahMessage("GetPatientName", "SidwServices-Careplan");
            //Free error handler object
            ErrorHandler = null;
            return returns;
        }

Open in new window


Class code
   [DataContract]
    public class PatientRecords
    {
        [DataMember]
        public long id
        {
            get;
            set;
        }

        [DataMember]
        public long site_id
        {
            get;
            set;
        }

        [DataMember]
        public string mrn
        {
            get;
            set;
        }


        //[DataMember]
        //public long pat_seqno
        //{
        //    get;
        //    set;
        //}

        //[DataMember]
        //public string first_name
        //{
        //    get;
        //    set;
        //}

        //[DataMember]
        //public long last_name
        //{
        //    get;
        //    set;
        //}

        //[DataMember]
        //public DateTime dob
        //{
        //    get;
        //    set;
        //}
    }
 

Open in new window

0
Comment
Question by:JDEE8297
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 150 total points
ID: 38815304
try with:

returns[x] = new PatientRecords();   <------ add this line
returns[x].id = ReturnValue[i].id;
returns[x].mrn = ReturnValue[i].mrn;

Open in new window

0
 

Author Closing Comment

by:JDEE8297
ID: 38818280
thank you, that is what I get for being a c# newbie :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 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