Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, a quick attempt of me trying to create random points it continuesly returns
with an error saying: "Object reference not set to an instance of an object.".

Here is the code:

C#
public void GeneratePoints(int Width, int MaximumGain, int Frequency)
{
    /* Create a random. */
    Random rd = new Random();

    /* Initialize the Points array. */
    double[] ReturnPoints = new double[Width];

    /* Set a starting point. */
    ReturnPoints[Width / 2] = rd.Next(0, MaximumGain);

    /* Generate random points after the middle.*/
    for (int i = Width / 2 + 1; i < Width; i++)
    {
        // -- Strangly, this is the error.
        ReturnPoints[i] = rd.Next(0, (int)Points[i - 1]);
    }

    /* Generate random points before the middle.*/
    for (int i = Width / 2 - 1; i > 0; i--)
    {
        ReturnPoints[i] = rd.Next(0, (int)Points[i + 1]);
    }

    /* Set the points. */
    Points = ReturnPoints;
}
Posted
Updated 4-Apr-13 2:37am
v2
Comments
ZurdoDev 4-Apr-13 8:40am    
The error is clear. What is not clear, at least to me, is which line of code throws the error. But why are you stuck?
Yvar Birx 4-Apr-13 8:41am    
" // -- Strangly, this is the error.
ReturnPoints[i] = rd.Next(0, (int)Points[i - 1]);
"

Well, I am stuck because I need these points to be generated.
ZurdoDev 4-Apr-13 8:46am    
What is Points? Also what is i?
Yvar Birx 4-Apr-13 9:00am    
I got it fixed. :P

This means that you are trying to access to array element, but array does not contain this element of array has been declared but never initialized.

See my past answer: NullReferenceException in vb.net[^]
 
Share this answer
 
Comments
Yvar Birx 4-Apr-13 8:46am    
Double arrays do not really have to be initialized, when they are declared they will all have the state of 0. And this is confusing because it is saying it's null. O.o
Maciej Los 4-Apr-13 8:50am    
Does Point[] array exists in current context?
Yvar Birx 4-Apr-13 8:59am    
I got it fixed, I'm sorry. I was using Point[] instead of ReturnPoint.

Thanks :)
Maciej Los 4-Apr-13 9:06am    
You're welcome ;)
It's not clear what you are trying to achieve but if you initialize Points also somewhere then the object reference error should not come.

C#
/* Initialize the Points array. */
double[] ReturnPoints = new double[Width];
// Add this line of code.
int[] Points = new int[Width];


Or else you have to use ReturnPoints itself where ever you are using Points

C#
/* Generate random points after the middle.*/
for (int i = Width / 2 + 1; i < Width; i++)
{
    // -- Strangly, this is the error.
    //ReturnPoints[i] = rd.Next(0, (int)Points[i - 1]);
    ReturnPoints[i] = rd.Next(0, (int)ReturnPoints[i - 1]);
}

/* Generate random points before the middle.*/
for (int i = Width / 2 - 1; i > 0; i--)
{
    //ReturnPoints[i] = rd.Next(0, (int)Points[i + 1]);
    ReturnPoints[i] = rd.Next(0, (int)ReturnPoints[i + 1]);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900