Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't load image through the given file path #45

Closed
lawncegoh opened this issue Sep 5, 2019 · 9 comments
Closed

Can't load image through the given file path #45

lawncegoh opened this issue Sep 5, 2019 · 9 comments

Comments

@lawncegoh
Copy link

lawncegoh commented Sep 5, 2019

Have these 2 lines in my duke main class.

private Image user = new Image(this.getClass().getResourceAsStream("/images/DaUser.png "));
private Image duke = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png "));

These are the file paths:

/Users/lawnce/Desktop/duke/src/main/resources/images/DaUser.png
/Users/lawnce/Desktop/duke/src/main/resources/images/DaDuke.png

This is the error i encounter. I tried solving through the Slack channel's way of renaming the file directly to resources or adding / in front of images but it doesn't work somehow

Screenshot 2019-09-05 at 11 29 28 AM

@damithc
Copy link
Collaborator

damithc commented Sep 5, 2019

Can you post the code to a branch and give the link here?

@lawncegoh
Copy link
Author

Hi Prof,

This is the link my duke.java

https://github.com/lawncegoh/duke/blob/master/src/main/java/Duke.java

@damithc
Copy link
Collaborator

damithc commented Sep 6, 2019

Just one file is not enough. Someone should be able to reproduce the problem by cloning your repo. Are all the relevant files in the master branch? If not, push the current state of the repo to a separate branch and give the link to the branch.

@kylase-learning
Copy link
Contributor

Please read this to find out how to access file in your resources folder https://www.mkyong.com/java/java-read-a-file-from-resources-folder/

@lawncegoh
Copy link
Author

lawncegoh commented Sep 6, 2019

@damithc
Copy link
Collaborator

damithc commented Sep 6, 2019

@j-lum can help?

@j-lum
Copy link

j-lum commented Sep 6, 2019

image
Unable to reproduce error from cloning your repo.

@damithc
Copy link
Collaborator

damithc commented Sep 6, 2019

Works or me too.
image

@danielwys
Copy link

danielwys commented Sep 6, 2019

I cloned your repo and managed to replicate the error:

Screenshot 2019-09-06 at 8 03 46 PM

The issue at hand is quite easily fixed: simply remove the trailing space after the file path for the two statements you posted:

private Image user = new Image(this.getClass().getResourceAsStream("/images/DaUser.png "));
private Image duke = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png "));
to
private Image user = new Image(this.getClass().getResourceAsStream("/images/DaUser.png"));
private Image duke = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png"));

The program executes successfully:
Screenshot 2019-09-06 at 8 05 18 PM

I infer that you're on Mac OS from the directory structure your indicated. It seems that Prof and Jeffry didn't encounter the issue as Mac OS/Unix systems in general are much more anal than Windows when it comes to file naming conventions (being case-sensitive by default, amongst other things).

edit: I just did some quick research and it turns out that Windows automatically removes whitespaces from directories while macOS/Unix does not. So do take note when addressing your file names!

Hope that helps and don't forget to close the issue if it's been resolved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants