Solved

object reference not set to an instance of an object in Struct

Posted on 2014-10-23
8
304 Views
Last Modified: 2014-10-23
I have a struct I created in it I have a PulseEnergy[] when I go to add to it I get an error
"object reference not set to an instance of an object"
Why?
 public struct My_ControllerMemoryLog
        {
            //public int numberofsurveys;
            public UInt32 EraseID;
            public DateTime downloadtime;
            public string technician;
            public string location;
            public UInt32 toolnumber;
            public UInt32 controllerserialnumber;
            public float toolsize;
            public string jobtype;
            public DateTime SurveyTime;
            public DateTime PulseSyncTime;
            public float numberofpulses;
            public float angle;
            public float azimuth;
            public float tgf;
            public float hoursleft;
            public float voltage;
            public UInt32 battused;
            public float temperature;
            public float tmf;
            public float dip;
            public int[] frame;
            public bool[] PulseJam;
            public bool[] PulsePing;
            public uint[] PulseEnergy;
            public int defaultLocationIndex;
            

        }

for (int j = 0; j < 13; j++)
                {
                    mClist.PulseEnergy[j] = Program.MainForm.myBus.surveydata[i].pulseenergy[j]; <----Error here
                    mClist.PulsePing[j] = Program.MainForm.myBus.surveydata[i].pulseping[j];
                    mClist.PulseJam[j] = Program.MainForm.myBus.surveydata[i].pulsejam[j];

                }
public My_ControllerMemoryLog mClist;

Open in new window

0
Comment
Question by:r3nder
8 Comments
 
LVL 18

Expert Comment

by:Peter Hutchison
ID: 40400160
Shouldn't line 40 where mClist is declared come before line 33, where it is used?
0
 
LVL 6

Author Comment

by:r3nder
ID: 40400168
it does I was just giving all the information - sorry for the confusion
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40400178
Does this cure it, you haven't created a instance of mCList in the code you supply.
public My_ControllerMemoryLog mClist = new My_ControllerMemoryLog();
0
 
LVL 6

Author Comment

by:r3nder
ID: 40400196
no andy that didn't do  it, iit makes it all the way through until it hits the array
 mClist = new My_ControllerMemoryLog();
            int numberOfSurveys = (int)Program.MainForm.myBus.numberofsurveys;
            for (int i = 0; (i < Program.MainForm.myBus.numberofsurveys); i++)
            {
                mClist.technician = Properties.Settings.Default.Username;
                mClist.location = Properties.Settings.Default.Location;
                mClist.EraseID = Program.MainForm.myBus.Controller_EEProm[EEPROM.CONTROLLER.EraseID].Data.asLong;
                mClist.controllerserialnumber = Program.MainForm.myBus.Controller_EEProm[EEPROM.CONTROLLER.SerialNum].Data.asLong;
                mClist.toolnumber = Program.MainForm.myBus.Controller_EEProm[EEPROM.CONTROLLER.ToolNumber].Data.asLong;
                mClist.SurveyTime = Program.MainForm.myBus.surveydata[i].SurveyTimeStamp;
                mClist.PulseSyncTime = Program.MainForm.myBus.surveydata[i].PulseSyncTimeStamp;
                mClist.angle = Program.MainForm.myBus.surveydata[i].Angle;
                mClist.azimuth = Program.MainForm.myBus.surveydata[i].Azimuth;
                mClist.dip = Program.MainForm.myBus.surveydata[i].DipAngle;
                mClist.hoursleft = Program.MainForm.myBus.surveydata[i].BatteryHoursLeft;
                mClist.voltage = Program.MainForm.myBus.surveydata[i].BatteryVoltage;
                mClist.battused = Program.MainForm.myBus.surveydata[i].BatteryCountsUsed;
                mClist.temperature = Program.MainForm.myBus.surveydata[i].Temperature;
                mClist.tgf = Program.MainForm.myBus.surveydata[i].TGF;
                mClist.tmf = Program.MainForm.myBus.surveydata[i].TMF;
                mClist.numberofpulses = Program.MainForm.myBus.surveydata[i].numberofpulsessent;
                mClist.defaultLocationIndex = Program.MainForm.currentuser.defaultlocationid;
                for (int j = 0; j < 13; j++)
                {
                    mClist.PulseEnergy[j] = Program.MainForm.myBus.surveydata[i].pulseenergy[j]; <---Error here
                    mClist.PulsePing[j] = Program.MainForm.myBus.surveydata[i].pulseping[j];
                    mClist.PulseJam[j] = Program.MainForm.myBus.surveydata[i].pulsejam[j];

                }
                for (int k = 0; k < 11; k++)
                {
                    mClist.frame[k] = Program.MainForm.myBus.surveydata[i].frames[k];
                }
                list1.Add(mClist);
            }

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40400211
aha, same principle, you need to create all the arrays.

eg.
public int[] frame;
should be
public int[] frame = new int[99];  //or however many you want in the array

and so on
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40400249
good catch thanks Andy - thank you
0
 
LVL 40
ID: 40400375
I would define a class instead of a structure in you case. Because of the way they are handled in memory, structures should be small objects. Microsoft recommends 16 bytes as the maximum. Your structure is way bigger than this suggested limit.
0
 
LVL 6

Author Comment

by:r3nder
ID: 40400473
Thanks Jacques I am doing  that now
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now