Thoughts about applet debugging

I have been having some difficulty in debugging my applets. Basically, none of my applets run from within eclipse, that’s because i use methods that loads images from URL’s. Basically i have to export the classes to a jar, manually add the images via 7zip, then build a container file to run the applet. If all goes well it runs. If i get an exception it doesn’t run at all. Is there any way i can simplify my current debugging technique? To be honest it’s not even debugging, it’s just run and hope it compiles XD

//Kurten

what is the exception that you are getting?

I get a null pointer exception when running from within eclipse.

This is due to a problem with me not knowing where to put images so the code can load it if not jar’ed up together.

This is my image loading method:

Method for getting URL of image

private URL getURL(String filename){
		URL url = null;
		try{
			url = this.getClass().getResource(filename);
		}catch(Exception e){}
		return url;
	}

Then i do something like this to load an image:


image = imageLoader.getImage(getURL("imagename.png"));

When i pack up the classes in a jar with the image in the same directory it loads but if i run it in eclipse it can’t find any image, therefore it results in a null pointer when it calls the getURL method.

My question is, where do i put the image files so eclipse can load em?

Just put your images in a jar and add it to the classpath in eclipse.

you may try JCreator

ok… sorry I get out ::slight_smile:
:slight_smile:

I tried that, dunno if i did it correctly though since it still won’t compile =/

plz, post compilation error, it would be a lot easier to help you

Ok, here is a simple applet i built to test putting applets on a website

Class file:

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

public class DrawImage extends Applet{
	//Image variable
	private Image image;
	private Image image2;
	private Image image3;
	private Image image4;
	private String construction;
	
	private URL getURL(String filename){
		URL url = null;
		try{
			url = this.getClass().getResource(filename);
		}catch(Exception e){}
		return url;
	}
	
	//applet init event
	public void init(){
		image = getImage(getURL("pilenliten.png"));
		image2 = getImage(getURL("Pilen_drogad.png"));
		construction = "Under construction :D";
	}
	
	//applet paint event
	public void paint(Graphics g){
		//create g2d instance
		Graphics2D g2d = (Graphics2D) g;
		
		//Fill bg
		g2d.setColor(Color.BLACK);
		g2d.fillRect(0, 0, getSize().width, getSize().height);
		
		//draw image
		g2d.drawImage(image, 0, 0, this);
		g2d.drawImage(image2, 512, 0, this);
		g2d.setColor(Color.RED);
		g2d.drawString(construction, 300, 240);
	}
}

Exception:

java.lang.NullPointerException
	at sun.awt.image.URLImageSource.<init>(Unknown Source)
	at sun.applet.AppletImageRef.reconstitute(Unknown Source)
	at sun.misc.Ref.get(Unknown Source)
	at sun.applet.AppletViewer.getCachedImage(Unknown Source)
	at sun.applet.AppletViewer.getImage(Unknown Source)
	at java.applet.Applet.getImage(Unknown Source)
	at spriteTest.ImageEntity.load(ImageEntity.java:65)
	at spriteTest.SpriteTest.init(SpriteTest.java:28)
	at sun.applet.AppletPanel.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

I know why it get’s an exception, it’s because it can’t find pilenliten.png and Pilen_drogad.png, it only runs if i export it to a .jar, then open it with 7zip and add pilenliten.png and Pilen_drogad.png to the folder with the class files in the .jar, then start the .jar with a html file.

soory if it look too much schoolar but here is maybe a clarification that will help you :

in java ( and a cupple of other languages ) there are two main kinds of path :

  • compilation : everything used for compilation need to be in this one (and compilation will be fine)
  • runtime : everything needed to run your application Applet/Application need to be in this one (and running your application will be fine)

usually/often you got two main directories in a small project : sources & build,

  • in source folder you ususally got source files (only *.java source file, no need of images in this one)
  • in build you got class file & ressources (ressources include images aswell as config file and such etc…)

for your “bug” what seems wrong is that you dont get everything in your build folder (compilation run fine), so you have to found where the compilation is done and put ressources (Pilen_drogad.png , etc…) in it : images ( and maybe later other necessaries files)

Thank you! :smiley:

I will try this at once, oh and no worry if it looks scholar, i want to learn so i don’t mind :slight_smile:

With NetBeans I used to put my images directly in the src folder with the source code, and they would be automatically bundled into the jar. For running I had a helper class that could automatically wrap the applet within a JFrame.

These combined meant I could run all my jars directly from my editor.

I would have two cases: make your code load from the classpath and fall back to a configured file path. Either wrap the fallback in a try-catch to handle the SecurityException or put it in an if block guarded by a static final boolean which you can toggle to compile for debug or RC.