Using $post->post_name in body id causing error: Trying to get property of non-object

A better method for what you are trying to achieve

This answer is not about the php error you are getting. You need these classes for styling your website right?

Put the following code in your functions.php

// A better body class
function condensed_body_class($classes) {
    global $post;

    // add a class for the name of the page - later might want to remove the auto generated pageid class which isn't very useful
    if( is_page()) {
        $pn = $post->post_name;
        $classes[] = "page_".$pn;
    }

    if( !is_page() ) {          
        $pn = $post->post_name;
        $classes[] = "post_".$pn;
    }

    if ( $post && $post->post_parent ) {
        // add a class for the parent page name
        $post_parent = get_post($post->post_parent);
        $parentSlug = $post_parent->post_name;

        if ( is_page() && $post->post_parent ) {
                $classes[] = "parent_".$parentSlug;
        }

    }

    // add class for the name of the custom template used (if any)
    $temp = get_page_template();
    if ( $temp != null ) {
        $path = pathinfo($temp);
        $tmp = $path['filename'] . "." . $path['extension'];
        $tn= str_replace(".php", "", $tmp);
        $classes[] = "template_".$tn;
    }
    return $classes;
}


/* a better body class */
add_filter( 'body_class', 'condensed_body_class' );

And where you need the body class, put this:

<body <?php body_class(); ?>>

You will see that so many useful class information will be printed.

Good Luck!