Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Thoughts about applet debugging  (Read 1620 times)
0 Members and 1 Guest are viewing this topic.
Offline Kurten

Junior Duke


Medals: 3
Exp: 3 years


-w-


« Posted 2011-02-11 18:17:10 »

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
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 78
Projects: 15


★★★★★


« Reply #1 - Posted 2011-02-11 19:07:59 »

what is the exception that you are getting?
Offline Kurten

Junior Duke


Medals: 3
Exp: 3 years


-w-


« Reply #2 - Posted 2011-02-11 20:04:01 »

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
1  
2  
3  
4  
5  
6  
7  
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:
1  
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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 78
Projects: 15


★★★★★


« Reply #3 - Posted 2011-02-11 20:06:28 »

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.
Offline DzzD
« Reply #4 - Posted 2011-02-11 20:07:50 »

[size=6pt]you may try JCreator [/size]

ok... sorry I get out Roll Eyes
Smiley

Offline Kurten

Junior Duke


Medals: 3
Exp: 3 years


-w-


« Reply #5 - Posted 2011-02-11 20:35:12 »

I tried that, dunno if i did it correctly though since it still won't compile =/
Offline DzzD
« Reply #6 - Posted 2011-02-11 20:42:07 »

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

Offline Kurten

Junior Duke


Medals: 3
Exp: 3 years


-w-


« Reply #7 - Posted 2011-02-11 20:48:22 »

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

Class file:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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.
Offline DzzD
« Reply #8 - Posted 2011-02-11 21:02:26 »

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)

Offline Kurten

Junior Duke


Medals: 3
Exp: 3 years


-w-


« Reply #9 - Posted 2011-02-11 21:11:19 »

Thank you! Cheesy

I will try this at once, oh and no worry if it looks scholar, i want to learn so i don't mind ^_^
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JL235

JGO Coder


Medals: 10



« Reply #10 - Posted 2011-02-12 20:33:17 »

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.

Online pjt33
« Reply #11 - Posted 2011-02-12 20:54:01 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (35 views)
2014-10-17 03:59:02

Norakomi (26 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (26 views)
2014-10-15 16:18:58

TehJavaDev (50 views)
2014-10-14 00:39:48

TehJavaDev (50 views)
2014-10-14 00:35:47

TehJavaDev (40 views)
2014-10-14 00:32:37

BurntPizza (63 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (75 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!