title: Why do I get "Call to a member function on null" error in CodeIgniter with call from Controller to Library function?
body: <p>I'm trying to call a library function from my controller, but getting an error </p>
  
  <blockquote>
    <p>Exception: Call to a member function items_upload() on null /usr/local/www/public_html/pos_clcdesq/application/controllers/Config.php 1022</p>
  </blockquote>
  
  <p>I think this error usually means that the controller doesn't have access to the library function, but as you can see from my constructor, I loaded the library.  What am I missing here?</p>
  
  <p>Controller constructor:</p>
  
  <pre><code>public function __construct()
  {
      parent::__construct('config');
  
      $this-&gt;load-&gt;library('barcode_lib');
      $this-&gt;load-&gt;library('sale_lib');
      $this-&gt;load-&gt;library('Clcdesq_integration_lib');
  }
  </code></pre>
  
  <p>Controller initial_items_upload function:</p>
  
  <pre><code>public function initial_items_upload()
  {
      $this-&gt;Clcdesq_integration_lib-&gt;items_upload();
  }
  </code></pre>
  
  <p>Library constructor and items_upload function:</p>
  
  <pre><code>class Clcdesq_integration_lib
  {
  private $CI;
  private $api_key;
  private $api_url;
  
  /**
   * Constructor
   */
  public function __construct($api_key = '')
  {
      $this-&gt;CI =&amp; get_instance();
  
      $this-&gt;api_key  = $this-&gt;CI-&gt;encryption-&gt;decrypt($this-&gt;CI-&gt;Appconfig-&gt;get('clcdesq_api_key'));
      $this-&gt;api_url  = $this-&gt;CI-&gt;encryption-&gt;decrypt($this-&gt;CI-&gt;Appconfig-&gt;get('clcdesq_api_url'));
  }
  
  public function items_upload()
  {
      $all_items = json_decode(json_encode($this-&gt;CI-&gt;Item-&gt;get_all(), JSON_UNESCAPED_UNICODE), true);
  
      foreach($all_items as $item)
      {
          $item_data[] = json_decode(json_encode($this-&gt;CI-&gt;Item-&gt;get_info($item['item_id']), JSON_UNESCAPED_UNICODE), true);
      }
  
      $pushdata   = $this-&gt;populate_api_data($item_data);
  
      if (version_compare(phpversion(), '7.1', '&gt;='))
      {
          ini_set( 'precision', 17 );
          ini_set( 'serialize_precision', -1 );
      }
  
      $json = json_encode($pushdata, JSON_UNESCAPED_UNICODE);
  
      $clcdesq_guid = $this-&gt;send_data($this-&gt;api_url, $this-&gt;api_key, $json);
  
      log_message("ERROR", "New Product JSON Results: $json");
      log_message("ERROR", "API Results: $clcdesq_guid");
  }
  </code></pre>
  
  <p>I'm calling the function from the view:</p>
  
  <pre><code>        &lt;div class="form-group form-group-sm"&gt;
              &lt;?php echo form_label($this-&gt;lang-&gt;line('config_clcdesq_utilities'), 'config_clcdesq_items_upload', array('class' =&gt; 'control-label col-xs-2')); ?&gt;
              &lt;div class='col-xs-2'&gt;
                  &lt;div id="items_upload" class="btn btn-default btn-sm"&gt;
                      &lt;span style="top:22%;"&gt;&lt;?php echo $this-&gt;lang-&gt;line('config_clcdesq_items_upload'); ?&gt;&lt;/span&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
  $("#items_upload").click(function() {
      window.location='&lt;?php echo site_url('config/initial_items_upload') ?&gt;';
  });
  </code></pre>