0

So I created Pairs class that contains int and double and I want to create an array of them with my array class by creating random values, but I'm getting System.NullReferenceException at Line 19 of my array class.

Here's my pair class

class Pair
{

    public int integer = 0;
    public double doubl = 0.0;

    public Pair(int integer, double doubl)
    {
        this.integer = integer;
        this.doubl = doubl;
    }

    public Pair()
    {

    }
    public int Integer() { return integer; }
    public double Doubl() { return doubl; }
}

And this is my array class and the abstract class

class MyDataArray : DataArray
{

    Pair[] data;
    int operations = 0;

    public MyDataArray(int n, int seed)
    {
        data = new Pair[n];
        Random rand = new Random(seed);
        for (int i = 0; i < n; i++)
        {
            data[i].integer = rand.Next(); //I get error here
            data[i].doubl = rand.NextDouble();

        }

    }

    public int integer(int index)
    {
        return data[index].integer;

    }

    public double doubl(int index)
    {
        return data[index].doubl;
    }
}

abstract class DataArray
{

    public int operations { get; set; }
    public abstract void Swap(int i, int j);
    public abstract void Swap2(int i, int high);
}

Also is it even worth it using this abstract class I used this from a reference that my university provided. I have to create an quicksort algorithm that sorts pairs in arrays and linked lists and analyze it.

1
  • The code doesn't compile. Swap and Swap2 are no overridden. Mar 30, 2020 at 17:29

2 Answers 2

2
data = new Pair[n];

This creates a new array of null references.

The loop should be

    for (int i = 0; i < n; i++)
    {
        data[i] = new Pair(rand.Next(), rand.NextDouble())
    }

While we are looking at your code: you are making a good attempt here to make an immutable pair, but it could be better. What you want is:

class Pair
{
    public Pair(int integer, double doubl)
    {
        this.Integer = integer;
        this.Double = doubl;
    }

    public int Integer { get; private set; }
    public double Double { get; private set; }
}

Shorter, safer, clearer.

3
  • You're welcome; make sure you take some time to understand why it works. A null reference exception indicates that you forgot to create an object somewhere. Mar 30, 2020 at 17:34
  • @EricLippert: If the intent is an immutable pair, I would usually prefer a read-only property (i.e., {get;} rather than {get; private set;}).
    – Brian
    Mar 30, 2020 at 18:48
  • @Brian: Sure, or even better, just use a tuple. Mar 30, 2020 at 19:54
0

The issue with your code is that you are only initializing the array of data in MyDataArray. When creating an array of instances, it only initializes references for the array, not the actual instances to be in the array. Those references all point to null. So when you do try to set the integer member of the i'th Pair instance in the data array:

...
data[i].integer = rand.Next();
...

You are actually trying to set the integer member of null, which does not exist.

...
null.integer = rand.Next();
...

To fix this, simply create a new instance of Pair for each index of data in your loop.

...
for (int i = 0; i < n; i++)
{
    data[i] = new Pair();
    data[i].integer = rand.Next();
    data[i].doubl = rand.NextDouble();

}
...

Even better, you can use the constructor you've made where it takes parameters to set integer and doubl upon construction to simplify the code in your loop.

...
for (int i = 0; i < n; i++)
{
    data[i] = new Pair(rand.Next(), rand.NextDouble());
}
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.