Solved

why do I get nullref....?

Posted on 2009-07-09
2
180 Views
Last Modified: 2012-05-07
Why do I get nullreference here?

 CM.Contacts.Add(cust);

How should it be done?
namespace MyPhoneBook

{

    public partial class Form1 : Form

    {

        private ContactManager CM;
 
 

        public Form1()

        {

            InitializeComponent();

            ContactManager CM = new ContactManager();

        }
 

        private void button1_Click(object sender, EventArgs e)

        {

            Contact cust = new Contact();

            cust.FirstName = textBox1.Text;

            CM.Contacts.Add(cust);

            
 
 

            Console.ReadLine();

        }

    }

}
 
 
 
 

--------------------------
 

using System;

using System.Collections.Generic;

using System.Text;
 

namespace Persons

{

    public class ContactManager

    {
 

        //Deklaration

        List<Contact> contact;

        bool exists = false;
 

        #region Konstruktor
 
 
 

        public ContactManager()

        {

            contact = new List<Contact>();           

            exists = true;

        } //end CustomerManager
 
 
 

         public ContactManager(List<Contact> constact)

        {

            this.contact = constact;

        } //end CustomerManager
 

        #endregion

        
 

        #region Egenskaper
 
 
 

        public int Count

        {

            get { return contact.Count; }

        } //end Count
 
 
 

        public List<Contact> Contacts

        {

            get { return contact; }

            set { contact = value; }

        } //end Customers
 
 

        #endregion
 
 

        #region Metoder
 
 
 

        public int Search(Contact cust)

        {

            int found = -1;
 

            if (exists)

            {

                for (int i = 0; i < contact.Count; i++)

                {

                    if (contact[i].FirstName.Equals(cust.FirstName) && contact[i].LastName.Equals(cust.LastName))

                    {

                        found = i;

                    } //end if                                    

                } //end for

            } //end if
 

            return found;

        } //end Search
 
 
 

        public void DeleteCustomer (int index) 

        {

            if (exists)

            {

                contact.RemoveAt(index);                

            } //end if        

        } //end DeleteCustomer
 
 
 

        public void ChangeCustomer(Contact cust)

        {
 

        }
 
 
 

        public Contact GetCustomer(int index)

        {

            return null;

        } //end GetCustomer
 
 
 
 

        public void AddCustomer(Contact cust)

        {

            if(exists)

            {

                contact.Add(cust);

            } //end if
 

            else

            {

                contact = new List<Contact>();

                contact.Add(cust);

                exists = true;

            } //end else            

        } //end AddCustomer
 
 
 
 
 

        #endregion
 

    } //end class

} //end namespace

Open in new window

0
Comment
Question by:Mickeys
2 Comments
 
LVL 5

Accepted Solution

by:
Pablissimo earned 500 total points
ID: 24811833
       public Form1()
        {
            InitializeComponent();
            ContactManager CM = new ContactManager();
        }


is wrong - should be

        public Form1()
        {
            InitializeComponent();
            CM = new ContactManager();
        }

You're actually declaring a local variable called CM (rather than initialising the CM member variable) at the minute.
0
 

Author Closing Comment

by:Mickeys
ID: 31601489
Ah thx. Get blind sometimes :-)  .....or maybe all the time is a more correct word. :-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now