#Question about getClass().getResource()

35 messages · Page 1 of 1 (latest)

hallow bolt
#

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.

harsh ginkgoBOT
#

This post has been reserved for your question.

Hey @hallow bolt! Please use /close or the Close Post button 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.

deft violet
#

Indeed it shouldn't be different, so what you're actually doing is probably different to what you claim you're doing

hallow bolt
#

i literally just replaced the first line with the second

#

nothing else in the program changes, and the image suddenly doesn't show up

candid idol
hallow bolt
#

i have the three lines commented out here so i can easily switch between them, only the middle one doesn't work

candid idol
#

Hmm Im not too sure why yours didnt work but normally you would use ClassName.class in static context and getClass() in instance context.

candid idol
#

When using these three statements in a print

hallow bolt
#

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

candid idol
#

Whats the error ?

hallow bolt
#

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

candid idol
#

🤣

#

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

hallow bolt
#

OH

#

god damn recursion really pops up everywhere huh

#

tysm this would have driven me insane

candid idol
#

Yeah so generally dont create a new instance just because you want to get the class. Use ClassName.class instead.

hallow bolt
#

i swear my terminal was empty earlier but perhaps i'm just blind

hallow bolt
#

cause it seems like the generally best way to get the class

candid idol
#

ClassName.class and getClass() return the same. So why waste resources on creating a new object when you are not going to use it ?

hallow bolt
candid idol
#

If you want the class of the current object (in the same context as this) you use getClass()

candid idol
hallow bolt
#

oh ok

#

ty