To start I searched the message board for anything pertaining to ArrayList and HeadFirst Java. I've also looked at the hfjava/errata stuff with no avail...
To make a long story short... My code will not run. The book says in order to use the ArrayList library all you need to do is this...
The program from chap 5 & chap 6 is a proto-basic Battleship game called SinkTheDotCom and it is composed of three files. I have posted the before of the SimpleDotCom.java and the primary change takes place on pg 135 in the SimpleDotCom.java as well as all three files.
However once I have modified the file I get the error:
Now I have barely grasped the concept of using arrays and am trying to feel my way through what the ArrayList is.
As far as I can tell my error is cause by me wanting to cast incompatible types? Two different objects?
And that ArrayList only will hold objects and never primatives... So handing over the list of locations (which are int's) to the ArrayList won't work because they are primatives.
Am I trying to hand off the referrence or the primative when I am doing this? Either way I'm surprised that no one has caught this before...
To top that off I don't even know what syntax I should use, I tried (foolishly) to do a theDotCom.add.setlocationsCells(locations) as well as loc? & locs? (see page 134-135).
I have to use the ArrayList.add() somehow and pass the randomly generated variables... somehow.
I *think* I understand how to use ArrayList and how it works but probably not...
Any help or clarification as to how to better understand ArrayList and use/fix the program would be wonderful.
Thank you for your time.
Sincerely, Ioan C. Sirbu [ January 15, 2005: Message edited by: Ioan Sirbu ]
I just took a quick look at this -- didn't test any code or anything -- but...
It looks like the setLocationCells method in SimpleDotCom is defined to accept an ArrayList as an argument. You're trying to pass locations to this method, and locations is an int -- not an ArrayList. (Either way, you're passing an object reference, since an Array is an object, whether it contains primitive values or object references.)
My instinct -- again, without looking too closely at the code structure -- would be to make a new ArrayList out of locations. It looks like the ArrayList should contain String objects, because you're later looking for the index of a particular String. So you'll probably need to loop through the int to make String objects out of each int element using Integer.toString(int), then pass that ArrayList of String objects.
[ January 15, 2005: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Jan 07, 2005
Sorry I didn't respond sooner, read the code I understand what your saying now to digest it.
I find it troubling that I can read and I can follow what code is supposed to do but creating the structures and pseudo-code in my mind, paper or in the ide/editor is extermely difficult.
Originally posted by Ioan Sirbu: ...I find it troubling that I can read and I can follow what code is supposed to do but creating the structures and pseudo-code in my mind, paper or in the ide/editor is extermely difficult...
I think that's normal until you get comfortable with it.
Joined: Jan 07, 2005
I have a new roadblock/problem.
I have gone and done a really detailed job of commenting and setting up the output message for anyone to compile and pinpoint quick..
It is probably a 10 second fix...
Within the JBuilder ide it doesn't state any warnings when I compile HOWEVER in cmd window mode...
Secondly(?) the code executes until it gets to "enter a number", any number entered throws this at me.
I've tried to use both of the new codes as well as checking the variable names for the strings checked to see what the matter was to no avail.
It seems that... well here is the full three files...
Thank you in advance..
[ January 16, 2005: Message edited by: Ioan Sirbu ]
NEW WAY (allows type checking)... ArrayList<String> locationsList = new ArrayList<String>();
The file is compiling fine, but javac is simply warning you that your Container is unchecked for type. (Even though SimpleDotCom also has an ArrayList, the message does not appear when compiling that file because we're not performing any "unsafe" operations of actually adding elements to the ArrayList.)
Second, the runtime Exception...
Use the line numbers provided by javac for debugging. The message...
NullPointerException at SimpleDotCom.checkYourself(SimpleDotCom.java:12) at SimpleDotComGame.main(SimpleDotComGame.java:36)
...tells us that there's a null reference in the SimpleDotCom.checkYourself method -- specifically at the 12th line of SimpleDotCom.java. This line is:
int index = locationCells.indexOf(userInput);
The error message also tells us that the above method is called on the 36th line of SimpleDotComGame.java, which is:
String result = theDotCom.checkYourself(userInput);
So we know where to look. We have a null reference with locationCells in SimpleDotCom. It turns out that locationCells is valued with the method public void setLocationCells(ArrayList loc), which is never called. So in SimpleDotComGame, after we've made our ArrayList, we just need to pass a reference to our SimpleDotCom object.
[ January 18, 2005: Message edited by: marc weber ]
Joined: Jan 07, 2005
Awesome. How in the heck?!?!
In the meantime..
I did create a second method for userinput as I thought there was a problem there. Just to return something.. Sure enough that did work...
Then I got to thinking about I was mishandling string value from the userInput and that I needed to convert into an Int for index and that was causing the exception...
I might have never figured out the passing the reference.
This way, you can see that it has, in fact, been initialized the way you expect it to be. In troubleshooting this particular problem, we could have used the following println messages to show us what we really had in that troublesome line identified by the compiler...
[ January 16, 2005: Message edited by: marc weber ]