7 Replies - 982 Views - Last Post: 28 July 2010 - 07:55 AM Rate Topic: -----

#1 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

NullreferenceException

Posted 28 July 2010 - 03:38 AM

Hi guys,

I keep getting an error that I can't seem to solve. I keep getting an error message on one line of my code. It's a NullReferenceException and says that an object is set to NULL. But I can't work out what object is set to NULL, I've tried watches etc... but this one has me stumped. Can anyone help?

Here's my method that is causing the issue. If you need more info let me know
public string GetPlayerInFinishingPosition(string gamedate, int points, int leagueyear)
        {
            SqlCommand dataCommand = new SqlCommand("SELECT playerid FROM attendees WHERE pointsearned = @points AND gamedate = @gamedate AND leagueyear = @leagueyear", conndb);
            SqlDataAdapter dataAdapter = new SqlDataAdapter(dataCommand);
            dataCommand.Parameters.AddWithValue("gamedate", gamedate);
            dataCommand.Parameters.AddWithValue("points", points);
            dataCommand.Parameters.AddWithValue("leagueyear", leagueyear);
            conndb.Open();
            Int32 pid = (Int32)dataCommand.ExecuteScalar();
            conndb.Close();
            string player = GetPlayerNickname(pid);
            return player;
        }


The problem line is this one:
Int32 pid = (Int32)dataCommand.ExecuteScalar();


Premier2k

This post has been edited by Premier2k: 28 July 2010 - 03:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: NullreferenceException

#2 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: NullreferenceException

Posted 28 July 2010 - 03:51 AM

is dataCommand.ExecuteScalar() guaranteed to return a value, or does it have the chance of returning a null? From what you put, it just seems that you are not getting the return value that you expect. Try setting the return value to a string so you can look at what comes back.
Was This Post Helpful? 0
  • +
  • -

#3 Rico Diesel  Icon User is offline

  • D.I.C Head

Reputation: 62
  • View blog
  • Posts: 122
  • Joined: 06-May 10

Re: NullreferenceException

Posted 28 July 2010 - 03:58 AM

The command dataCommand.ExecuteScalar(); returns an object containing the result set if there is a result set. I reckon your command doesn't return a result set, because no data is selected by your command. Maybe checking your result before you convert it will do the trick for you:

public string GetPlayerInFinishingPosition(string gamedate, int points, int leagueyear)
{
  string player = String.Empty;

  SqlCommand dataCommand = new SqlCommand("SELECT playerid FROM attendees WHERE pointsearned = @points AND gamedate = @gamedate AND leagueyear = @leagueyear", conndb);
  SqlDataAdapter dataAdapter = new SqlDataAdapter(dataCommand);
  dataCommand.Parameters.AddWithValue("gamedate", gamedate);
  dataCommand.Parameters.AddWithValue("points", points);
  dataCommand.Parameters.AddWithValue("leagueyear", leagueyear);

  conndb.Open();
  Object result = dataCommand.ExecuteScalare();
  if (result != null) 
  {
    Int32 pid = (Int32)result;
    player = GetPlayerNickname(pid);
  }
  conndb.Close();
  
  return player; //returns the found player or an empty string
}



Hope this helps,

Rico

EDIT: Adkins beat me to it :P

This post has been edited by Rico Diesel: 28 July 2010 - 03:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: NullreferenceException

Posted 28 July 2010 - 04:05 AM

Make sure conndb is defined and that gamedate isn't null. If gamedate is truly a date in the database, convert it to a DateTime before you bind it. Get rid of the DataAdapter, you aren't using it.

The conndb really shouldn't be global. Pass it or, better, make a method that can be called for a new SqlConnection when you need it.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: NullreferenceException

Posted 28 July 2010 - 04:35 AM

nevermind....don't think that would work

This post has been edited by eclipsed4utoo: 28 July 2010 - 04:38 AM

Was This Post Helpful? 0
  • +
  • -

#6 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: NullreferenceException

Posted 28 July 2010 - 07:44 AM

OK, this is strange....

I have used Rico Diesel's code snippet to check for the results of what's coming back and used message boxes to confirm this and it appears that NULL is indeed being returned from the database. However, when I watch the values I get the values passed in correct. In this instance they are "13 July 2010", 5, 2010 (as string, int, int).

If I check the database manually using
SELECT playerid FROM attendees WHERE gamedate = '13 July 2010' AND pointsearned = 5 AND leagueyear = 2010
then the playerid is returned. However if I pass them in in code then it seems to return NULL???

I don't understand whats going wrong

Premier2k
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,592
  • Joined: 16-October 07

Re: NullreferenceException

Posted 28 July 2010 - 07:54 AM

Again, is gamedate a varchar or a date in the database? If it's a database date, binding to a string probably won't work.
Was This Post Helpful? 0
  • +
  • -

#8 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: NullreferenceException

Posted 28 July 2010 - 07:55 AM

OK I've solved it....

I feel stupid now....

Earlier on today I was testing my database and checking primary keys and foreign keys etc.. and I created some dummy data in the game table. The way my code worked was it checked for the last date in the game table and matched it up with the attendees table. Unfortunately I had created a game but added no attendees for that game.

I've just deleted my database and recreated it with my SQL script and my code now works perfectly!

Quote

The conndb really shouldn't be global. Pass it or, better, make a method that can be called for a new SqlConnection when you need it.

Why should conndb NOT be global? What is the reason for this?

Thanks everyone!

Premier2k
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1