Sprite Nullptr when access from another class

I create a sprite and add to scene in the Scene Init fucntion - No problem
I then do a duplicate process with a external class and get nullptr error- no idea why

I have done these things before so cant figure out why error

	auto layer = cocos2d::Layer::create();
	this->addChild(layer, 3);
	cocos2d::Sprite* sprite = cocos2d::Sprite::create("Agent/dot.png");
	layer->addChild(sprite, 1);// NO PROBLEM

    gaddbox->init();
	layer->addChild(gaddbox->getSprite(), 1);// PROBLEM

GaddBox Class

Header File

#ifndef GADDBOX_H
#define GADDBOX_H

#include "cocos2d.h"
#include "WorldLayer.h"
#include "Hud.h"
#include <string>

class GaddBox
{
	public:
		GaddBox();
		~GaddBox();
		void init();
		cocos2d::Layer* createLayer(std::string name);
		bool deleteLayer(int index);
		WorldLayer* getLayer(std::string layerName);
		int getLayerCount();
		std::vector<std::string*> getLayerTags();
		cocos2d::Sprite* getSprite();
	private:
		cocos2d::Sprite* sprite;
		std::vector<WorldLayer*> worldLayers;
};
#endif // DEFINE - GADDBOX_H

CPP File

#include "GaddBox.h"

GaddBox::GaddBox(){

}

GaddBox::~GaddBox(){

}

void GaddBox::init(){
	sprite = cocos2d::Sprite::create("Agent/dot.png");//DEBUGGER SAYS "write access violation, This was nullptr"
	sprite->setPosition(cocos2d::Vec2(100, 100));
}

cocos2d::Sprite* GaddBox::getSprite() {
	return sprite;
}

I have feeling its a silly thing I am missing

Can someone point it out to me please . Thanks very much

Are you positive you are using the code as you paste it here ?

are you sure you are calling :
gaddbox->init();
layer->addChild(gaddbox->getSprite(), 1);// PROBLEM

right after ? or could it be you are doing that inside a lambada function ?

If it is inside a lambada function cocos will recognize your sprite is not in use and will release its memory.

alos I am not quite sure what did you mean :
sprite = cocos2d::Sprite::create(“Agent/dot.png”);//DEBUGGER SAYS “write access violation, This was nullptr”

do you mean that in this like you get the error ?

1 Like

Yes I am copying and pasting exactly as so, I did call init(). I also tried placing in constructor.

You question after, The debugger breaks at that point(Visual Studio) after I run program where I commented the error

I have done this before many times so very confused as to what i am doing wrong or what i am missing

I tried adding also “USING_NS_CC;” to header or CPP header

[SOLVED BY Jedi Nuno147 ]
Thanks very much Jedi Nuno147 for pointing out what i was missing !!!

gaddbox = new GaddBox();

No problem @GaddMaster :slight_smile:
enjoy your cocos2dx time :slight_smile:

Btw, because your constructor is empty. you can just set it up by declaring it as
GaddBox gaddbox;
instead of *gaddbox

This way the instance will be constructed without the need of a new (and will also be free automatically)

It is a good practice to do that in such cases.

also try use m_ or _ when declaring a member variable. will help you out understand which variable is a member or not.

1 Like