why does I get nullptr when I try access object variable

Hello!

When I try to access a bool variable from a object in another class I get nullptr when I start my application. I dont relly know why, I have try to make the object to a pointer and a normal but I dont get it right.

This is the code from where I create the variable
1
2
3
4
5
6
7
8
9
10
//main_guy.h
class main_guy : public Entity
{
public:
	main_guy(sf::RenderWindow* window, Entitymanger* entitymanger, class Map* map, float x, float y);
	bool update(sf::RenderWindow *window);
	void collision(Entity* entity);

//this is the variable
bool Finish = false;



In this class I create the object
1
2
3
4
5
6
7
8
9
10
11
12

#include "main_guy.h"

class expbar : public sf::RectangleShape
{
public:
	expbar(int x, int y, sf::RenderWindow* window, main_guy* Player);
	void update(sf::RenderWindow* window);
private:
//here is the object I create when I try to access the bool varibale.
main_guy* Player;


this is the cpp class for expbar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
expbar::expbar(int x, int y, sf::RenderWindow* window, main_guy* Player)
{
	this->Player = Player;
}
void expbar::update(sf::RenderWindow* window)
{

//Here does the error NULLPTR detects.
//The error reads like this "this->Player was nullptr"
if (this->Player->Finish == true)
	{
		std::cout << "Add exp" << std::endl;
	}

}
1
2
3
4
expbar::expbar(int x, int y, sf::RenderWindow* window, main_guy* Player)
{
	this->Player = Player;
}

You need to check that Player is not a nullptr, where is expbar created?
I create the Expbar In another file called "main_game"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "main_game.h"

void main_game::Initialize(sf::RenderWindow *window)
{
	

	
	this->manager = new Entitymanger();

	this->manager->Add("main_guy", new main_guy(window, manager, map, 0, 600));
	
	//window
	
//Here does the expbar creates! :D
	this->Expbar = new expbar(442, 715, window, player);

Last edited on
And what does player in main_game point to?
Last edited on
Nothing I think? I have just create a object of the player class as a pointer in the main_game header file like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

//main_game header

#include "expbar.h"
#include "main_guy.h"

class main_game : public tiny_state
{
public:

	//Här använder vi de virtuela funktionerna från game_state

	void Initialize(sf::RenderWindow *window);

	void Update(sf::RenderWindow *window);

	void Render(sf::RenderWindow *window);

	void Destroy(sf::RenderWindow *window);

private:
	expbar* Expbar;

        //I have just create a object as this, nothing more.
        main_guy* player;
I have just create a object of the player class as a pointer

You have not created a Player object. You have created a pointer to a Player object.

The pointer has not been initialized. You might want to create the object with new as you do with expbar and Entitymanger.


Another option is to not use pointers.
1
2
// player is a main_guy object
main_guy player;

Pointers often complicate the code so it is generally best to only use them when you have a good reason for it. In this situation I don't really see the point of using a pointer (at least not in main_game class) but I only see part of your program so I can't say for sure.
Last edited on
Okay thanks for the answer!

I changed in my expbar.h file
1
2
3
4
//From 
main_guy *player;
//to
main_guy player;


When I have done that I get a new error in the expbar cpp file and reads like this
"no default constructor exists for class "main_guy" ". I slove this error to do like this
1
2
3
4
5
6
//                                                                                    I added this little text underneath
expbar::expbar(int x, int y, sf::RenderWindow* window) : player(player)
{//... Here is where I got the error before I slove it.


}


Then when I started the game it worked fine. But the object dosent read their own values right. Because when I started debugg and hold my mouse above the player it was written that every Player variable was 0 or false. I dont think the object is working correct?
1
2
3
4
5
6
7
8
9
10
void expbar::update(sf::RenderWindow* window)
{
// I hold my mouse above player
if (Player.Finish == true)
	{
		std::cout << "Add exp" << std::endl;
	}

}



Last edited on
You don't want to initialize the player with itself (using the copy constructor). Doesn't the main_guy class have some other constructor you can use?
Hey, thanks for the answer!

I created a new constructor for main_guy.
1
2
3
4
5
6

main_guy::main_guy(int goal)
{
	this->Goal = goal;
}


So in exp bar I changed to not use copy constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class expbar : public sf::RectangleShape
{
public:
int Expgoal = 0;
}


expbar::expbar(int x, int y, sf::RenderWindow* window) : player(Expgoal)
{
}

if (Expgoal == 1)
	{
		std::cout << "goal" << std::endl;
	}


But I dont seem to get it right. Dont know if it was like this you mean?
Last edited on
Oh, sorry, I got a bit confused. In the original code expbar got a variable named Player, and main_game got a variable named player, so when I saw player later I just assumed it was in the main_game class.

My suggestion about not using pointers was primarily aimed towards the main_game class.

If the expbar class needs access to the player object you would have to pass a pointer or reference, either to the constructor and store it in a variable like you did in your original code, or if you only need it inside the update function you could pass it as argument to that function instead.

If all the expbar needs is the experience value of the player you might want to consider decoupling the expbar class from the main_guy class by simply passing the experience value to the expbar somehow, instead of the whole player object.
Last edited on
Topic archived. No new replies allowed.