Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

why do I get nullref....?

Posted on 2009-07-09
2
Medium Priority
?
187 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

661 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