Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Java - getCodeBase nullPointerException problems

This topic is 5294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I've got a problem where I have a class Pacman in which the constructor loads an image file using getCodeBase. In my main file, I simply create an object of Pacman and don't do anything to it. Everything compiles fine but whenever I run the applet, it spits out a "nullPointerException" at me every time. I have no idea what's causing this! Files are below which have had all unnecessary information removed: PACMAN.JAVA import java.applet.*; import java.awt.*; public class Pacman extends Applet { /* functions and stuff here */ //the constructor public Pacman() { /* assign other variable values and stuff here */ //initialize upFrames array upFrames = new Image[4]; //tries to load image but instead ends up causing a //nullpointerexception error // //Note that THIS is the statement causing the error upFrames[1] = this.getImage(getCodeBase(),"pacmanRight1.gif"); } } --------------------------------------------------------------- PACMAN_MAIN.JAVA import java.applet.*; import java.awt.*; public class P_cman extends Applet implements Runnable { //tries to create a pacman object using the default //constructor as specified above in pacman.java Pacman pacman1 = new Pacman(); /* other functions and variables and whatnot } So basically, the pacman1 object being created calls the default constructor which calls the getCodeBase statement which ends up causing the nullPointerException. My problem is that I don't know why in the world this is happening. It compiles correctly, and only brings up the error when run. Also, the image file does exist and the path is correct. I tested it with an Image object not within a class in my main file and it worked fine. Once I stuck it in the Pacman class however, the error was caused. I've been debugging for the past 2 hours and I haven't made much progress :[ . Any replies would be greatly appreciated! [edited by - load_bitmap_file on May 20, 2004 10:08:43 PM] [edited by - load_bitmap_file on May 20, 2004 10:10:43 PM]

Share this post

Link to post
Share on other sites
When the browser creates the applet it calls the applets method setStub() that gives the applet a reference to its environment. Your Pacman object doesn't have this reference, it doesn't know where its at. So getCodeBase() doesn't have a URL to return so its null (look at the Applet method setStub() ).

There is no reason to have pacman extend Applet anyway. Since you want to load images from the Applet merely pass the parent Applet to Pacman and use it to call getImage;

public class Pacman {
Applet applet;

public Pacman(Applet a) {
public void loadimages() {

Pacman pacman = new Pacman(this);


Thats one way you could do it...

[edited by - nonnus29 on May 20, 2004 11:48:33 PM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!