Click here to Skip to main content
15,936,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm building a CMS. When Login button is pressed a menu should pop out. But instead I get a

Fatal error: Uncaught Error: Call to a member function load() on null in C:\Program Files\Ampps\www\CMS\app_main\app\login.php:38 Stack trace: #0 {main} thrown in C:\Program Files\Ampps\www\CMS\app_main\app\login.php on line 38


Here's the login file

include("init.php");

if (isset($_POST['username']))
{
    // get data
    $CMS->Template->setData('input_user', $_POST['username']);
    $CMS->Template->setData('input_pass', $_POST['password']);

    // validate data
    if ($_POST['username'] == '' || $_POST['password'] == '')
    {
        // show error
        if ($_POST['username'] == '') { $CMS->Template->setData('error_user', 'required'); }
        if ($_POST['password'] == '') { $CMS->Template->setData('error_pass', 'required'); }
        $CMS->Template->setAlert('Please fill in all required fields', 'error');
        echo '<script type="text/javascript">jQuery.colorbox.resize();</script>';
        $CMS->Template->load(APP_PATH . "core/views/v_login.php");
    }
    else if ($CMS->Auth->validateLogin($CMS->Template->getData('input_user'), $CMS->Template->getData('input_pass')) == FALSE)
    {
        // invalid login
        $CMS->Template->setAlert('Invalid username or password!', 'error');
        echo '<script type="text/javascript">jQuery.colorbox.resize();</script>';
        $CMS->Template->load(APP_PATH . "core/views/v_login.php");
    }
    else
    {
        // successful log in
        $_SESSION['username'] = $CMS->Template->getData('input_user');
        $_SESSION['loggedin'] = TRUE;
        $CMS->Template->load(APP_PATH . "core/views/v_loggingin.php");
    }
}
else
{
    $CMS->Template->load(APP_PATH . "core/views/v_login.php");
}


And here's the included init file

<?php

// create application settings
define("SITE_PATH", "http://localhost/CMS/app_main/%22);
define("APP_PATH", str_replace("\", "/", dirname(FILE)) . "/");

define("SITE_RESOURCES", "http://localhost/CMS/app_main/resources/%22);
define("APP_RESOURCES", "http://localhost/CMS/app_main//app/resources/%22);
define("SITE_CSS", "http://localhost/CMS/app_main/app/resources/css/style.css%22);

// database settings
$server = 'localhost';
$user = 'root';
$pass = 'mysql';
$db = 'cms';

// error reporting
mysqli_report(MYSQLI_REPORT_ERROR);

// create FlightPath core object
require_once(APP_PATH . "core/core.php");
$CMS = new CMS_Core($server, $user, $pass, $db);


core file where CMS is located

<?php 
//main CMS file and functionality
class CMS_Core{
    public $Template, $Auth, $Database;
 
    function __construct($server, $user, $pass, $db)
    {
        //db connect
        $this->Database= new mysqli($server, $user, $pass, $db);
        // create template object
        include(APP_PATH . "core/models/m_template.php");
 
        $this->$Template = new Template();
        $this->$Template->setAlertTypes(array('success', 'warning', 'error'));
        //create auth object
        include(APP_PATH . "core/models/m_auth.php");
        $this->Auth = new Auth();
 
        /* session start */
        session_start();
    }
    function __destruct()
    {
        $this->Database->close();
    }
 
    function head(){
        if($this->Auth->checkLoginStatus()){
            include(APP_PATH . "core/templates/t_head.php");
        }
        if (isset($_GET['login']) && $this->Auth->checkLoginStatus() == false){
            include(APP_PATH . "core/templates/t_login.php");
        }
    }
 
    function body_class(){
        if($this->Auth->checkLoginStatus()){
            echo "cms_editing";
        }
    }
 
    function toolbar(){
        if($this->Auth->checkLoginStatus()){
            include(APP_PATH . "core/templates/t_toolbar.php");
        }
    }
 
    function login_link(){
        if($this->Auth->checkLoginStatus()){
            echo"<a href='" . SITE_PATH . "'app/logout.php'>Logout</a>";
        }else{
            echo"<a href='?login'>Login</a>";
        }
    }
}


And the login file with it's code. Just in case.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Log in</title>
    <link rel="stylesheet" href="views/style.css">
</head>
<body>
    <h1> Log In </h1>
    <div id="content">

    <form action="" method="POST">
        <div>
        <?php 
          $alerts = $this->getAlerts();
          if ($alerts !=''){echo '<ul class="alerts">' . $alerts . '</ul>';}
         ?>
            <div class="row">
                <label for="username">Username: *</label>
                <input type="text" name="username" value="<?php echo $this->getData ('input_user')?>">
                <div class="error"><?php echo $this->getData ('error_user'); ?></div>
            </div>
            <div class="row">
                <label for="password">Password: </label>
                <input type="password" name="password" value="<?php echo $this->getData ('input_pass')?>">
                <div class="error"><?php echo $this->getData ('error_pass'); ?></div>
            </div>
            <div class="row">
                <p class="required"> required</p>
                <input type="submit" name="submit" class="submit" value="submit">
            </div>
        </div>

    </form>
    </div>
</body>
</html>


What I have tried:

I've tried changing fixing the file path to login file, and tried to see if something with the template file. So far nothing.
Posted
Updated 18-Nov-20 8:14am
v2

1 solution

The problem is that something is empty: probably $CMS->Template since that is what load is called on.

But which one, and why? That's the question - and we can't answer that without your code and data available and running...

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - run your program in the debugger (Xdebug - Debugger and Profiler Tool for PHP[^] may help) and when it fails, you can see the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900