aspose file tools*
The moose likes Beginning Java and the fly likes Head First Java - pg 135 - ArrayList and Compile Errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java - pg 135 - ArrayList and Compile Errors " Watch "Head First Java - pg 135 - ArrayList and Compile Errors " New topic
Author

Head First Java - pg 135 - ArrayList and Compile Errors

Ioan Sirbu
Greenhorn

Joined: Jan 07, 2005
Posts: 8
Hello Everyone,

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.

Before


After



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 ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
Ioan Sirbu
Greenhorn

Joined: Jan 07, 2005
Posts: 8
Marc,

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.

Thank you very much for the reply!

Sincerely,
Ioan
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
Ioan Sirbu
Greenhorn

Joined: Jan 07, 2005
Posts: 8
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..

Sincerely,
Ioan

___________

GameHelper.java


SimpleDotCom.java


SimpleDotComGame.java

[ January 16, 2005: Message edited by: Ioan Sirbu ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

First, the compile-time warning...

One of the new features of Java 1.5 is "generics." See the topic, "Generic Types," on this "J2SE in a Nutshell" page from Sun...
http://java.sun.com/developer/technicalArticles/releases/j2se15/

Basically, generic types for Collections allow you to specify the type of object a Container should hold.

OLD WAY (Container for any Object)...
ArrayList locationsList = new ArrayList();

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 ]
Ioan Sirbu
Greenhorn

Joined: Jan 07, 2005
Posts: 8
Marc,

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.

Thank you!

Sincerely,
Ioan
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Here's a troubleshooting tip: Use println messages that display values at given points. That way, you can verify that you have what you think you have.

For example, in SimpleDotComGame, rather than using...

System.out.println("Arraylist has been initialized.");

...use instead...

System.out.println("locationsList: " + locationsList);

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java - pg 135 - ArrayList and Compile Errors