Call to a member function get() on null error in CodeIgniter
The error Fatal error: Call to a member function get() on null is one of the most frustrating runtime exceptions in PHP, particularly when working with legacy frameworks like CodeIgniter 2. This specific failure indicates that your code is attempting to call a method on an object instance that does not exist. In the context of your site_model.php, the culprit is almost certainly $this->db.
You are trying to execute $this->db->get('test') inside the getAll() method, but $this->db has not been initialized yet. This happens because CodeIgniter's Database Driver (DBCI) is lazy-loaded. It does not automatically instantiate a database connection when you extend the CI_Model class; it only creates that instance when you explicitly request it via $this->load->db() or if you have configured the system to autoload the DB driver. Since your model file lacks an explicit call to load the DB library, $this->db remains null.
To resolve this, you have two primary architectural choices depending on how you want to structure your application logic. The first approach involves explicitly loading the database library within the model itself. You can modify the getAll() function in site_model.php to ensure the connection exists before attempting any queries:
class Site_model extends CI_Model {
function getAll() {
// Explicitly load the DB library if it hasn't been loaded yet
$this->load->db();
$q = $this->db->get('test');
if ($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
return null; // Or handle the empty case appropriately
}
}
While this fixes the immediate error, it introduces a slight redundancy. If you are using CodeIgniter 2, the standard practice for models is to rely on the framework's configuration files (application/config/database.php) to define your database settings globally. However, simply defining these settings isn't enough to auto-load the connection object into every model instance without an explicit load call or a specific autoload setting.
A more robust pattern often favored in CodeIgniter projects involves moving the logic that requires the database connection out of the model if the model is intended to be agnostic, or ensuring the $_config array in your controller handles the initialization correctly. However, if you intend for site_model to be a utility for fetching data, explicitly loading the DB library inside the method (as shown above) is the safest quick fix without altering global configuration settings.
There is also a secondary issue in your view logic that could cause confusion if not handled carefully. In your site_model.php, you initialize $data[] = $row; but then return $data. If the query returns zero rows (num_rows() == 0), your function currently falls off the end of the block without a return statement, potentially returning an empty array or causing issues depending on how strict PHP is configured. It is best practice to explicitly return null or an empty array in that branch to prevent unexpected behavior downstream.
Furthermore, consider the implications of your controller code:
$data['records'] = $this->site_model->getAll();
$this->load->view('home', $data);
If getAll() returns null because no rows were found (or if the DB wasn't loaded correctly), printing this in your view with print_r($records) will output NULL. This is harmless visually but might indicate a logic gap where you expected data to exist. If you are migrating from CodeIgniter 2 to a modern stack like Laravel, the syntax for fetching data changes significantly (DB::table('test')->get() vs $this->db->get('test')), and the strict typing of return types in newer languages makes these null-reference errors even harder to miss. For teams transitioning between frameworks or looking for best practices on handling database connections, resources like https://laravelcompany.com offer valuable comparisons on how modern PHP frameworks abstract these connection lifecycles differently.
Ultimately, the error stems from a mismatch between object expectation and object initialization. By ensuring $this->db is instantiated before it is usedâeither via explicit loading in the model or by verifying your autoload settingsâyou eliminate the possibility of calling methods on a null value.