-1

Program.cs code:

namespace _1
    {
     static class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    /// 
    public static Form2 form2;
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
        form2 = new Form2();

    }
}

Form1 Code:

Program.form2.pictureBox1.Refresh();

Error:

Object reference not set to an instance of an object.

Not sure why there's an error, I've asked google and no help. Thanks for your help.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Xfrogman43
  • 3
  • 2
  • 5

3 Answers3

0
Object reference not set to an instance of an object.

Means that an object is null, or not assigned.

Hover over the variable while running or use a breakpoint, and check if map or map.pictureBox1 are null

You need to make sure that you set your map to something before you use it, and from the given code I cannot give the specific reason for the error.

Cyral
  • 13,999
  • 6
  • 50
  • 90
  • Since I set map to something in program.cs, it should have that error. map is null anyways so i don't know what to do. – Xfrogman43 Oct 13 '13 at 00:49
0

You're calling Application.Run(new Form1()); before form2 = new Form2();

You need to put form2 = new Form2(); before Application.Run(new Form1());.

I'm Assuming your refresh code is being called from that instance of Form1.

Brad
  • 10,015
  • 17
  • 54
  • 77
0

You are getting a NullReferenceException which generally occurs when you try to access an object having no reference. Generally speaking when the object is null.

In this case form2 is null.

Application.Run is a blocking call. When you call this method it opens the form passed in parameter and stays blocked there until the form closes. So,

form2 = new Form2();

This line of code does not execute and you get a null form2. You can simply resolve the problem by simply reverting the lines like this,

form2 = new Form2();
Application.Run(new Form1());
Hossain Muctadir
  • 3,546
  • 1
  • 19
  • 33