Q: Call to member function on null - Freeform (Solspace) 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()-&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();
            }
        }
    );
Please read the article handling errors gracefully in Craft  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