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 Class Review

Help to previous Packages Challenge Task 4 of 4

I keep getting this error:

./Display.java:1: error: cannot access BlogPost
import com.example.BlogPost;
                  ^
  bad source file: ./com/example/BlogPost.java
    file does not contain class com.example.BlogPost
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.
1 error

In the DIsplay.java file I have the following code:

import com.example.BlogPost;

public class Display {
  public static void main(String[] args) {
    // Your code here...
    BlogPost blogPost = new BlogPost();
    System.out.printf("This is a blog post: %s", blogPost);
  }
}

And the code in the com/example/BlogPost.java I have the following code:

package com.example.BlogPost;

public class BlogPost {
}

What am I doing wrong? The error output.html file is suggesting that there is something wrong with the subdirectory of the sourcepath.

5 Answers

SPOILER

So to clear everything out, this is how this code challenge should be solved:

The Display class:

import com.example.BlogPost;

public class Display {
  public static void main(String[] args) {
    // Your code here...
    BlogPost blogPost = new BlogPost();
    System.out.printf("This is a blog post: %s", blogPost);
  }
}

The BlogPost class:

package com.example;

public class BlogPost {
}

Agreed the "import" call needs the ".BlogPost" at the end, that is what did it for me, also the challenge 3 of 4 should not be passable with "import com. example" seeing as how "import com.example.BlogPost" is defiantly what the final challenge needs to pass. Thank you so much for working through this with me Poltexious

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

The package definition should be:

package com.example;

Whooops! I think I just need to wait until tomorrow to continue with this course! With this kind of mistakes, I should probably go and get some sleep instead.

Thank you anyway, Craig Dennis :-)

I am also getting an interesting error in the final challenge of the package lesson, I am making the new package with

package com.example;

And that all works, when I come to the final part of the challenge when I instantiate the new blog post using:

import package com.example;

as the import call and this as the instantiate call:

BlogPost blogPost = new blogPost();
System.out.printf("This is a BlogPost: %s", blogPost);

I can't seem to find anything that would cause these errors?

./Display.java:1: error: expected import package com.example; ^ ./Display.java:1: error: package .com does not exist import package com.example; ^ ./Display.java:4: error: cannot find symbol BlogPost blogPost = new blogPost(); ^ symbol: class BlogPost location: class Display ./Display.java:4: error: cannot find symbol BlogPost blogPost = new blogPost(); ^ symbol: class blogPost location: class Display 4 errors

Craig Dennis
Craig Dennis
Treehouse Teacher

Remove the package keyword from your import statement.

It is not working

JavaTester.java:65: error: illegal start of expression
      package com.example;
      ^
JavaTester.java:65: error: not a statement
      package com.example;
                 ^
2 errors

Change the following:

 Display.Java
import com.example;

to this:

import com.example.BlogPost;

Try this:

BlogPost blogPost = new BlogPost();

instead of:

BlogPost blogPost = new blogPost();

You are trying to instantiate a blogPost object from the blogPost class which does not exist. It has to be from the BlogPost class.

Okay, I understand why i wasn't able to instantiate, and craig, After trying to do it without "package" in my import statement, im still being thrown an error, i dont understand what im doing wrong everything seems to match up. one my time my code goes as follows

 Display.Java
import com.example;
public class Display {
  public static void main(String[] args) {
    // Your code here...\
    BlogPost blogPost = new BlogPost();
    System.out.printf("This is a blog post: %s", BlogPost);
  }
}
package com.example;
public class BlogPost {
}

error is :

./Display.java:1: error: cannot find symbol import com.example; ^ symbol: class example location: package com ./Display.java:5: error: cannot find symbol BlogPost blogPost = new BlogPost(); ^ symbol: class BlogPost location: class Display ./Display.java:5: error: cannot find symbol BlogPost blogPost = new BlogPost(); ^ symbol: class BlogPost location: class Display ./Display.java:6: error: cannot find symbol System.out.printf("This is a blog post: %s", BlogPost); ^ symbol: variable BlogPost location: class Display 4 errors

package com.example;

Can you please fix your markdown code?

From what I can see you have typed the following:

System.out.printf("This is a blog post: %s", BlogPost);

You have to type in the following instead:

System.out.printf("This is a blog post: %s", blogPost);

What you are doing wrong in the System.out.printf(...) method is that you are not making a reference to your instantiated object blogPost, instead you are making a reference to BlogPost (which is your class), and you are not supposed to do that there.

In your previous post you actually did the right thing in the System.out.printf(...) method :-)

Even with the blogPost in system.out.printf this is my error and i havent had a problem with anything yet, not like this where i couldn't figure out realatively quick

Error in syntax: ./Display.java:1: error: cannot find symbol import com.example; ^ symbol: class example location: package com ./Display.java:5: error: cannot find symbol BlogPost blogPost = new BlogPost(); ^ symbol: class BlogPost location: class Display ./Display.java:5: error: cannot find symbol BlogPost blogPost = new BlogPost(); ^ symbol: class BlogPost location: class Display 3 errors