Java - Why that URL == null -
welcome, have problem code. url shoud hold path resources not initialized can me solving problem? here code of loading class:
import java.awt.image.bufferedimage; import java.io.ioexception; import java.net.url; import javax.imageio.imageio; public class bufferedimageloader { public bufferedimage loadimage(string pathrelativetothis) throws ioexception { url url = this.getclass().getresource(pathrelativetothis); bufferedimage img = imageio.read(url); return img; } }
and errors getting, like:
exception in thread "awt-eventqueue-1" java.lang.illegalargumentexception: input == null! @ javax.imageio.imageio.read(unknown source) @ rpg.bufferedimageloader.loadimage(bufferedimageloader.java:12) @ rpg.character.draw(character.java:40) @ rpg.main.paint(main.java:210) @ rpg.main.update(main.java:190) @ sun.awt.repaintarea.updatecomponent(unknown source) @ sun.awt.repaintarea.paint(unknown source) @ sun.awt.windows.wcomponentpeer.handleevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$200(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
te method class.getresource(string) loads named resource based on class loader of class instance.
what means, unless custom classloaded loaded class instance, resource trying load should relative classpath.
for example, if had following structure on file system:
c:\ + temp + some-directory + some-other-directory + some-file.txt
and added c:\temp\some-directory
classpath, url some-file.txt
using class.getresource(string) this:
url somefileurl = someclass.getresource("/some-other-directory/some-file.txt");
Comments
Post a Comment