<p>Please read the article <a href="https://nystudio107.com/blog/handling-errors-gracefully-in-craft-cms" rel="nofollow noreferrer">handling errors gracefully in Craft </a> it will explain to code defensively and what to do in those scenarios. Your <code>$user</code> variable is <code>null</code> and you can't execute functions on <code>null</code> variables </p>

CC BY-SA 4.0
2018-07-11T18:33:04.070Z
2018-07-11T18:33:04.070Z
Robin Schambach
<p>I am running Freeform 1.9.0 and I have a small plugin that is supposed to save user submission id's to a custom user field. The plugin returns error when it runs after a new user submits a form though.</p>

<p>Here is the error</p>

<blockquote>
  <p>Call to a member function getContent() on null</p>
</blockquote>

<p>Here is the code</p>

<pre><code>craft()-&gt;on(
        "freeform_submissions.onAfterSave",
        function (Event $event) {
            $submission = $event-&gt;params["model"];
            $isNew    = $event-&gt;params["isNew"];

            $submissionId = $submission-&gt;id;

            $user = craft()-&gt;userSession-&gt;getUser();

            $user-&gt;getContent()-&gt;submissionID = $submission-&gt;getId();
            $success = craft()-&gt;users-&gt;saveUser($user);

            if (!craft()-&gt;users-&gt;saveUser($user))
            {
                $errors = $user-&gt;getErrors();
            }
        }
    );
</code></pre>

<php><plugin-freeform>
Q: Call to member function on null - Freeform (Solspace)
# Q: Call to member function on null - Freeform (Solspace)

(Asked by: username_0 on 2018-07-11)

I am running Freeform 1.9.0 and I have a small plugin that is supposed to save user submission id's to a custom user field. The plugin returns error when it runs after a new user submits a form though.
Here is the error

Call to a member function getContent() on null

Here is the code
craft()->on(
        "freeform_submissions.onAfterSave",
        function (Event $event) {
            $submission = $event->params["model"];
            $isNew    = $event->params["isNew"];

            $submissionId = $submission->id;

            $user = craft()->userSession->getUser();

            $user->getContent()->submissionID = $submission->getId();
            $success = craft()->users->saveUser($user);

            if (!craft()->users->saveUser($user))
            {
                $errors = $user->getErrors();
            }
        }
    );

**Tags:** php, plugin-freeform

---

## 1 answers

### Answer by username_1 on 2018-07-11. Score: 1

Please read the article [handling errors gracefully in Craft](https://nystudio107.com/blog/handling-errors-gracefully-in-craft-cms)  it will explain to code defensively and what to do in those scenarios. Your `$user` variable is `null` and you can't execute functions on `null` variables


---

