Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts

    java - getCodeBase()

    Im trying to load an Image for my applet:
    Code:
    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...
    <JPM />

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Jason

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kentucky
    Posts
    133
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try getDocumentBase(). That's what I've always used in that situation for Applets.

  • #4
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    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?!!?

    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
    http://www.domain.com/blah/applet.htm
    getCodeBase().getHost() will return "domain"

    [getCodeBase() will return a URL datatype]

  • #5
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •