• 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

ArrayList problems

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK so I'm a Newbie but am rather pleased that I've been able to succesfully code all the tutorials in this, rather excellent, suite of tutorials all the way up to ArrayLists.

The specific ArrayList tutorial is here but I'm already getting errors with this code:-


Errors appear on lines 2, 7, 8, 9 and 10.
Error line 2 = arraylist.ArrayList is already defined in this compilation unit.
Errors line 7-10 = cannot find symbol, symbol: method add(String), location: variable listTest of type ArrayList.

Anyone got any clues as to what is happening?
My thanks
R
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roger Wolfenden wrote:Anyone got any clues as to what is happening?


Yup. Duplicate names.

Call your class something different (ArrayListTest?), and all those problems will disappear.

Winston
 
Roger Wolfenden
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well excellent speedy response Winston.

Though if anyone else is following this there was still an error after Winston's useful input and that is in line 3 which should not be declared as public. So the correct code now looks like:-

Thanks again
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roger Wolfenden wrote:
Though if anyone else is following this there was still an error after Winston's useful input and that is in line 3 which should not be declared as public. So the correct code now looks like:-



A more appropriate fix would have been to rename the .java file to ArrayListTest.java.

Typically the class containing your main() will be public. Top-level public classes have to be in a source file that matches the class name.
 
Roger Wolfenden
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you've got me confused now and have to admit that even after accepting Winston's suggestion, changing the class name to ArrayListTest (thanks Jeff) and completing the tutorial I got a compile error - Error: Could not find or load main class arraylist.ArrayList.

As soon as I saw that I knew it was something to do with the first few lines of code so I'm not quite sure what to do now.

My completed tutorial code looks like this now.


I'm using NetBeans and my ArrayList / Source Packages / arraylist/ file is called ArrayList.java should this be ArrayListTest.java?

Confused, I was really pleased with my progress on this suite of tutorials as I'd had all sorts of similar issues with non working tutorials elsewhere and I don't really understand the relationship between package names, class names and the final src .java files.

It's probably a cinch for you experts but a maze for me.
Your help would be appreciated.
Thanks for reading.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roger Wolfenden wrote:- Error: Could not find or load main class arraylist.ArrayList.
...
I'm using NetBeans and my ArrayList / Source Packages / arraylist/ file is called ArrayList.java should this be ArrayListTest.java?



Yes.

Source file should be ArrayListTest.java.

Class name declaration inside that file should be public class ArrayListTest.

In your "Run Configuration" setup (or whatever NB calls that particular option), you may need to explicitly tell it that the class you're running is called ArrayListTest. It seems that it still thinks you're trying to run the ArrayList class. I don't use NB, so I don't know where that particular setting is.

Many people (including me) recommend that beginners don't use and IDE like NB or Eclipse, and instead just use a programmer's editor (I think SlickEdit, UltraEdit, and TexPad+ or something like that) and use the command line for compiling or running.
 
Roger Wolfenden
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well thanks Jeff, appreciate that and I'm off to experiment.

On the use of NetBeans, funnily enough I'm two timing the tutorials that I'm doing Tutorials Here as I've also spent the last few days reading a book, "Head First Java" which recommends the text editor approach as you do yourself which is good as it forces you to understand all the detailed issues like closing curly braces etc. etc. but hasn't adequately covered off all the gramatical stuff about class names, packages etc. etc.

Just to put it in prespective, last year I decided to formally study JavaScript and ended up with a (UK) qualification in JavaScript so all the standard Java stuff like - if (){ //Do something} else {//Do something else} and lots and lots of etceteras to this example seems like a doddle to me. What is not a doddle though is all the naming conventions of classes, packages and methods.

My Head First Java book is approx 700 pages long and the first 100 pages or so deal with all the programming grammar conventions and is a complete nightmare to understand whereas the last 600 pages that deal with all the regular operator stuff look quite straightforward.

Thanks again Jeff, appreciated.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.

And don't feel bad or get discouraged about the naming and classpath stuff. The rules are actually fairly simple and straightforward, but they can be hard to explain clearly, and a little tricky to grasp until that "A-ha!" moment.
 
reply
    Bookmark Topic Watch Topic
  • New Topic