title: Fatal error: Call to a member function getName() In magento
body: <p>i am trying to Create a Custom module ( A Copy Paste From Definitive Guide ) to Show random Products on the Home Page. I Started With Hello world It was Fine. But when I added More Code to Show Products On Home Page It Is Giving Me The Following Error.</p>
  
  <p><code>Fatal error: Call to a member function getName() on a non-object in /opt/lampp/htdocs/magento/app/code/local/Magentotutorial/Definitivehello/Block/Randomproducts.php on line 12</code></p>
  
  <p>here goes my code for <code>/app/code/local/Magentotutorial/Definitivehello/Block/Randomproducts.php</code></p>
  
  <pre><code>&lt;?php
  class Magentotutorial_Definitivehello_Block_Randomproducts 
      extends Mage_Core_Block_Template
  {
      protected function _toHtml()
      {
          $randProdModel = Mage::getModel('Magentotutorial_Definitivehello/Randomproducts');
          $randProducts = $randProdModel-&gt;getRandomProducts();
          $html = "&lt;ul&gt;";
          foreach ($randProducts as $product) {
              $name = $product-&gt;getName();
              $price = number_format($product-&gt;getPrice(), 2);
              $imageLink = $this-&gt;helper('catalog/image')-&gt;init($product, 'thumbnail')-&gt;resize(100,100);
              $productLink = $this-&gt;helper('catalog/product')-&gt;getProductUrl($product);
              $html .= "&lt;p&gt;&lt;a href='$productLink'&gt;&lt;img src='$imageLink' alt='$name'/&gt;&lt;/a&gt;&lt;br/&gt; $name &lt;br/&gt; $price &lt;/p&gt;";
          }
          $html .= "&lt;ul&gt;";
          return $html;
      }
  }
  </code></pre>
  
  <p>Can anyone Tell Me Whats Wrong. Thanks</p>
  
  <p>Here Goes My Code For GetRandomProducts() method</p>
  
  <pre><code>&lt;?php
  class Magentotutorial_Definitivehello_Model_Randomproducts 
      extends Mage_Core_Model_Abstract
  {
      public function getRandomProducts($maxCount = 3)
      {
          $randProducts = array();
          $allProducts = array();
          $productCollection = Mage::getModel('catalog/product')
              -&gt;getCollection()
              -&gt;addAttributeToSelect('*')
              -&gt;getItems();
  
          foreach ($productCollection as $id =&gt; $data) {
              //print_r($data); exit;
              $allProducts[] = $data;
          }
  
          $productIds = array_keys($allProducts);
          $totalProductIds = count($productIds);
          for ($i=0; $i&lt;$maxCount; $i++) {
              $randIndex = rand(0,$totalProductIds);
              $randProductId = $productIds[$randIndex];
              $randProducts[] = $allProducts[$randProductId];
          }
          return $randProducts;
      }
  }
  </code></pre>