4 Replies - 1019 Views - Last Post: 03 March 2014 - 10:18 AM Rate Topic: -----

#1 alex90   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-March 14

Help with an error: Object reference not set to an instance of an obj

Posted 02 March 2014 - 12:28 AM

Hi,

I am very new on C# and I need your help to explain me an very annoying error.I'm trying to make a "Rain" application, by drawing some lines on the form, and then they must fall.
So here is my start code:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        Random r = new Random();
        Graphics paper;
        Line line;

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
            for (int i = 0; i < 100; i++)
            {
                line = new Line(r);
           
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            paper = this.CreateGraphics();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < 20; i++)
            {
                //line = new Line(r);
                line.Rain();
                line.DrawLine(paper);
            }
        }


    }
    class Line
    {

        Point sp;
        Point ep;
        private int x1, y1, x2, y2;
        Pen p;


        public Line(Random r)
        {
            x1 = r.Next(510);
            y1 = r.Next(360);

            x2 = x1;
            y2 = y1 + 10;
        }

        public void DrawLine(Graphics paper)
        {
            p = new Pen(Color.White);
            sp = new Point(x1, y1);
            ep = new Point(x2, y2);
            paper.DrawLine(p, sp, ep);
        }

        public void Rain()
        {
            y1 += 10;
            y2 += 10;
            sp = new Point(x1, y1);
            ep = new Point(x2, y2);
        }


    }


So when I start debugging I get the "Object reference not set to an instance of an object." error.
If I remove the comments lines at "timer1_Tick" this error doesn't appear of course.
What can I do to escape from this because it follows me everywhere I go :D/>.
I tried to make with arrays, but same problem.I searched on web about this error, but I didn't understand so much and that's why I came here.Hope you can help me guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with an error: Object reference not set to an instance of an obj

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5260
  • View blog
  • Posts: 14,080
  • Joined: 18-April 07

Re: Help with an error: Object reference not set to an instance of an obj

Posted 02 March 2014 - 12:37 AM

Well you created a variable called "line" at the top, but it is not instantiated to an instance of the object. Since it is not actually an object, you can't simply call .Rain() on it because it is currently null. When you uncomment the line you have commented there, you are creating an instance of the object and placing it in the variable, thus "line" is now an instance you can call .Rain() on.

When it comes to objects there are always two things you do... create the variable step 1, step 2 you create an instance of the object and place it into the variable. Sometimes you see this done at the same time... Line line = new Line®;

Simply declaring a variable of type class does not actually go out and get memory space for the object to use. That is why you have to "instantiate it" using the "new" keyword to go get some memory for it.

Now if you are not wanting to use the "new" keyword for some reason, you could check out using structs or creating a class that only contains static methods. These will allow you to create a struct or class without having to instantiating an instance with the new keyword.

:)

This post has been edited by Martyr2: 02 March 2014 - 12:38 AM

Was This Post Helpful? 1
  • +
  • -

#3 alex90   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-March 14

Re: Help with an error: Object reference not set to an instance of an obj

Posted 02 March 2014 - 02:00 AM

Hi,

Thanks for reply.
I understand that I must write "Line line = new Line®". But I don't understand how can I draw 20 lines on the form.
If I use an array by creating global the variable and the instance of the object("Line[] line = new Line[20]") why I must instantiate in every method (button1_click and timer1_Tick) like here:
//Here is for button1_click
for (int i = 0; i < 20; i++)
            {
                line[i] = new Line(r);
            }

//Here is for timer1_Tick
for (int i = 0; i < 20; i++)
            {
                line[i] = new Line(r);
                line[i].Rain();
}
                


Why it can't retain the values from line[i] = new Line® from button1_Click method in line[i].Rain() from timer1_Tick, without instantiate again ?
I hope you understand what am I saying.
Sorry that I am so noob, but like I said, I am a beginner :(.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Help with an error: Object reference not set to an instance of an obj

Posted 02 March 2014 - 08:57 AM

With so little code to go by its hard to know where things are going wrong... But in general you CAN instantiate all 20 lines just once and use them throughout. If you keep assigning the lines a NEW LINE in every method you are over writing them every time. You might be defining the array within a method and therefore the scope of the lines is only in that method, not in the entire class.

You also need to re-example the order of your logic. For example, you start the timer, THEN you initalize the lines. What happens if the interval is so small that the timer ticks before you are done? Then your timer.tick code will run before you are done making the objects. That would be bad.

Then on your drawing it looks like you are making the rain, THEN drawing the paper. Won't that cover all your lines with new paper? Oh wait... No it won't because Rain() doesn't do what the name suggests. It doesn't actually make it rain (drawing the rain). It calculates points. You might want to get in the habit of giving more accurate and meaningful names to the methods. Methods by convention are named as verbs describing what they do. "CalculateRainDrops()" for example.
Was This Post Helpful? 0
  • +
  • -

#5 alex90   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-March 14

Re: Help with an error: Object reference not set to an instance of an obj

Posted 03 March 2014 - 10:18 AM

Hmm...maybe my code can be listed on "the worst code written" :D.
Anyway, I will work on the application and if I have questions be sure I will come again :D.
Thanks again guys, I'm sure your replies will help me . :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1