Welcome Guest, Not a member yet? Register   Sign In
Trying to get property of non-object
#1

[eluser]GesterX[/eluser]
I'm sure this is something trivial but I've spent the last hour scratching my head on this one!

I have several functions in my Model that all look like this (that find various things):

Code:
function getNPCName($npc)
    {
      $this->db->select('name')->from('npcs')->where('name', $npc)->limit(1);
      $query = $this->db->get();
      $row = $query->row();
      return $row->name;
    }

Then in my controller they are put in the view like this:

Code:
function getNPCData($npc)
    {
      $npcDialogue = $this->NPCModel->getNPCDialogue($npc);
      $npcQuestDialogue = $this->NPCModel->getNPCQuestDialogue($npc);
      $npcNoQuestDialogue = $this->NPCModel->getNPCNoQuestDialogue($npc);
      $npcQuestCompleteDialogue = $this->NPCModel->getNPCQuestCompleteDialogue($npc);
      $npcName = $this->NPCModel->getNPCName($npc);
      $npcQuest = $this->NPCModel->getNPCQuest($npc);

      $npcData = array(
                        'npcDialogue' => $npcDialogue,
                        'npcQuestDialogue' => $npcQuestDialogue,
                        'npcNoQuestDialogue' => $npcNoQuestDialogue,
                        'npcQuestCompleteDialogue' => $npcQuestCompleteDialogue,
                        'npcName' => $npcName,
                        'npcQuest' => $npcQuest
                        );

      $statData = $this->PlayerModel->createPlayerInfoArray();
      $this->load->view('game/statpanelView',$statData);
      $this->load->view('game/npcView', $npcData);
    }

The function is called from a different view (the game view) like this:
Code:
foreach ($listnpcs as $value)
     {
      echo anchor('npc/getNPCData/' . $value['name'], $value['name']). "<br>";
     }

Finally, the npc view looks like this:

Code:
echo "<b>" . $npcName . "</b><br><br>";
     echo "<b>" . $npcDialogue . "</b><br><br>";

So if my NPC is called "Bob the Scrapyard Man"; my "getNPCData()" function is called with the $npc parameter set as "Bob the Scrapyard Man". This means that the model functions are called to find the various data for Bob. However, when the npc view is loaded I get the error "Trying to get property of non-object" for every method in the controller!

Bob the Scrapyard Man is definitely in my npc table and all the other fields have data assigned. I tried calling $row = $query->result() but got the same error.

Any ideas? I'm sure this is something simple I've missed.
Thanks in advance!
#2

[eluser]InsiteFX[/eluser]
Did you load the database and your model?

InsiteFX
#3

[eluser]GesterX[/eluser]
[quote author="InsiteFX" date="1303695791"]Did you load the database and your model?

InsiteFX[/quote]

Yup. The wierd thing is that I have a very similar function in a different model which works completely fine. The constructor for both Models are identical:

Code:
function __construct() {
        parent::__construct();
        $this->load->library(array("session", "form_validation"));
        $this->load->helper(array('url'));
        $this->load->model('NPCModel', '', TRUE);
        $this->load->model('PlayerModel', '', TRUE);
    }

The db is loaded automatically and works fine on my other pages...
#4

[eluser]InsiteFX[/eluser]
I would run the CI Profiler to see what is going on, you could also changing the logging in appliication/config/config.php to like 4 and turn logging on to also see whats going on.

InsiteFX
#5

[eluser]GesterX[/eluser]
[quote author="InsiteFX" date="1303700852"]I would run the CI Profiler to see what is going on, you could also changing the logging in appliication/config/config.php to like 4 and turn logging on to also see whats going on.

InsiteFX[/quote]

I'm reasonably new to CI. How do I go about turning on and using the profiler?
#6

[eluser]osci[/eluser]
userguide - errors
userguide - profiling
#7

[eluser]InsiteFX[/eluser]
Code:
// Enable Profiler - Place in your controller!
$this->output->enable_profiler(TRUE);

// Disable Profiler
$this->output->enable_profiler(FALSE);

InsiteFX
#8

[eluser]GesterX[/eluser]
[quote author="InsiteFX" date="1303705029"]
Code:
// Enable Profiler - Place in your controller!
$this->output->enable_profiler(TRUE);

// Disable Profiler
$this->output->enable_profiler(FALSE);

InsiteFX[/quote]

After running the profiler I spotted the error. The spaces in the name were being turned into 's. I cleaned this up with the str_replace php function.

Thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB