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 trialVaibhav Yaramwar
Courses Plus Student 4,292 PointsI am getting error while solving task
Note from 2017-03-21 09:39:12.518
Describe your new note here.
I am getting below error Oops! It looks like Task 1 is no longer passing.
Question is :
Now make sure that if a com.example.BlogPost is passed in for obj that you then cast it to a BlogPost. Return the results of the getTitle method on the newly type-casted BlogPost instance.
I've included BlogPost.java for your reference only
My Program :
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((String) obj instanceof String)
{
result = (String) obj;
}
if((BlogPost) obj instanceof BlogPost)
{
BlogPost blogpost = (BlogPost) obj;
result = blogpost.getTitle();
}
return result;
} }
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;
}
}
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((String) obj instanceof String)
{
result = (String) obj;
}
if((BlogPost) obj instanceof BlogPost)
{
BlogPost blogpost = (BlogPost) obj;
result = blogpost.getTitle();
}
return result;
}
}
2 Answers
Simon Coates
28,694 Pointsthe following seems to work:
public static String getTitleFromObject(Object obj) {
// Fix this result variable to be the correct string.
String result =null;
if( obj instanceof String) {
result = (String) obj;
}
if( obj instanceof BlogPost) {
BlogPost blogpost = (BlogPost) obj;
result = blogpost.getTitle();
}
return result;
}
I'll take a look at your error and try and give you an explanation. Update: the first problem is that your use of cast will attempt to cast any object received by the method to a string. I've created a demo of the problem.
class Main {
public static void main(String[] args) {
getTitleFromObject("this will work"); // WORKS
getTitleFromObject(new Integer(5)); //CRASHES USING DIFFERENT OBJECT TYPE
}
public static String getTitleFromObject(Object obj) {
String result ="";
if((String) obj instanceof String)//CRASH OCCURS ON THIS LINE AS IT SEEMS
//TO ATTEMPT TO CAST TO STRING REGARDLESS OF TYPE.
{
result = (String) obj;
}
return result;
}
}
Vaibhav Yaramwar
Courses Plus Student 4,292 PointsOhhhh sorry I haven't seen that....will check that.Thank You very much for your help
Vaibhav Yaramwar
Courses Plus Student 4,292 PointsThank You Simon....Your Solution worked and I am able to solve the Quize
Simon Coates
28,694 Pointsok, good.
Vaibhav Yaramwar
Courses Plus Student 4,292 PointsVaibhav Yaramwar
Courses Plus Student 4,292 PointsHi Simon, I have successfully solved first problem but getting error for second....please give solution for second problem
Simon Coates
28,694 PointsSimon Coates
28,694 PointsVaibhav Yaramwar . The first code snippet I included is the solution to both parts of the challenge. And you were only able to pass the first challenge because the treehouse challenge verification code was only using a string value. Your use of casting in the conditional statement is flawed. The aim is to test the object type, and AFTERWARDS perform a cast safely. The problem with your code is:
if((String) obj instanceof String)
This converts the obj to a string. If it is a BlogPost, then it can't be converted (the code crashes). The correct line is
if(obj instanceof String)
This confirms that the obj is a String and as such, it is able to be cast to a String type (inside the {}).