View Full Version : java - getCodeBase()

03-01-2005, 01:13 AM
Im trying to load an Image for my applet:

getImage(getCodeBase(), "ship.gif")
It compiles fine, but when I try to run it getCodeBase() returns a nullPointerException. This is really confusing as I cant find another way to do this...

03-01-2005, 01:43 AM
have you tried to take out the getCodeBase(), ?

abstract Image getImage(String filename)
Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG.


03-01-2005, 02:22 PM
Try getDocumentBase(). That's what I've always used in that situation for Applets.

03-01-2005, 04:47 PM
this error always confused me, i could get it working in my first applet, then i tried remaking it from the basics and it threw npe, then i copied the original, deleted everything appart from what was in my 2nd file, and it worked even though the .java was exactly the same?!!? :confused:

anyway, i think this is thrown a lot when u are using appletviewer or file://... instead of loading it from a domain (or ip if ur gunna complain at me for saying domain) eg
getCodeBase().getHost() will return "domain"

[getCodeBase() will return a URL datatype]

03-02-2005, 03:41 PM
It stopped throwing the exception when I loaded the Image in the init() method, though it still didn't work... So I had to put it the on my local web server and use a URL object to get it working.

URL iurl = new URL("http://localhost/bilder/invader.jpg");
invaders[i] = new InvaderSprite(getImage(iurl),width, height, 3, 0,this);