0

I've been applying the solutions from other similar questions.

I was getting a image from res folder using this line:

shell.setImage(new Image(display, ClassLoader.getSystemClassLoader().getResourceAsStream("icon_128.png")));

The file is inside "res" folder in the project.

It worked perfectly until I uploaded my project to a Git repo in Bitbucket. After cloning the project and importing it, now my project crash because getResourceAsStream("icon_128.png") returns null.

It's frustrating because it works perfectly in the other project which is not versioned into G|it, but crashes only in my cloned new directory project with Git.

In both versions of the project the file is inside the "res" folder.

What could be happening with this?

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • Are you sure that you committed everything to GIT and file actually persists in cloned repo? – Ivan Oct 12 '18 at 15:57
  • also check that the `res` folder is not (only) under the source folder - it must be in the build folder, there were also the class files are searched for – user85421 Oct 12 '18 at 16:01
  • omg the problem is that i need this line on the classpath "" and wtf how can I solve this situation? ,classpath eclipse project file is supposed to be ignored on git, so how to deal with this? – NullPointerException Oct 12 '18 at 16:01
  • @Ivan I'm using regular eclipse launching, so must be ANT, can you give me a sample build.xml file with that in the classpath in an answer? – NullPointerException Oct 12 '18 at 16:05
  • Check this question https://stackoverflow.com/questions/1216744/adding-non-code-resources-to-jar-file-using-ant – Ivan Oct 12 '18 at 16:09
  • @Ivan they are not using build.xml file with .classpath – NullPointerException Oct 12 '18 at 16:10
  • That answer has the example of step that you need to include your resources into jar. if you do not have build.xml I cannot write one from scratch for you – Ivan Oct 12 '18 at 16:12

1 Answers1

0

git has nothing to do with it. You didn't give enough detail to be sure about what's going on, but 2 obvious problems come to mind:

[1] getResourceAsStream looks for the named file in the same place that java looks for class files: The classpath. You're running this code either from an editor, or with java on the command line (in which case you're running a jar file and a build tool added a Class-Path entry to that jar, if you use the -jar switch, or you're not, in which case you're specifying the classpath on the command line), or with a build tool (in which case it will be supplying the classpath): icon_128.png needs to be in the root of one of the entries on the classpath, and now it isn't. The fix is to, well, fix that. Maven, for example, copies all resources find in /src/main/resources into any jars it makes. Your icon_128.png should be there.

[2] This isn't the right way to do it. The right way is ClassThisCodeIsIn.class.getResourceAsStream("/icon_128.png") (note: The starting slash; it is important). Your version has various somewhat exotic fail cases which this version skips. This version will look specifically in the classpath which produced your class file, and cannot NPE; your version will fail or throw NullPointerExceptions in various cases.

NB: When you cloned and re-built, the 'build' directories were effectively wiped because you don't check those into source control. That's why it worked before and doesn't now. git isn't to blame; you, or your IDE, copied icon_128.png to the build dir, and that step needs to be repeated every time you clone your git repo. A build tool automates this step and ensures that you can just do a fresh checkout from source control, and then invoke the build tool and all will be well after it finishes.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • the problem is that i need this line on the classpath "" and wtf how can I solve this situation? ,classpath eclipse project file is supposed to be ignored on git, so how to deal with this? – NullPointerException Oct 12 '18 at 16:02
  • Eclipse copies files in src dirs that aren't java files right over, so eclipse fixes things itself. The 'fix' is to use build tools. The popular choices are Maven or Gradle. – rzwitserloot Oct 12 '18 at 16:07
  • If you want your `.classpath` file in git, no one is going to stop you. Are they? – nitind Oct 12 '18 at 17:11