Solved

why do I get nullref....?

Posted on 2009-07-09
2
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

740 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