Solved

why do I get nullref....?

Posted on 2009-07-09
2
181 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

13 Experts available now in Live!

Get 1:1 Help Now