Solved

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

Posted on 2013-01-24
2
511 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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