[java] Class member problem (more details inside)

Started by
6 comments, last by andrewk3652 19 years, 4 months ago
Okay, long story short, I have a class SpriteTable that resides in package aklabs.game . The class has various member functions for storing a pair of hashtables for sprites and their associated animations. The class itself compiles just fine, and the class imports from the package into my program just fine - however, when I try to do anything beyond construct it, I get errors about it not finding the method in the class. To wit:

/********** SPRITETABLE CLASS **************/
package aklabs.game;

import java.awt.*;
import java.util.*;

/** A class for managing and linking sprite and animation frame data */
public class SpriteTable
{
        protected Hashtable Images;
        protected Hashtable Sprites;

        /** Constructor */
        public SpriteTable()
        {
                Images = new Hashtable();
                Sprites = new Hashtable();
        }

        /* Snipped a whole bunch of other functions here, they
all produce similar errors during compilation */

        /** Adds a frame of sprite animation to the table
        @param img An Image object to add
        @param Id A String value to use as a key to this frame
        @return boolean Returns false if either img or Id are null, or
                if the key or value passed already exist (independently
                or as a pair) in the table */
        public boolean addFrame(Image img, String Id) throws NullPointerException
        {
                if ( img == null || Id == null )
                        throw new NullPointerException("Null pointer in input SpriteTable.addFrame");

                if ( containsPair(Images, img, Id) )
                        return false;

                Images.put(Id, img);
                return true;
        }
}

/**** OFFENDING CODE (in a separate .java file) *****/

Image theImg = getImage(getCodeBase(), "source/ball.png");
SpriteTable Sprites = new SpriteTable();
try {
        Sprites.addFrame(theImg, "ball");            
}
catch (NullPointerException e) {
}





The above code produces the following result:

bash-2.05b$ javac opong.java 
opong.java:18: cannot find symbol
symbol  : method addFrame(java.awt.Image,java.lang.String)
location: class SpriteTable
                        Sprites.addFrame(theImg, "ball");
