NullPointer Error - HUH?

When i test run my game i get a applet noninit error. I opened my java console and found the following error:

If i have to make an intelligent guess it would have to do with my LoadImage being called by the Ball class at the applets Init method, but what is a NullPointer? Any ideas?

Here is the code:

GameObject.java


import java.applet.*;
import java.awt.*;
import java.net.*;

public abstract class GameObject extends Applet {
	
	private int x;
    private int y;
    private int vx;
    private int vy;
	private Image image;

	//member methods
	public void setX(int new_x) {x = new_x;}
	public void setY(int new_y) {y = new_y;}
	public void setVX(int new_vx) {vx = new_vx;}
	public void setVY(int new_vy) {vy = new_vy;}

	//ancestor methods
	public int getX() {return x;}
	public int getY() {return y;}
	public int getVX() {return vx;}
	public int getVY() {return vy;}
	public Image getImage() {return image;}
	public int getImageWidth(){return image.getWidth(this);}
	public int getImageHeight(){return image.getHeight(this);}
	
	//boundry checking code
	public boolean hitTopBountry(){if(getY() < 0) {return true;}else{return false;}}
	public boolean hitLeftBountry(){if(getX() < 0){return true;}else{return false;}}
	public boolean hitBottomBountry(){if(getY() > this.getHeight()){return true;}else{return false;}}
	public boolean hitRightBountry(){if(getX() > this.getWidth()){return true;}else{return false;}}
	
	
	public void loadImage(String filename) {
	// load image file
	image = getImage (getCodeBase(), filename); 
	}
	
    public abstract void Draw(Graphics g);

    public abstract void Update();
    
}

Ball.java


import java.awt.*;

class Ball extends GameObject{

public Ball(int initial_x, int initial_y, int initial_vx, int initial_vy, String filename) {
	setX(initial_x);
	setY(initial_y);
	setVX(initial_vx);
	setVY(initial_vy);
	loadImage(filename);
}

public void Update() {
Move();
Check_Bounds();
}

public void Check_Bounds() {
	//ball has collided with the left boundry
	if(getX() <= 0) {
	setVX(-getVX());
	}
	
	//ball has collided with the right boundry
	if(getX() >= this.getWidth()) {
	setVX(-getVX());
	}

	//ball has collided with the top boundry
	if(getY() <= 0) {
	setVY(-getVY());
	}
	
	//ball has collided with the bottom boundry
	if(getY() >= this.getHeight()) {
	setVY(-getVY());
	}	
}

public void Move() {
setX(getX() + getVX());
setY(getY() + getVY());
}

public void Draw(Graphics g) {
	g.drawImage(getImage(), getX(), getY(), this);
}

}

Hyper_Rush_Pong.java


// import necessary packages
import java.applet.*;
import java.awt.*;

// Inherit the applet class from the class Applet
public class Hyper_Rush_Pong extends Applet implements Runnable
{
	// declare two instance variables for double buffering at the head of the program
	private Image dbImage;
	private Graphics dbg;

	//declare our game objects
	Ball ball;

      // Now you should implement the following methods

      // init - method is called the first time you enter the HTML site with the applet
      public void init() {
      	Ball ball = new Ball(250, 200, 1, 1, "Data/Gfx/Ball.png");
      	}

      // start - method is called every time you enter the HTML - site with the applet
      public void start() {
      	// define a new thread
	  Thread th = new Thread (this);
	  // start this thread
	  th.start (); 
      	}

      // stop - method is called if you leave the site with the applet
      public void stop() {
      	}

      // destroy method is called if you leave the page finally (e. g. closing browser)
      public void destroy() {
      	}

	 // run method is called everytime to move the objects onscreen
	 // define a new thread
	
