Add/Remove Item - Player online?

Hello,

i try to add and remove items from players with this lines of code:

if (!*args)
return false;

Player* target;
uint64 targetGuid;
std::string targetName;

if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
return false;

uint32 itemid = 70000;

//Add
target->AddItem(itemid,1);
target->SaveToDB();

//Remove
target->DestroyItemCount(itemid,1,true,false);
target->SaveToDB();

If the target is offline, then the core crash. So i want to check the players online status. Is there a function for check the online status from target or is there annother solution to solve the core crash problem?

Initialize target with NULL.

Then check it after extracting the target, for example:

Player* target = NULL;
uint64 targetGuid;
std::string targetName;
 
if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName) || !target)
    return false;