Click here to Skip to main content
14,328,128 members
Rate this:
Please Sign up or sign in to 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
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100