简体   繁体   中英

NullReferenceException when accessing some object

I know this is a noob error but I really can't discover why it's coming up as I am accessing an object that is set.

The xloc and yloc both being local variables.

gameBorder.FormInstance.tableLayoutPanel1.GetControlFromPosition(xloc, yloc).BackgroundImage = Properties.Resources.Image;

However this has been set within the form class:

namespace csharp_build
{
    public partial class gameBorder : Form
    {
        public static gameBorder FormInstance;

        public gameBorder()
        {
            FormInstance = this; 
            InitializeComponent();            
        }
    }
}

Any idea why this happens? Would it be to do with the fact that the form class is referenced as gameBorder , and that is what the constructor is called, and the name for the form class in the solution explorer is Form1.cs?
I know this is a noob problem and I do apolagize but any help would be greatly appreciated.

If you are sure that tableLayoutPanel1 exists and is not null, then change your code to this and see what happens:

  var control = gameBorder.FormInstance.tableLayoutPanel1.GetControlFromPosition
      (xloc, yloc);
  if (control == null) throw new NullReferenceException(
           "No control at those coordinates");
  control.BackgroundImage = Properties.Resources.Image;

jeez, guys, this is code for analysis, debugging, to help figure out illustrate the cause of the issue, that's all.....

WARNING WARNING WARNING: NOT INTENDED AS FINAL PRODUCTION CODE
Thanks to @SriramSakthivel comments below, please Note that NullReferenceException is being thrown here only for debugging/Analysis purposes, and should never be thrown by application code in production release.

The way you chain members and method results makes it hard to determine at a glance what causes the NullReferenceException to be thrown. FormInstance could be null if it's accessed before any gameBorder instances ever get created. Although unlikely, tableLayoutPanel could be null as well. The result of GetControlFromPosition method could very well be too, if no control is on the specified cell.

When you are encountering this kind of problem within this kind of code, the best thing to do is to decompose the calling chain. This way you will be able to quickly tell where the problem lies.

You can also make code assertions , like this:

var formInstance = gameBorder.FormInstance;
Debug.Assert(formInstance != null);
var controlAtPos = formInstance.tableLayoutPanel1.GetControlFromPosition(xloc, yloc);
Debug.Assert(controlAtPos != null);

controlAtPos.BackgroundImage = Properties.Resources.Image; // You may want to make some assertions on resolving your image instance too

The beauty of code assertions is that it's easy to exclude these from compiling into production code. By default, it's compiled in debug configuration and excluded in release configuration. This way you can benefit from the help it provides on debugging without worrying about the extra overhead in deployed code.

Keep this in mind, it will be useful if you ever face this issue again in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM