5 Replies - 447 Views - Last Post: 12 July 2012 - 12:58 PM Rate Topic: -----

#1 zomgrock  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-July 11

nullReferenceException

Posted 12 July 2012 - 01:57 AM

getting a nullReferenceException
Player class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame3
{
    class Player
    {
        public Texture2D texture;
        public int health;
        public Vector3 location = new Vector3(0, 0, 0);
        public Vector2 velocity = new Vector2(0,0);
        //make weapon class then add weapon attribute

        public Player(Texture2D a, int b, Vector3 c)
        {
            texture = a;
            health = b;
            location = c;
        }

        public void Update(double dTime)
        {
            location.X += velocity.X * (float)dTime;
            location.Y += velocity.Y * (float)dTime;

        }

        public void Attack()
        {
        }
    }
}



error occurring on this line
if (player.velocity.X != 0f && player.velocity.Y != 0f)


kind of new to c# programming and not sure why I'm getting this error. Any help would be greatly appreciated :withstupid:

:surrender: oh quick edit: player is a Player object

Is This A Good Question/Topic? 0
  • +

Replies To: nullReferenceException

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: nullReferenceException

Posted 12 July 2012 - 02:27 AM

http://www.dreaminco...-common-errors/
Was This Post Helpful? 0
  • +
  • -

#3 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: nullReferenceException

Posted 12 July 2012 - 02:50 AM

Take a look at your error. A nullReferenceException means you are attempting to read or write variables or objects whose value is null, meaning they haven't been set.

It's pointing at the line where you are using player (an object of Player class). Assuming you have instantiated the player, the error must be to do with the Vector2 velocity - you're trying to test against its values before it has any set).

You could either

A. Test for nulls before this condition, or

B. Assign the values explicitly before this condition, or

C. Use a constructor for the Player class that will define default velocity values (and any others you may want) that will be set when you create an instance.

Hope that helps.

This post has been edited by MrShoes: 12 July 2012 - 02:51 AM

Was This Post Helpful? 1
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: nullReferenceException

Posted 12 July 2012 - 02:56 AM

Like the fellow programmers above stated, check for null values.

By the looks of it, I'd say the error occurs because you haven't instantiated player object, you are currently using it like this:

Player player;
if (player.velocity.X != 0f && player.velocity.Y != 0f)
  // do something



Or you are somewhere settings your player's velocity to nothing:
player.velocity = null;



Your player class itself looks just fine: everything has a value so it those fields shouldn't throw null reference exceptions unless you somewhere else set them to null.
Was This Post Helpful? 2
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5511
  • View blog
  • Posts: 11,815
  • Joined: 02-June 10

Re: nullReferenceException

Posted 12 July 2012 - 07:20 AM

What does this error mean?

Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:

This post has been edited by tlhIn`toq: 12 July 2012 - 07:21 AM

Was This Post Helpful? 1
  • +
  • -

#6 landopr786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-April 12

Re: nullReferenceException

Posted 12 July 2012 - 12:58 PM

The links tlhIn`toq gave are excellent along with a bit of advice he gave me on a previous post:
Don't ASSUME you have a value.
Make sure there is something there before you start comparing. VS's debugger is very powerful and you'll get used to solving this annoyances pretty quickly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1