	  public void run() {
	  	       // lower ThreadPriority
      Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

      // run a long while (true) this means in our case "always"
      while (true)
      {
            // repaint the applet
            repaint();

            try
            {
                  // Stop thread for 20 milliseconds
                  Thread.sleep (20);
            }
            catch (InterruptedException ex)
            {
                  // do nothing
            }

            // set ThreadPriority to maximum value
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
      		}

		}
	
	
/** Update - Method, implements double buffering */
public void update (Graphics g) {

      // initialize buffer
      if (dbImage == null)
      {
            dbImage = createImage (this.getSize().width, this.getSize().height);
            dbg = dbImage.getGraphics ();
      }

      // clear screen in background
      dbg.setColor (getBackground ());
      dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

      // draw elements in background
      dbg.setColor (getForeground());
      paint (dbg);

      // draw image on the screen
      g.drawImage (dbImage, 0, 0, this);
		}
	
	
      /** paint - method allows you to paint into your applet. This method is called e.g. if you move your browser window or if you call repaint() */
      public void paint (Graphics g) {
      	ball.Draw(g);
      	} 

}

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4070158

tbh I think someone forgot to code defensively.

What do you mean by “code defensively”?

public abstract class GameObject extends Applet {

The GameObject class should not be a subclass of Applet.

It has to or else i will get errors:

Plus, i don’t think thats the problem.

[quote]It has to or else i will get errors:
[/quote]
No it doesn’t have to. Just think about it for a second: A GameObject and a Ball are not Applets, so you should not misuse the Applet class just because it has some methods you could use. There are other, better ways to design this.

[quote]Plus, i don’t think thats the problem.
[/quote]
It might very well be the problem. What do you think the getCodeBase method should return if you’re not using the Applet class how it’s supposed to be used?

getCodeBase may return the .jar package that contains the class, it should be used only for classpath operations. you better use the CWD “.” instead of that applet meth’. NullPointerException means that your resource isn’t found by the URL resource classloader.

The files excist and the path is correct, maybe because i’m running it locally outside a jar?

And how would i rewrite the applet based code?

What do you mean I quote “locally outside a .jar”?

[quote]And how would i rewrite the applet based code?
[/quote]
Just factor out your dependencies from GameObject to the Applet class to start with.

For example, if you want to get the dimensions of the applet in a GameObject, you could use a reference to the actual applet (the instance of your Hyper_Rush_Pong class).

How do i pass it a reference to my applet?


public Applet getWidth(Applet app){

}

And what about the image code? the image is something thats part of gameobject exclusively, how do i use that?

He meant the other way around:


class GameObject
{
  // A reference to the applet
  private Applet applet;

  // either pass the Applet as construction argument
  public GameObject(Applet applet)
  {
    this.applet=applet;
  }
  
  //or provide a setter and call it after construction of the GameObject
  public void setApplet(Applet applet)
  {
    this.applet=applet;
  }
}

Just load your images or other resources from the classpath (e.g. from inside your jar). Google for it.

for example:


                InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
                BufferedImage image = ImageIO.read(is);

As a matter of fact the getImage should use the resource classLoader to get the URL. If you had a look to the doc about classLoader.getResource you’d understand that the path is always inclusive the classpath, and precisely for common resource like image whether beginning with slashes or not the base would take place at the root of the .jar if your applet is packaged.

Such an issue must be resolved by instancing a full File instance and a FileInputStream, which can be buffered, and to load the image with the root ImageReader, known as ImageIO.read(filename); add the codeBase to the filename and you’ll surely by out of the issue. :smiley: BTW the nio package impl. a RandomAccesFile with channel that can be FileLock’ed to replace the usual File-FileInputStream. That can also avoid wrong file data. :wink:

AFAIK the codeBase is the baseUrl from where the applet is loaded, so you can never instanciate a file with it. I believe he wants to load the image directly from the “webserver” (even if not a webserver using the AppletViewer) with a path relative to the applet’s jar location, so using a FileInputStream is not an option.

I updated GameObject.java and i’m getting new errors:

Updated Code:


import java.applet.*;
import java.awt.*;
import java.net.*;

public abstract class GameObject {
	// A reference to the applet
  	private Applet applet;
  	
	private int x;
    private int y;
    private int vx;
    private int vy;
	private BufferedImage image;

  // Pass the Applet as construction argument
  public GameObject(Applet applet)
  {
    this.applet = applet;
  }

