Solved

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

Posted on 2014-10-23
8
310 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 19

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

895 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

13 Experts available now in Live!

Get 1:1 Help Now