none
Object reference not set to an instance of an object. RRS feed

  • Question

  • I forgot to say that I'm just a begginer in c#. and also new in this forum.

    I have a new problem now after my recent "Provider list" problem,after I click the update button, I'm encountering "Object reference not set to an instance of an object." that highlights "throw;". i'm just following some instructions but it didn't work for  me.

    I am suppose to create a simple "Add, delete and update " c# program with MS access.

    I use a Windows 7 ultimate 64 bit , Visual studio 2010 express, MS Office 2013.

    I'm very sorry but i cannot post a picture because my account is not verified yet.

     

    This is my codes with the given error:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.OleDb;
    using Domain;
    using System.Data;
    
    namespace Session
    {
        public class Broker
        {
            OleDbConnection Connection;
            OleDbCommand command;
    
            private void ConnectTo()
            {
                Connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Genesis\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\Database.accdb;Persist Security Info=False");
                command = Connection.CreateCommand();
            }
    
            public Broker()
            {
                ConnectTo();
            }
    
            public void Insert(Person p)
            {
                try
                {
                    command.CommandText = "INSERT INTO TPersons (FirstName, LastName) VALUES('" + p.FirstName + "', '" + p.LastName + "')";
                    command.CommandType = CommandType.Text;
                    Connection.Open();
    
                    command.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (Connection != null)
                    {
                        Connection.Close();
                    }
                }
            }
    
            public List<Person> cmdFill()
            {
                List<Person> personList = new List<Person>();
    
                try
                {
                    command.CommandText = "SELECT * FROM TPersons";
                    command.CommandType = CommandType.Text;
                    Connection.Open();
    
                    OleDbDataReader reader = command.ExecuteReader();
    
                    while (reader.Read())
                    {
                        Person p = new Person();
    
                        p.Id = Convert.ToInt32(reader["ID"].ToString());
                        p.FirstName = reader["FirstName"].ToString();
                        p.LastName = reader["LastName"].ToString();
    
                        personList.Add(p);
                    }
                    return personList;
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (Connection != null)
                    {
                        Connection.Close();
                    }
                }
            }
    
            public void Update(Person oldPerson, Person newPerson)
            {
                try
                {
                    command.CommandText = "UPDATE TPersons SET FirstName = " + newPerson.FirstName + ", LastName = " + newPerson.LastName + " WHERE ID=" + oldPerson.Id;
                    command.CommandType = CommandType.Text;
                    Connection.Open();
    
                    command.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (Connection != null)
                    {
                        Connection.Close();
                    }
                }
            }
    
           
        }
    }

    Please HELP ME!, I need this tomorrow, Thanks


    • Edited by Genesis_23 Sunday, October 6, 2013 10:21 AM I've just highlight my keywords to my problem
    Sunday, October 6, 2013 8:05 AM

Answers

  • Hi

    I found one changes in your update query thats single quotes are missing

    command.CommandText = "UPDATE TPersons SET FirstName = '" + newPerson.FirstName + "', LastName = '" + newPerson.LastName + "' WHERE ID=" + oldPerson.Id;


    Happy Coding, RDRaja

    • Marked as answer by Genesis_23 Monday, October 7, 2013 10:36 AM
    Sunday, October 6, 2013 4:01 PM

All replies

  • Please Help Me T_T
    Sunday, October 6, 2013 8:49 AM
  • Hello?....please I need help. T_T
    Sunday, October 6, 2013 10:17 AM
  • I don't see that you initiated the command like this:

    command = new OleDbCommand();

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    Sunday, October 6, 2013 10:40 AM
  • You should check, if some of your objects are null, this can cause your problem. Just set a breakpoint and inspect your objects.

    In these lines

    command.CommandText = "UPDATE TPersons SET FirstName = " + newPerson.FirstName + ", LastName = " + newPerson.LastName + " WHERE ID=" + oldPerson.Id;
    command.CommandType = CommandType.Text;
    Connection.Open();
    

    if

    • command is null, command.CommandText and Command.CommandType will throw an exception
    • newPerson is null, newPerson.FirstName and newPerson.LastName will throw an exception
    • oldPerson is null, oldPerson.Id will throw an exception
    • Connection is null, Connection.Open will throw an exception

    You could check with an if-statement if one of these objects are null to prevent this exception or you have further to debug your could to discover why one of these objects are null.



    Chris

    Code Samples: Code Samples

    Sunday, October 6, 2013 11:11 AM
  • You should read the article about what is an instance of an object.

    someobj obj = new someobj()

    Now obj is an instance in memory and

    http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    The above link applies to Object Oriented Programming, which Java or any language like VB.NET or C#.NET are OOP languages.

    ---------------------------------

    Object reference not set to an instance of an object

    It means that an object was referenced in code, but the object has no instance. The object has no memory reserved for it. The objects value in a null value.

    Sunday, October 6, 2013 12:21 PM
  • Hi

    I found one changes in your update query thats single quotes are missing

    command.CommandText = "UPDATE TPersons SET FirstName = '" + newPerson.FirstName + "', LastName = '" + newPerson.LastName + "' WHERE ID=" + oldPerson.Id;


    Happy Coding, RDRaja

    • Marked as answer by Genesis_23 Monday, October 7, 2013 10:36 AM
    Sunday, October 6, 2013 4:01 PM
  • All of the above codes make it runs great....

    I really want to thank all of you but how?.

    Sunday, October 6, 2013 11:23 PM
  • I've also accidentally delete my form design O_o....

    but it is still running....How can I recover it?

    please tell me how.

    Sunday, October 6, 2013 11:55 PM
  • Hi Genesis_23,

    I agree with darnold924, you should read the article about what is an instance of an object.

    Have you taken basic debugging steps to find out what might be going wrong? Here is an article about Debugging in Visual Studio:http://msdn.microsoft.com/en-us/library/vstudio/sc65sadd.aspx

    Thanks.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 7, 2013 5:52 AM
  • Hii,

    Write down the e below Bold  line of code in Updated Method.

    try
                {
    
                 if(command!=null)
    
                 {
                    command.CommandText = "UPDATE TPersons SET FirstName = " + newPerson.FirstName + ", LastName = " + newPerson.LastName + " WHERE ID=" + oldPerson.Id;
                    command.CommandType = CommandType.Text;
                    Connection.Open();
    
                    command.ExecuteNonQuery();
                }
    }


    PS.Shakeer Hussain



    Monday, October 7, 2013 6:36 AM
  • O_O

    all of the you give me the right codes, the only problem now is how can I mark all yours answers?,

    I mean How can i thank all of you at once?

    i'm very glad that all of you had help me, Thank you all so much :D.

    Monday, October 7, 2013 10:36 AM