Solved

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

Posted on 2014-10-23
8
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 20

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Introduction to Processes
Starting up a Project

636 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