Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using E_contact.econtactClasses;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace E_contact
{
    public partial class Econtact : Form
    {
        public Econtact()
        {
            InitializeComponent();
        }
\\ the eror is coming from here 
        contactClass c = new contactClass(); 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //get value from the input fields
            c.FirstName = txtboxFirstName.Text;
            c.LastName = txtboxLastName.Text;
            c.ContactNr = txtboxContactNumber.Text;
            c.Adress = txtboxAdress.Text;
            c.Gender = cmbGender.Text;

            bool success = c.Insert(c);
            if(success==true)
            {
                MessageBox.Show("Contact Inserted");

            }
            else
            {
                MessageBox.Show("Failed to add New Contact. Please try Agian");
            }

        }

        private void textboxContactNumber_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
C#



What I have tried:

I am new to coding so its very hard for me to find bugs. Please if anyone could help me it would save my life. Thanks in Advance. The NullReferenceException is coming from contactClass c = new contactClass();
Posted
Updated 30-Oct-19 3:16am
Comments
MadMyche 30-Oct-19 7:27am    
Please add in the code for the contactClass, particularly the Construct portion; by using the Improve Question widget

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
The contactClass is missing or there is something wrong in this class, read here how to define a class:
Classes - C# Programming Guide | Microsoft Docs[^]

Also see: c# - What is a NullReferenceException, and how do I fix it? - Stack Overflow[^]
 
Share this answer
 
v3
What is happening in the constructor for contactClass?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900