This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Help with ArrayList error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with ArrayList error" Watch "Help with ArrayList error" New topic

Help with ArrayList error

Tempora Telora
Ranch Hand

Joined: Jun 20, 2005
Posts: 83
Hey guys here is my code. It is having a problem with returning the arraylist list. Please help me figure out my problem

import java.util.ArrayList;
public class Methods

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
there's nothing wrong in the return of the ArrayList
stripping all the readFile code leaves this, which returns OK

your problem must be in the file reading.
if the structure of the file is to have the first line indicate how many lines there are:
//read first line use for loop
int i = Integer.parseInt(br.readLine());

perhaps if you wrap the adding to the arraylist into a conditional
object = br.readLine();

if((object = br.readLine()) != null)
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Actually, stripping all the file reading code leaves this (formatting cleaned up):

The problem is that "list" is declared inside the try block, so that it's only visible inside that try block; but the code tries to use it at the last line, which is outside the try, so "list" is undefined.

Just move the line that declares the ArrayList:

Now "list" is visible to all the code in the whole function.

[Jess in Action][AskingGoodQuestions]
jQuery in Action, 2nd edition
subject: Help with ArrayList error