• 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

Help with ArrayList error

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
import java.io.*;
public class Methods
{

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
list.add(object);

becomes
if((object = br.readLine()) != null)
{
list.add(object);
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic