title: Fatal error: Uncaught Error: Call to a member function prepare() on null Stack trace:Database->query('SELECT jobs .*,...')Job->getAllJobs()
body: <p>I get the output if i remove <code>// $template-&gt;jobs = $job-&gt;getAllJobs(); //</code> but it is not what i expected as it  does not take the data from the database.. I`have tried most of the answers from this page related to this problem but still get this kind of error..</p>
  
  <pre><code>&lt;?php
  
  class Database{
  
      private $host = DB_HOST;
      private $user = DB_USER;
      private $pass = DB_PASS;
      private $dbname = DB_NAME;
  
      private $dbh;
      private $error;
      private $stmt;
  
  
  
      public function __construct(){
          //set DSN
  
          $dsn= 'mysqli:host=' .$this-&gt;host .';dbname='. $this-&gt;dbname;
          $options = array(
                  PDO::ATTR_PERSISTENT =&gt; true,
                  PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION 
          );
  
          //PDO Instances
          try{
          $this-&gt;dbh = new PDO($dsn, $this-&gt;user, $this-&gt;pass, $options);       
          } catch(PDOException $e) {
              $this-&gt;error = $e-&gt;getMessage();
          }
      }  
  
      public function query($query){
          $this-&gt;stmt = $this-&gt;dbh-&gt;prepare($query);
      }
      //bind values after obtaining the values
      public function bind($param, $value, $type = null){
          if(is_null($type)){
              switch(true){
                  case is_int ( $value ) :
                  $type = PDO::PARAM_INT;
                  break;
                  case is_bool ( $value ) :
                  $type = PDO::PARAM_BOOL;
                  break;
                  case is_null( $value ) :
                  $type = PDO::PARAM_NULL;
                  break;
                  default:
                  $type = PDO::PARAM_STR;
              }
          }
          $this-&gt;stmt-&gt;bindValue($param, $value, $type);
      }
  
      public function execute(){
      return $this-&gt;stmt-&gt;execute();          
      }
  
      public function resultSet(){
          $this-&gt;execute();
          return $this-&gt;stmt-&gt;fetchAll(PDO::FETCH_OBJ);
      }
      public function single(){
          $this-&gt;execute();
          return $this-&gt;stmt-&gt;fetch(PDO::FETCH_OBJ);
      }
    }
  //Code for job class
  
  &lt;?php
    class Job{
      private $db;
  
      public function __construct(){
        $this-&gt;db = new Database();
      }
      //get all jobs
      public function getAllJobs(){
  
        $this-&gt;db-&gt;query("SELECT jobs .*, categories.name AS cname FROM jobs NATURAL JOIN categories ON jobs.category_id = categories.id ORDER BY post_date DESC "); //jobs.category_id==foreignkey
        $results = $this-&gt;db-&gt;resultSet();
        return $results;
     }
  }
  
  ///code that caused problem
  $job = new Job;
  $template-&gt;jobs = $job-&gt;getAllJobs();
  </code></pre>
  
  <p>The error occured as:    </p>
  
  <blockquote>
    <p>Fatal error: Uncaught Error: Call to a member function prepare() on null in<br>
    C:\xampp\htdocs\project\lib\database.php:32 Stack trace: #0<br>
    C:\xampp\htdocs\project\lib\job.php(11): Database->query('SELECT jobs .*,...') #1<br>
    C:\xampp\htdocs\project\homejoblister.php(7): Job->getAllJobs() #2 {main} thrown in<br>
    C:\xampp\htdocs\project\lib\database.php on line 32</p>
  </blockquote>