Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
my application contain two classes Database & manager class if i try to calling the createDatabase static method of manager class
Object reference not set to an instance of an object error occur.how can solve this problem?
private Database objdb = Manager.CreateDatabase(ConfigurationManager.AppSettings["DBCN"].ToString());

the manager class is as
C#
using System;
using System.Configuration;

  public class Manager
  {
      private Manager()
      {
      }

      public static Database CreateDatabase(string sConnectionString)
      {
          switch ((ConfigurationSettings.AppSettings["Db"] + "").ToLower())
          {
              case "oracle":
                  return new OracleDatabase(sConnectionString);

              case "mssql":
                  return new SqlDatabase(sConnectionString);
          }
          return null;
      }
  }

Posted
Updated 10-Feb-10 0:47am
v2

yogeshptl wrote:
private Database objdb = Manager.CreateDatabase(ConfigurationManager.AppSettings["DBCN"].ToString());


ConfigurationManager.AppSettings["DBCN"] might be null. In fact, that seems the most likely thing. You should read up on how to use the debugger, set a breakpoint, work out what is null, then fix it.
 
Share this answer
 
One of two things happened.

0) "DBCN" doesn't exist in app.config. Remember that property names are case-sensitive - could it be that your have used "DbCN" or something along those lines?

1) The property you're using exists in app.config, but doesn't have a value.

Either find out what's wrong, or wrap that line of code in a try/catch block, and handle the problem silently (set the value to something that's valid for your app) if possible.

Beyond all that, Christian was right - use the debugger and find out what's wrong with the code. You have the tools - use them.
 
Share this answer
 
This is one of the most basic error which means you are trying to use an object which has not been instantiated.

Debug the code to check in which line exactly the error appears.
You will find, you are trying to access a null object.
 
Share this answer
 
v2

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