user1410081
user1410081

Reputation:

PHP: fatal error: call to a member function get() on non object

I got an error while running my code, it says call to a member function getBallparkDetailsStartDate() on a non-object.

if($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
            $ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);               
            $projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
            $projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
            $projectDetails["projectid"] = $projectId;
            $projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
            $projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE; 
        }

I got the error in this line: $projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();

Here is my other code:

public function __construct($ballparkDetailsId, $project, 
        $ballparkDetailsBookingRef, 
        $ballparkDetailsStartDate, $ballparkDetailsEndDate, 
        $ballparkDetailsExpiryDate, $ballparkDetailsDescription, 
        $ballparkDetailsNotes) {
    $this->ballparkDetailsId = $ballparkDetailsId;
    $this->project = $project;
    $this->ballparkDetailsBookingRef = $ballparkDetailsBookingRef;
    $this->ballparkDetailsStartDate = $ballparkDetailsStartDate;
    $this->ballparkDetailsEndDate = $ballparkDetailsEndDate;
    $this->ballparkDetailsExpiryDate = $ballparkDetailsExpiryDate;
    $this->ballparkDetailsDescription = $ballparkDetailsDescription;
    $this->ballparkDetailsNotes = $ballparkDetailsNotes;
}

public function getBallparkDetailsId() {
    return $this->ballparkDetailsId;
}

public function getProject() {
    return $this->project;
}

public function getBankName() {
    return $this->getProject()->getBankName();
}

public function getBankRef() {
    return $this->getProject()->getBankRef();
}

public function getRegionName() {
    return $this->getProject()->getRegionName();
}

public function getProjectStatusName() {
    return $this->getProject()->getProjectStatusName();
}

public function getBallparkDetailsBookingRef() {
    return $this->ballparkDetailsBookingRef;
}

public function getBallparkDetailsStartDate() {
    return $this->ballparkDetailsStartDate;
}

public function getBallparkDetailsEndDate() {
    return $this->ballparkDetailsEndDate;
}

public function getBallparkDetailsExpiryDate() {
    return $this->ballparkDetailsExpiryDate;
}

public function getBallparkDetailsDescription() {
    return $this->ballparkDetailsDescription;
}

public function getBallparkDetailsNotes() {
    return $this->ballparkDetailsNotes;
}

public function getProjectId() {
    return $this->getProject()->getProjectId();
}

public function getProjectStatusId() {
    return $this->getProject()->getProjectStatusId();
}

}
?>

The last time I check this it ran well. But now I don't know what's wrong with this? Please help me find the error. Thanks.

Upvotes: 0

Views: 1644

Answers (2)

LSerni
LSerni

Reputation: 57463

Apparently

$ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);

is not returning a "ballpark" at all. Probably it is returning an error, or something like an empty array.

Try var_dump()'ing $ballpark immediately before the line that raises the error, and see what it contains (probably False, NULL, array() or something equally un-ballparky.

Then, inspect the ballparkDetailsByProjectId() function in the BallparkDetailsHandler.php file. At a guess, you might be passing an invalid (i.e. nonexistent, removed, etc.) $projectId.

Then you might rewrite the code with error checking:

if($projectStatusId == ProjectStatusKeys::BALLPARK_ACTIVE) {
        $ballpark = $this->ballparkDetailsHandler->getBallparkDetailsByProjectId($projectId);
        if (!is_object($ballpark))
            trigger_error("Error: bad project ID: '$projectId': $ballpark",
                E_USER_ERROR);

        $projectDetails["startdate"] = $ballpark->getBallparkDetailsStartDate();
        $projectDetails["enddate"] = $ballpark->getBallparkDetailsEndDate();
        $projectDetails["projectid"] = $projectId;
        $projectDetails["name"] = $ballpark->getBallparkDetailsBookingRef();
        $projectDetails["status"] = ProjectStatusKeys::BALLPARK_ACTIVE; 
    }

Then in the BallparkDetailsHandler.php file you could modify this code:

// Prepare query or die
if (!($stmt = $this->mysqli->prepare($query))
    return "Error in PREPARE: $query";

$stmt->bind_param("i", $projectId);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($ballparkDetailsBookingRef, $bankRef, $regionName,
     $projectStatusId, $projectStatusName,  $ballparkDetailsDescription,
     $ballparkDetailsNotes, $ballparkDetailsStartDate, $ballparkDetailsEndDate,
     $ballparkDetailsExpiryDate);
$stmt->fetch();

// If no data, then die
if(!$stmt->num_rows)
    return "No data in DB for projectID '$projectId': $query";

// Should be clear sailing from here on. Actually I ought to check
// whether all these new() here do return anything sensible, or not

$bank = new Bank("", "", $bankRef, "");
$region = new Region("", $regionName, "");
$projectStatus = new ProjectStatus($projectStatusId, $projectStatusName);
$project = new Project($projectId, $bank, $region, $projectStatus);

return new BallparkDetails("", $project,
    $ballparkDetailsBookingRef, $ballparkDetailsStartDate, 
    $ballparkDetailsEndDate, $ballparkDetailsExpiryDate, 
    $ballparkDetailsDescription, $ballparkDetailsNotes);

Upvotes: 1

SDC
SDC

Reputation: 14212

$ballpark clearly doesn't contain the object you think it does on the line with the error. In fact, it obviously doesn't contain an object at all.

This implies that the preceding line (which sets $ballpark) isn't working properly. It would appear that it's returning a value that is is not an object.

I can't tell what that value is -- it could be null, or it could be an integer, string, array, etc. But whatever it is, it isn't a ballpark object.

I suggest you look at your getBallparkDetailsByProjectId() method to find the source of this problem.

Upvotes: 0

Related Questions