Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Getting There Type Casting

Binyamin Friedman
Binyamin Friedman
14,615 Points

Cannot find symbol

I'm getting errors: ./TypeCastChecker.java:18: error: cannot find symbol result = (BlogPost)obj.getTitle; ^ symbol: variable getTitle location: variable obj of type Object ./TypeCastChecker.java:18: error: incompatible types: BlogPost cannot be converted to String result = (BlogPost)obj.getTitle; ^ 2 errors

com/example/BlogPost.java
package com.example;

import java.util.Date;

public class BlogPost {
    private String mAuthor;
    private String mTitle;
    private String mBody;
    private String mCategory;
    private Date mCreationDate;

    public BlogPost(String author, String title, String body, String category, Date creationDate) {
      mAuthor = author;
      mTitle = title;
      mBody = body;
      mCategory = category;
      mCreationDate = creationDate;
    }

    public String getAuthor() {
      return mAuthor;
    }

    public String getTitle() {
      return mTitle;
    }

    public String getBody() {
      return mBody;
    }

    public String getCategory() {
      return mCategory;
    }

    public Date getCreationDate() {
      return mCreationDate;
    }
}
TypeCastChecker.java
import com.example.BlogPost;

public class TypeCastChecker {
  /***************
  I have provided 2 hints for this challenge.
  Change `false` to `true` in one line below, then click the "Check work" button to see the hint.
  NOTE: You must set all the hints to false to complete the exercise.
  ****************/
  public static boolean HINT_1_ENABLED = false;
  public static boolean HINT_2_ENABLED = false;

  public static String getTitleFromObject(Object obj) {
    // Fix this result variable to be the correct string.
    String result = "";
    if(obj instanceof String) {
      result = (String)obj;
    } else if(obj instanceof com.example.BlogPost) {
      result = (BlogPost)obj.getTitle;
    }
    return result;
  }
}

2 Answers

Leandro Botella Penalva
Leandro Botella Penalva
17,618 Points

Hi Binyamin,

First of all you missed the set of parenthesis after

(BlogPost)obj.getTitle;

So it should be:

(BlogPost)obj.getTitle();

Also, you need a more specific cast. What you wanted is convert the obj variable to BlogPost however is not doing that. It's converting what obj.getTitle() returns to BlogPost instead, that is why it shows the error "BlogPost cannot be converted to String", because you are trying to assign to the result String variable something converted to BlogPost.

To fix that, you need to use another set of parenthesis around the casting and the variable you want to convert like this:

((BlogPost)obj).getTitle();

In this way it first converts the obj variable to BlogPost and then it calls the method and returns the title String.

The first if is correct. But there are a couple problems in your else if block.

Instead of checking if the object is an instance of com.example.BlogPost, you want to do:

else if (obj instanceof BlogPost) {

}

In this block you're calling the getTitle method on the post, but that method will only work on a certain type. So first you need to cast the obj as a BlogPost, then you can call the getTitle method on it.

So one way you can do that is to create a new variable to store the obj of type BlogPost, then set result equal to that object with getTitle called on it.

Example:

else if (obj instanceof BlogPost) { BlogPost post = (BlogPost)obj; result = post.getTitle(); }