Solved

why do I get nullref....?

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

832 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