Results 1 to 3 of 3
  1. #1
    meustace is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default ArrayList going out of scope

    Hi,
    I am having problems with an ArrayList that goes out of scope for some reason. It is declared at the highest level, populated later on, used in one child for next loop, but then goes out of scope and is unusable elsewhere. The ArrayList in question is assList. Any clues? Abbreviated code is here:

    Java Code:
    try
    {
       ...
       ArrayList assList = null;
       ArrayList assEmailList = null;
       String[] assArray = null;
       ...
       try
       {
          if (actionField.equalsIgnoreCase("CREATE"))
          {
             if( req.containsKey( "Assignee" ) )
             {
                assCol = ((String[])req.get("Assignee"))[0];
                assArray = assCol.split(";");
                assList = new ArrayList(Arrays.asList(assArray)); [B]//Assign values here[/B]
             }
             if (null == assEmailCol || assEmailCol.equalsIgnoreCase(""))
             {
                try 
                {
                   conn= DALTool.getConnection(dmDal);
     
                   String tempAssEmail = "";
                   boolean chkOwnerAdded = false;
                   assEmailList = new ArrayList();
                   for(int i=0; i <= assList.size(); i++)
                   {
                      stmt = null;
                      stmt = conn.createStatement();
                      //Start of PEOPLE db call 
                      _sql = "some sql select '" + assList.get(i).toString() + "'";
                      System.out.println(assList.get(i).toString());[B] //Here I get the values output[/B]
                      rs = stmt.executeQuery(_sql);
                      while (rs.next())
                      {
                         tempAssEmail = rs.getString(1);
                      }   
                      if (null != tempAssEmail && !tempAssEmail.equalsIgnoreCase(""))
                      {
                         assEmailList.add(tempAssEmail);
                      }
                      else
                      {
                         if (chkOwnerAdded==false)
                         {
                            assEmailList.add(userName.substring(0, userName.indexOf('@')) + emailAddress);
                            assList.set(i, userName.substring(0, userName.indexOf('@')));
                            chkOwnerAdded=true; //do this only once
                         }
                         else
                         {
                            assList.remove(i); //I have checked and this is not being called
                         }
                      }
                      System.out.println(assList.get(0).toString()); [B]//Here I get the value output to my log file[/B] 
                  }
                   System.out.println(assList.get(0).toString()); [B]//Here I DO NOT get the value output to my log file[/B]
                }
                catch (ConnectionNotAvailableException cnaEx)
                { ... }
                finally
                { ... }
             }
             catch (Exception Ex)
             { ... }
          }
       }
    }
    catch ( Exception ex )
    { ... }
    Last edited by meustace; 05-19-2010 at 05:46 PM.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Variables exist only in one block of code. You have this:
    Java Code:
    try {
      ArrayList assList = new ArrayList();
      //some other code
    } catch(Exception e) {}
    When the try catch block is over, the variable assList no longer exists. Define your array outside of the try catch block, that should solve your problem.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    bleah's Avatar
    bleah is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default

    I recommend using eclipse.It will warn you when you declare something out of scope. So you will detect your error before you can compile.

Similar Threads

  1. Scope Problem???
    By c_walker in forum New To Java
    Replies: 4
    Last Post: 12-12-2009, 03:30 AM
  2. Variable Scope
    By Laura Warren in forum New To Java
    Replies: 3
    Last Post: 01-11-2009, 11:16 PM
  3. jsf scope query
    By nc_newie in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 08-06-2008, 03:34 PM
  4. Replies: 2
    Last Post: 04-21-2008, 12:43 PM
  5. return out of scope?
    By another_steve in forum New To Java
    Replies: 5
    Last Post: 01-28-2008, 10:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •