I have a class ImageManager, and if I write the line public ImageIcon test = new ImageIcon(getClass().getResource("/Images/board.png")); in said class, it does what I would expect and creates a valid ImageIcon with the picture at that file path. However, if I write the line public ImageIcon test = new ImageIcon(new ImageManager().getClass().getResource("/Images/board.png")); I get a blank ImageIcon instead. I have no idea why the first line would work but the second wouldn't, considering getClass() should be getting the same class for both lines. If anyone could explain what is going on here, I would greatly appreciate it.
#Question about getClass().getResource()
35 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @hallow bolt! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Indeed it shouldn't be different, so what you're actually doing is probably different to what you claim you're doing
as far as I know it's not
i literally just replaced the first line with the second
nothing else in the program changes, and the image suddenly doesn't show up
is that still the case if you use new ImageIcon(ImageManager.class.getResource("/Images/board.png"));
this one properly shows an image
i have the three lines commented out here so i can easily switch between them, only the middle one doesn't work
Hmm Im not too sure why yours didnt work but normally you would use ClassName.class in static context and getClass() in instance context.
All three of these should return a file. Does the second one print null
When using these three statements in a print
1st and 3rd print the absolute file path of the image
2nd actually seems to be throwing an exception now??
even without printing anything, when i just uncomment the middle line it prints this to my console. it seems to throw so many exceptions that i can't even see the full message which is presumably at the top
Whats the error ?
i have no idea, that's my entire terminal
like it repeats the same at Images.ImageManager.<init>(ImageManager.java:8) line so many times that it fills up the terminal and i cant see what's at the top
is there any other way to see the error?
the ImageIcon isn't being declared in a loop or anything so i have no idea why it's printing this line so many times
🤣
I know what it is
You are executing the second statement in your ImageManager class. Each class creates a new instance ofImageManager which then also creates a new instance to get the class. Basically inifinte recursion -> StackOverflow error
OH
god damn recursion really pops up everywhere huh
tysm this would have driven me insane
Yeah so generally dont create a new instance just because you want to get the class. Use ClassName.class instead.
i swear my terminal was empty earlier but perhaps i'm just blind
is there any reason why you wouldn't do this even in an instance context?
cause it seems like the generally best way to get the class
ClassName.class and getClass() return the same. So why waste resources on creating a new object when you are not going to use it ?
you said up here to use getClass() in an instance context tho. is that just convention or is there an actual reason to do that?
If you want the class of the current object (in the same context as this) you use getClass()
You can use both to differentiate between getting thr class statically or not.