When page is in Versions tab I get "Call to a member function getUserName() on null"

Permalink
I have this piece of code
$u = new User();
$ui = UserInfo::getByID($u->getUserID());
$userName = $ui->getUserName();


which works a treat in page. But when I go to do a roll back in the versions tab I click an old page to see and i get this

"Call to a member function getUserName() on null"

Any ideas or is this a bug?

TMDesigns
 
TMDesigns replied on at Permalink Best Answer Reply
TMDesigns
Thank you @mnakalay - https://www.concrete5.org/profile/-/view/75201/...

The problem was when in version tab there is no user set.

$userName = "";
$u = new User();
if (is_object($u) && (int) $u->getUserID() > 0) {
    $ui = UserInfo::getByID($u->getUserID());
    if (is_object($ui)) {
        $userName = $ui->getUserName();
    }
}