?
Solved

NullReferenceException was unhandled c#

Posted on 2007-10-13
2
Medium Priority
?
840 Views
Last Modified: 2008-01-09
Hi, I keep getting an error "NullReferenceException was unhandled" when I try to initialize an array of class. follow part of the code.


   public partial class newAnimal : Form
    {
        public String diet;
        public String photofile;
        public Image picture;


        public newAnimal()
        {
          InitializeComponent();
          this.picture = Image.FromFile(photofile);
        }
       
    }
public partial class Form1 : Form
    {
        private newAnimal[] m_Animals = new newAnimal[10];
       
         public Form1()
        {
           
            InitializeComponent();
            m_Animals[0].diet = "diet name"
                           
        }
}


thanks for help
0
Comment
Question by:d2marcelo
  • 2
2 Comments
 
LVL 11

Accepted Solution

by:
Babycorn-Starfish earned 1000 total points
ID: 20070549
Hi,

in your Form class you need to initialise each element of the array, so change your code to:

 public partial class Form1 : Form
    {
        private newAnimal[] m_Animals = new newAnimal[10];

        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < m_Animals.Length; i++)
            {
                m_Animals[i] = new newAnimal();
            }
            m_Animals[0].diet = "diet name";
        }
    }

You'll get a new error now however because you don't have a name for the image when the newAnimal is constructed:

public String diet;
        public String photofile;
        public Image picture;

        public newAnimal()
        {
            InitializeComponent();
            //this.picture = Image.FromFile(photofile); <--------------------------------------- photofile is never set
        }

so you will need to think of a way of initialising the picture field either by changing the constructor to accept a path to a picture file, or you can create a little method like below

public String PhotoFilePath
{
      get
      {
           return photofile;
      }
      set
      {
            photofile = value; //set to the new value
            this.picture = Image.FromFile(photofile);  //update the image with the new image
       }
}

hope this helps,
           
0
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 20070550
sorry, that is property, not a method :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question