Hey basically im creating a really simple chess game, i have an array which stores all the pieces, a meathod that validates all the moves and a board that prints out black and white.. Now i wanna add my pieces to this board from the array, but i just cant seem to manage it. My code compiles but throws exception errors up the ying yang every time i try to send over the array..
ANy help apreciated....
HEre is the code for Display Pieces...
and then the code in startGame..
Like i say it compiles but throws an exception and i dont see any pieces... help really apreciated!
When you're reporting problems, it helps to show us the actual exception stack trace: there's a lot of useful info there, and we could help you learn to interpret it.
In any case, a lot of the code where the problems might lie isn't shown here, but I'm going to say a few things anyway. In no particular order:
First, it's odd that the "ppieces()" routine seems to be expecting the gameEngine array to contain Strings. For example, this line clearly thinks each element of the array is part of a filename:
Image img = tk.getImage (array[x][y]+".gif");
although this could work if the gameEngine class had an appropriate toString() method.
Second, note that Toolkit.getImage() can return null if the requested image doesn't exist. You really need to check for this and report the error sensibly before passing the img variable to g1.drawImage().
Third, "g1" is not initialized anywhere that I can see; therefore any calls to g1.anything are going to throw a NullPointerException. This really isn't the right way to do graphics in Java, anyway; you're supposed to subclass a JComponent and override the paintComponent() method; see, for example, here.
Fourth, note that allocating an array, be it one, two, or n-dimensional, only allocates references to objects, and not the objects themselves. I don't see where the gameEngine array is every filled with actual gameEngine objects. Therefore using the elements of the array for almost any purpose will, again, lead to a NullPointerException.
Fifth, Java has some fairly firmly entrenched naming conventions that are worth following; perhaps the most important is for all ClassNamesToLookLikeThis, and variableNamesLikeThis -- i.e., using "camel case", with capitalized class names and lower-case initials for variable names. These rules are important because they make it easier for other people to read and understand your code quickly.
Finally: this looks and sounds to me like an example of "big bang integration": writing a bunch of code, compiling it, and then marveling at is as it spectacularly fails to work. Experienced programmers know that the right way to do things is incrementally, in baby steps, testing at every step. Write two lines of code; test to make sure it works. Write two more, test some more. If a test fails, you know that whatever's wrong must be in the last two lines you wrote, not the last two hundred. Make sense?
So my advice to you is to back up, take a deep breath, and start over, making sure at every step of the way that each line of code does what you think it should do.
Thank you for your post... i apreciate the time taken to reply.
The gameengine and the set up such as piece moves have already been done. It is literally the graphics that need to be done. Eg the array has already been set up with the pieces enterd. and i can move them around. Infact everything works perfectly and if i could get away with the graphics just being a simple
Inside 2 for loops be perfect
I agree in your observation that im trying to bite off more than i can chew. But my lecturer seems to think that in 20 hours of learning java i should be able to do all this
and did ya mean that ToolKit cant return a null value??? rather than can return a null value? if so can i just have a transparent gif called null.gif ? [ February 08, 2005: Message edited by: Alan Hollis ]