The class is in my classpath (and I've tried compiling with it in the same directory, too). The results are always the same. I don't get it!
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================
Advertisement
1: Don't ignore the naming conventions. You have given your object a class name. Without a complete listing of your classpath or a reflection there's no way for us to have any confidence that you don't merely have *another* class called "Sprites" hanging around.
try to compile with

$ javac *.java

It should resolve all the dependencies.

WS
Firstly, there's only one .java file being compiled - doing *java would do nothing for me.

Secondly, I haven't ignored the naming conventions. My class is named SpriteTable, and it is included in a package named aklabs.game; it is a unique name in this package. The instance of the object in question is named Sprites, and it is at the class-level scope; there are no other variables named Sprites. There's no class named Sprites anywhere in my classpath, yes I've checked.

Neither of these are the problem.

But for those who insist on seeing more, here is my classpath:

CLASSPATH=.:/usr/home/andrew/source/BY-LANGUAGE/JAVA/classes/

and here is the complete source of the two files in question (SpriteTable.java, which has already been cleanly compiled and put into the proper package location within my classpath; and opong.java, the file in question). I had been trying to avoid posting them as they're a bit lengthy to browse thru, but here goes:

/******************* SPRITETABLE.JAVA ***************/package aklabs.game;import java.awt.*;import java.util.*;/** A class for managing and linking sprite and animation frame data */public class SpriteTable{	protected Hashtable Images;	protected Hashtable Sprites;	/** Constructor */	public SpriteTable()	{		Images = new Hashtable();		Sprites = new Hashtable();	}	// checks if the key/value pair already exist in the table	private boolean containsPair(Hashtable tbl, Object obj, String key)	{		if ( tbl.contains(obj) )			return true;		if ( tbl.containsKey(key) )			return true;		return false;	}	/** Adds a frame of sprite animation to the table	@param img An Image object to add	@param Id A String value to use as a key to this frame	@return boolean Returns false if either img or Id are null, or		if the key or value passed already exist (independently		or as a pair) in the table */	public boolean addFrame(Image img, String Id) throws NullPointerException	{		if ( img == null || Id == null ) 			throw new NullPointerException("Null pointer in input data in SpriteTable.addFrame()");		if ( containsPair(Images, img, Id) )			return false;		Images.put(Id, img);		return true;	}	/** Adds a sprite to the table	@param spr A sprite object to add	@param Id A String value to use as a key for this sprite 	@return boolean Returns false if either spr or Id are null, or		if the key or value passed already exist (independently		or as a pair) in the table */	public boolean addSprite(Sprite spr, String Id) throws NullPointerException	{		if ( spr == null || Id == null )			throw new NullPointerException("Null pointer in input data in SpriteTable.addSprite()");		if ( containsPair(Sprites, spr, Id) )			return false;		Sprites.put(Id, spr);		return true;	}	/** Returns the frame associated with the String Id */	public Image getFrame(String Id) throws NullPointerException	{		if ( Id == null ) 			throw new NullPointerException("Null pointer in input data in SpriteTable.getFrame()");		if ( Images.containsKey(Id) )			return (Image)Images.get(Id);		else return null;			}	/** Returns the Sprite associated with the String Id */	public Sprite getSprite(String Id) throws NullPointerException	{		if ( Id == null )			throw new NullPointerException("Null pointer in input data in SpriteTable.getSprite()");		return (Sprite)Sprites.get(Id);	}}/*************** OPONG.JAVA *****************/import aklabs.game.*;import java.util.*;import java.awt.*;import java.awt.event.*;import java.awt.geom.*;import java.applet.*;public class opong extends Applet implements KeyListener{	SpriteTable Sprites;	Actor2D[] Actors;			public void init()	{		/* This is the only time in the file I use an Image object			for temporary storage - I was just doing it to			see if that would fix the problem. The rest of			the statements in this file with addFrame()			just pass the image directly back from getImage().*/		Image theImg = getImage(getCodeBase(), "source&#47;ball.png");		Sprites = new SpriteTable();		try {			Sprites.addFrame(theImg, "ball");		}		catch (NullPointerException e) {		}		/* This is code from the first coding of this file,			where I was passing the results of getImage			directly to the hashtables */		Sprites.addFrame(getImage(getCodeBase(), "source&#<span class="java-number">47</span>;ball.png"</span>),<br>				<span class="java-literal">"ball"</span>);<br>		Sprites.addSprite(<span class="java-keyword">new</span> Sprite(<span class="java-literal">"ball"</span>, <span class="java-number">0</span>));<br>		Sprites.addFrame(getImage(getCodeBase(), <span class="java-literal">"source&#<span class="java-number">47</span>;table.png"</span>),<br>				<span class="java-literal">"table"</span>);<br>		Sprites.addSprite(<span class="java-keyword">new</span> Sprite(<span class="java-literal">"table"</span>, <span class="java-number">0</span>));<br>		Sprites.addFrame(getImage(getCodeBase(), <span class="java-literal">"source&#<span class="java-number">47</span>;paddle.png"</span>),<br>				<span class="java-literal">"paddle"</span>);<br>		Sprites.addSprite(<span class="java-keyword">new</span> Sprite(<span class="java-literal">"paddle"</span>, <span class="java-number">0</span>));<br><br>		Actors = <span class="java-keyword">new</span> Actor2D[<span class="java-number">3</span>];<br>		Actors[<span class="java-number">0</span>] = <span class="java-keyword">new</span> Actor2D(<br>				<span class="java-keyword">new</span> String[] {<span class="java-literal">"paddle"</span>, <span class="java-literal">"paddle"</span>},<br>				<span class="java-keyword">new</span> <span class="java-primitives">long</span>[] {Actor2D.ACTOR_ST_IDLE, <br>					Actor2D.ACTOR_ST_MOVING});<br>		Actors[<span class="java-number">1</span>] = Actors[<span class="java-number">0</span>];<br>		Actors[<span class="java-number">2</span>] = <span class="java-keyword">new</span> Actor2D(<br>				<span class="java-keyword">new</span> String[] {<span class="java-literal">"ball"</span>, <span class="java-literal">"ball"</span>},<br>				<span class="java-keyword">new</span> <span class="java-primitives">long</span>[] {Actor2D.ACTOR_ST_IDLE, <br>					Actor2D.ACTOR_ST_MOVING});<br>	}<br><br>	<span class="java-visibilitymodifier">public</span> <span class="java-primitives">void</span> keyReleased(KeyEvent e)<br>	{<br>	}<br><br>	<span class="java-visibilitymodifier">public</span> <span class="java-primitives">void</span> keyPressed(KeyEvent e)<br>	{<br>	}<br><br>	<span class="java-visibilitymodifier">public</span> <span class="java-primitives">void</span> keyTyped(KeyEvent e)<br>	{<br>	}<br>}<br><br></pre></div><!--ENDSCRIPT--> 
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================
Don't be so quick to ignore suggestions, you *are* in fact ignoring standard java naming conventions. The standard naming convention is that class-names start with an uppercase letter, while object-names (veriables) start with a lowercase letter.
When you ignore naming conventions such as these, and have variables named Sprites and Images, it makes the code much harder to read for other people.

That having been said, it's unlikely that's the root cause of your problems.

I tried compiling you code, and did not encounter the problem you described, although the compiler didn't like your calls to addSprite in SpriteTable.
Here's the addSprite method signature:
public boolean addSprite ( Sprite spr, String Id )throws NullPointerException;

And here's how you call it:
Sprites.addSprite ( new Sprite ( "ball", 0 ) );

which actually tries to call a (non-existent) method:
public bool addSprite ( Sprite spr );


My suggestion:
1. Fix the variable naming you've got going.
2. Get yourself a decent IDE (personally I like JDev).
3. Put the opong class in the aklabs.game package, I don't really see any good reason why it shouldn't be in a package.
4. Try compiling with the IDE.

If this doesn't work, post your full code (*full* code, all classes) either directly, or put it in a zip that we can download somewhere, so that we can properly try to compile it ourselves, and see what happens.

- Neophyte

Okay, here's what I did. And it finally worked.

I took it out of the aklabs.game package, and recompiled, then put it into the opong directory. It compiled and ran just fine. So the problem must've been in my classpath somewhere; I went to looking, and it was pointing in the right location. The only thing I could think of was that since my classpath was

/usr/home/andrew/source/BY-LANGUAGE/JAVA/classes/

maybe it was not recognising the trailing / on the classpath, and was appending its own / to the classpath when retrieving files - i.e., instead of trying ...../JAVA/classes/aklabs/game/SpriteTable.class, it was looking for ...../JAVA/classes//aklabs/game/SpriteTable.class. I took off the leading slash, and this was indeed the problem. Code compiled just fine.

*does the three stooges multiple face smack* nnnnyaaaaahhh!!!!

Thanks for the help. (And honestly, I didn't know object names are supposed to start with lowercase letters. I'll pay attention to that in the future. I just thought he meant that I was duplicating object/class names. )
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================
Why don't you get an IDE to work with?
With IDE you won't get that classpath troubles (usually) :)
I once use a notepad and it really pain to the heart :)

Quote:(and I've tried compiling with it in the same directory, too).


Be sure to add "." on your classpath, I once get this problem too.

And yes object should begin with lowercase, and class should begin with Uppercase.
Then the opong.java should be Opong.java
And SpriteTable Sprites; should be SpriteTable sprites;
-------------Golden T Game Engine - Java Game EngineGolden T Website | Golden T Forum
Yeah I actually use visual Slickedit 99% of the time; I was just doing this on the command line with nano and manual compilation because I was just doing a small proof of concept, and didn't think I'd have any problems.

'Swhat I get for thinking. :-)
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================

This topic is closed to new replies.

Advertisement