Solved

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

Posted on 2014-10-23
8
315 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

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 article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

830 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