	//member methods
	public void setX(int new_x) {x = new_x;}
	public void setY(int new_y) {y = new_y;}
	public void setVX(int new_vx) {vx = new_vx;}
	public void setVY(int new_vy) {vy = new_vy;}

	//ancestor methods
	public int getX() {return x;}
	public int getY() {return y;}
	public int getVX() {return vx;}
	public int getVY() {return vy;}
	public Image getImage() {return image;}
	public int getImageWidth(){return image.getWidth(this);}
	public int getImageHeight(){return image.getHeight(this);}
	
	//boundry checking code
	public boolean hitTopBountry(){if(getY() < 0) {return true;}else{return false;}}
	public boolean hitLeftBountry(){if(getX() < 0){return true;}else{return false;}}
	public boolean hitBottomBountry(){if(getY() > this.getHeight()){return true;}else{return false;}}
	public boolean hitRightBountry(){if(getX() > this.getWidth()){return true;}else{return false;}}
	
	
	public void loadImage(String filename) {
	// load image file
	InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
    image = ImageIO.read(is); 
	}
	
    public abstract void Draw(Graphics g);

    public abstract void Update();
    
}

Ball.java


import java.awt.*;

class Ball extends GameObject{

public Ball(int initial_x, int initial_y, int initial_vx, int initial_vy, String filename) {
	setX(initial_x);
	setY(initial_y);
	setVX(initial_vx);
	setVY(initial_vy);
	loadImage(filename);
}

public void Update() {
Move();
Check_Bounds();
}

public void Check_Bounds() {
	//ball has collided with the left boundry
	if(getX() <= 0) {
	setVX(-getVX());
	}
	
	//ball has collided with the right boundry
	if(getX() >= this.getWidth()) {
	setVX(-getVX());
	}

	//ball has collided with the top boundry
	if(getY() <= 0) {
	setVY(-getVY());
	}
	
	//ball has collided with the bottom boundry
	if(getY() >= this.getHeight()) {
	setVY(-getVY());
	}	
}

public void Move() {
setX(getX() + getVX());
setY(getY() + getVY());
}

public void Draw(Graphics g) {
	g.drawImage(getImage(), getX(), getY(), this);
}

}

would you try another IDE plz? I can’t answer…

I can’t i’ve paid $50 for this ide and netbeans was slow as heck for me. I can’t figure how to install Eclipse either. :’(

Don’t be offended, but please try to understand what you do and not just copy some code into yours and hope it will magically work. :o Otherwise our answers won’t help you…

The applet reference was to eliminate the need to extend from Applet and since you called this.getWidth(), which was inherited from Applet(!), it surely does not work anymore. You now have to call applet.getWidth() to get the width of the Applet. And since you extend Ball from GameObject, you either need to make applet protected (to allow access from a subclass) or provide a protected Applet getApplet(){ return applet; } you can call to get the Applet’s reference.

Also you if you extend Ball from GameObject and GameObject does only have a constructor expecting an Applet argument, you have to call this constructor from Ball and have to pass the Applet. So your Ball class also needs a constructor getting an Applet:


public Ball(Applet applet, int initial_x, int initial_y, int initial_vx, int initial_vy, String filename) {
	super(applet); // call the superclass' constructor passing the given applet
	setX(initial_x);
	setY(initial_y);
	setVX(initial_vx);
	setVY(initial_vy);
	loadImage(filename);
}

Having said all that, it is probably not the best idea passing Applet references around, since you will depend your game from being an applet forever. Think of what Informations you need, extract them from applet and set them, where you need them.

The last error ist due to drawImage() does not expect a Ball as 4th argument, but an ImageObserver. Applet implements an ImageObserver, so it worked before, but now Ball alone is no ImageObserver. Either implement the ImageObserver interface on Ball or just pass null

Edit: Applet/GameObject typo

what t f’’’ is it doing with “cannot find symbol”? don’t you have access to code completion for 50 bucks? :slight_smile: I don’t wanna be disturbing you with that, but it can be the IDE to reconfigure…