Sign in to follow this  

[java] Class member problem (more details inside)

This topic is 4766 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

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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/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/ball.png"),
"ball");
Sprites.addSprite(new Sprite("ball", 0));
Sprites.addFrame(getImage(getCodeBase(), "source/table.png"),
"table");
Sprites.addSprite(new Sprite("table", 0));
Sprites.addFrame(getImage(getCodeBase(), "source/paddle.png"),
"paddle");
Sprites.addSprite(new Sprite("paddle", 0));

Actors = new Actor2D[3];
Actors[0] = new Actor2D(
new String[] {"paddle", "paddle"},
new long[] {Actor2D.ACTOR_ST_IDLE,
Actor2D.ACTOR_ST_MOVING});
Actors[1] = Actors[0];
Actors[2] = new Actor2D(
new String[] {"ball", "ball"},
new long[] {Actor2D.ACTOR_ST_IDLE,
Actor2D.ACTOR_ST_MOVING});
}

public void keyReleased(KeyEvent e)
{
}

public void keyPressed(KeyEvent e)
{
}

public void keyTyped(KeyEvent e)
{
}
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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. )

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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. :-)

Share this post


Link to post
Share on other sites

This topic is 4766 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this