Forum error - Call to a member function get_author_id() on null

Forum error - Call to a member function get_author_id() on null

by Liam Moran -
Number of replies: 7

Sven Laudel had reported this issue after an upgrade and that appears to be the only mention of it on the internet.

We had the same issue with a course that sprung up yesterday. In our case, teachers were unable to see the page load, although students could.

After many queries, log searches, and source code read-throughs, we concluded that a discussion within a forum had no discussion posts associated with it, throwing an error at line 136 of this file. After confirming this was definitely the case, we deleted that row in the forum_discussions table and the forum activity began working normally again.

Hope that helps someone down the line.

Average of ratings: Useful (2)
In reply to Liam Moran

Re: Forum error - Call to a member function get_author_id() on null

by Guillermo Dova -
Thank you, your advise help me to fix the error.
I have to do a little more digging:

1) enable debugging (developer) + show messages, + show SQL origins
2) look for an error indicating offset ID:
---->>>> Notice: Undefined offset: 7272 in /your-moodle-path/moodle/mod/forum/classes/local/builders/exported_discussion_summaries.php on line 136
3) open phpmyadmin or any DB query tool
query: SELECT * FROM mdl_forum_discussions where id = 7272
(replace with your PREFIX (mdl_) and ID found on step 2)
4) it should find 1 record, take note of field "firstpost", in my case ID 18807
5) query: SELECT * FROM mdl_forum_posts where id = 18807
(replace with your PREFIX (mdl_) and ID found on step 3)
6) it should find 1 record, take note of field "discussion". If it indicates 0 it's wrong.
Replace with ID found on step 2, in my case 7272
Check forum, it should work.

Hope it helps!
In reply to Liam Moran

Re: Forum error - Call to a member function get_author_id() on null

by Sohail Noor -
Hello, I am facing an issue with the forum. I have also tried all the solutions that you guys have suggested but seems like all the data is fine. I will share the details. Kindly if you can help please suggest something.

Notice: Undefined offset: 7582 in /var/www/html/kiu_new/kiu_ar/mod/forum/classes/local/builders/exported_discussion_summaries.php on line 228
Exception - Call to undefined function is_mod_forum\local\entities\author()

More information about this error

Debug info:
Error code: generalexceptionmessage×Dismiss this notification
Stack trace:
line 91 of /lib/classes/external/exporter.php: Error thrown
line 91 of /mod/forum/classes/local/exporters/discussion_summaries.php: call to core\external\exporter->__construct()
line 198 of /mod/forum/classes/local/factories/exporter.php: call to mod_forum\local\exporters\discussion_summaries->__construct()
line 151 of /mod/forum/classes/local/builders/exported_discussion_summaries.php: call to mod_forum\local\factories\exporter->get_discussion_summaries_exporter()
line 467 of /mod/forum/classes/local/factories/renderer.php: call to mod_forum\local\builders\exported_discussion_summaries->build()
line 225 of /mod/forum/classes/local/renderers/discussion_list.php: call to mod_forum\local\factories\renderer->mod_forum\local\factories\{closure}()
line 253 of /mod/forum/view.php: call to mod_forum\local\renderers\discussion_list->render()
In reply to Sohail Noor

Re: Forum error - Call to a member function get_author_id() on null

by Liam Moran -
This looks like it may be a slightly different problem. Can you post the full "get_latest_posts_authors" function from your mod/forum/classes/local/builders/exported_discussion_summaries.php ?

Your line numbers are offset by one from upstream, so it should go from line 225 to 234 in that file.
In reply to Liam Moran

Re: Forum error - Call to a member function get_author_id() on null

by Liam Moran -
I think it may first be this issue: https://tracker.moodle.org/browse/MDL-72417

That patch has not been incorporated into the canonical branches: it would be in line 102 in the 401 branch here: https://github.com/moodle/moodle/blob/MOODLE_401_STABLE/lib/classes/external/exporter.php

The patch also won't fix your problem, it'll just throw a new, slightly more informative error. The solution for that will be some kind of surgery to remove a database corruption: in this case, it looks like a missing user that's being referenced rather than an empty discussion forum.
In reply to Liam Moran

Re: Forum error - Call to a member function get_author_id() on null

by Sohail Noor -
I have debugged the records in the database and I have found some userid that user does not exist in the mdl_user table but I am not sure that this is the issue and if the issue is with the userid then how should I tackle this error?

As I am new to Moodle so if you can help then it will be great.

Thanks
In reply to Liam Moran

Re: Forum error - Call to a member function get_author_id() on null

by Sohail Noor -
Yes, actually I have tried many solutions and also the solutions that you guys suggested but it did not work.

Here is the full function:

/**
* Returns a mapped array of discussionid to the authors of the latest post
*
* @param array $latestposts Mapped array of discussion to latest posts.
* @return array Array of authors mapped to the discussion
*/
private function get_latest_posts_authors($latestposts) {
$authors = $this->vaultfactory->get_author_vault()->get_authors_for_posts($latestposts);

$mappedauthors = array_reduce($latestposts, function($carry, $item) use ($authors) {
$carry[$item->get_discussion_id()] = $authors[$item->get_author_id()];

return $carry;
}, []);
return $mappedauthors;
}