locked
Create an object in a class RRS feed

  • Question

  • Hello all,

    I have written some code in Form1.cs where I have a method called GetFileData

    private void GetFileData() { char[] seperation = { ',', ' ' }; string[] names; string temp; string[] temp2; char[] MyChar = { 'A', 'D', 'a', 'd', 'C', 'F', 'G' }; int len; int totChannels; /* Lots of other code //////// ////// */ AnalogChannelInfo analogChannelInfo = new AnalogChannelInfo();

    for (int i = 1; i <= totChannels; i++) { temp2 = clist[2 + i].Split(seperation); analogChannelInfo.An[i] = Convert.ToInt32(temp2[0]); analogChannelInfo.ch_id[i] = Convert.ToInt32(temp2[1]); analogChannelInfo.ph[i] = Convert.ToInt32(temp2[3]); analogChannelInfo.ccbm[i] = Convert.ToInt32(temp2[4]); analogChannelInfo.uu[i] = temp2[5]; } label1.Text = analogChannelInfo.uu[3];

    }

    In a separate class file called ClassProcessing.cs I have created an object as shown below

    public class ClassProcessing
    {
        public static int CalcSampleSize(int Ak, int DM)
        {
        //This formula calculates the size of a sample in bytes
        return (Ak * 2) + (2 * (DM / 16)) + 8;
        }
    
    }
    
    public class AnalogChannelInfo
    {
        public int[] An;
        public int[] ch_id;
        public int[] ph;
        public int[] ccbm;
        public string[] uu;
        public double[] chMult;
        public double[] chOffset;
        public double[] chSkew;
        public int[] minRange;
        public int[] maxRange;
        public int[] primRatio;
        public int[] secRatio;
        public string[] scaleID;
    }

    Unfortunately I get a NullReferenceException The object reference is not set to an instance of an object. I guess I have made a mistake in the ClassProcessing.cs file but I don't know what I have done wrong. Could someone help me please?

    Thanks in advance

    The object reference is not set to an instance of an objec
    The object reference is not set to an instance of an objec
    Wednesday, September 16, 2020 10:22 AM

All replies

  • In ClassProcessing if this is where the error is set a breakpoint and examine variables looking for one which is null then figure out why it's not assigned.

    Your array properties need to be initialized or have a fix size if the error is happening when assigning values in the for next.

    See https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Wednesday, September 16, 2020 10:39 AM
  • Im sorry I did not tell where the error occurs. It sets the breakpoint inside the For loop at the second line:

    analogChannelInfo.An[i] = Convert.ToInt32(temp2[0]);

    I dont see how I can initialize these array properties because they will vary from time to time.


    Wednesday, September 16, 2020 10:48 AM
  • Indeed if I give the arrays in public class AnalogChannelInfo a fixed size the problem stops. That gives me the question how could I give the size of the array to this public class? 
    Wednesday, September 16, 2020 11:01 AM
  • I was able to fix it but had to use List<>

    Great learned something new today
    Wednesday, September 16, 2020 1:34 PM
  • Im sorry I did not tell where the error occurs. It sets the breakpoint inside the For loop at the second line:

    analogChannelInfo.An[i] = Convert.ToInt32(temp2[0]);

    I dont see how I can initialize these array properties because they will vary from time to time.


    As you have learned a list<T> solves the problem. If later you need an array from the list simply use .ToArray()

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, September 16, 2020 2:38 PM