Saturday, March 24, 2012

Object reference not set to an instance of an object

I keep getting the following error when I access my Schedule.aspx page.


[NullReferenceException: Object reference not set to an instance of an object.]
Sports.Games.ReadGame(Int32 GameID) in c:\Inetpub\wwwroot\app1\inc\components\games.cs:83
UBM.Schedule.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\app1\inc\cb\schedule.cs:24
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731

I'm using schedule.cs as my CodeBehind and it uses a class file called Games. I was able to compile both successfully. I have a DataList control called "dlstGameList" in my Schedule.aspx page. The rest of my code are as follows:

schedule.cs


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sports;

namespace UBM

{

public class Schedule : Page{

protected virtual void Page_Load(Object sender , EventArgs e) {

DataList dlstGameList;

Games objGame = new Games();
objGame.ReadGame(1);

dlstGameList = objGame.dlstGames;
}

}
}

games.cs

namespace Sports {

using System;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public class Games {

private SqlCommand cmdQuery;

private string strQuery;
private SqlConnection conDados;
public DataList dlstGames;

private SqlDataReader dtrGames;

public Games(){

}

public void ReadGame(int GameID) {

conDados = new SqlConnection( "Server=(local);USER ID=*****;Password=*****;database=*****" );
conDados.Open();

strQuery = "Select * From tbGames WHERE ID = " + GameID;
cmdQuery = new SqlCommand(strQuery, conDados );

dtrGames = cmdQuery.ExecuteReader();
conDados.Close();

dlstGames.DataSource = dtrGames;
dlstGames.DataBind();
}

}
}

<b>Is there anything I'm missing here? I read somewhere that it is a bug and you need to register a DLL in the gacutil but I can't find that file in my .Net directory.

Thanks.Check to make sure dtrGames isn't null, maybe? Also, make sure GameID has a value.

Brian

0 comments:

Post a Comment