Call to a Member Function on Null

Fatal error: Uncaught Error: Call to a member function getName() on null

Quick Answer

You are calling a method on a variable that is null instead of an object. Check that the variable is properly initialized and that any function returning it does not return null.

Why This Happens

This error occurs when you try to call a method on a variable that holds null instead of an object instance. It commonly happens when a database query returns null, a find method fails to locate a record, or an object was never instantiated.

The Problem

$user = $repository->find(999); // Returns null
echo $user->getName();

The Fix

$user = $repository->find(999);
if ($user !== null) {
    echo $user->getName();
} else {
    echo 'User not found';
}

Step-by-Step Fix

  1. 1

    Identify the null variable

    Check which variable is null by looking at the method call mentioned in the error and the line number.

  2. 2

    Trace where it should be set

    Find where the variable gets its value. Check if a function could return null, if a conditional branch skips assignment, or if a query returned no results.

  3. 3

    Add a null check

    Add a null check before calling the method, use the nullsafe operator ?-> in PHP 8+, or throw a meaningful exception if null is unexpected.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free