• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Head First Java - pg 135 - ArrayList and Compile Errors

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ioan Sirbu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic