Issues with array lists

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

42 Replies - 626 Views - Last Post: 28 September 2012 - 10:24 AM Rate Topic: -----

#1 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Issues with array lists

Posted 21 September 2012 - 01:27 PM

I am completely new to Array lists and I am running into this issue of giving me IndexOutOfBoundsException
public void ArrayList()
    {
        
    ArrayList regionOne = new ArrayList();
    ArrayList regionThree = new ArrayList();
    ArrayList regionFive = new ArrayList();
    ArrayList regionSix = new ArrayList();
    
    int index=0;

            
         for (index = 0;
                index < (numStates - 1);
                index++)
         {
             if(unitedStates[index].getRegionNumber()==1)
             {
                 regionOne.add(unitedStates[index]);
             }
             else if(unitedStates[index].getRegionNumber()==3)
                     {
                         regionThree.add(unitedStates[index]);
                     }
             else if(unitedStates[index].getRegionNumber()==5)
                     {
                         regionFive.add(unitedStates[index]);
                     }
             else if(unitedStates[index].getRegionNumber()==6)
                     {
                         regionSix.add(unitedStates[index]);
                     }
         }
         for(int i=0; i<index; i++)
            System.out.println(regionOne.get(i));
       
             
    }
}


OUTPUT:
Maine Augusta ME 1244250 New_England 1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
Massachusetts Boston MA 6147132 New_England 1
Connecticut Hartford CT 3274069 New_England 1
Rhode_Island Providence RI 988480 New_England 1
Vermont Montpelier VT 588632 New_England 1
New_Hampshire Concord NH 1185048 New_England 1
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at StatesController.ArrayList(StatesController.java:123)
at Main.main(Main.java:24)
Java Result: 1

Is This A Good Question/Topic? 0
  • +

Replies To: Issues with array lists

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5433
  • View blog
  • Posts: 8,747
  • Joined: 19-March 11

Re: Issues with array lists

Posted 21 September 2012 - 01:36 PM

That error is telling you you're trying to remove an element that isn't there. Clearly, you only had 6 states loaded, because when you got to the seventh, there was nothing to remove.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 8,810
  • Joined: 20-September 08

Re: Issues with array lists

Posted 21 September 2012 - 01:36 PM

The code you posted is not very useful - most of the key variables are not shown
Was This Post Helpful? 0
  • +
  • -

#4 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Issues with array lists

Posted 21 September 2012 - 01:37 PM

So how do i stop it before it tries to look at the last element?
Was This Post Helpful? 0
  • +
  • -

#5 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Issues with array lists

Posted 21 September 2012 - 01:42 PM

I hope this is more useful code
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author davidSnow
 */

import java.io.*;
import java.util.ArrayList;
import java.lang.*;
public class StatesController {
private States[] unitedStates;
private ArrayList regionOne = new ArrayList();
private ArrayList regionThree = new ArrayList();
private ArrayList regionFive = new ArrayList();
private ArrayList regionSix = new ArrayList();
private int front;
private int rear;
private int numStates;
private int maxSize;
//------------------------------------------------------------------------------
public StatesController(int maxSize)
{
    unitedStates=new States[maxSize];
    front=0;
    rear=-1;
    numStates=0;
}
//------------------------------------------------------------------------------ 
 public void loadData(String filename) throws IOException
{
    FileInputStream fis1 = new FileInputStream("States2.txt");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
    String state, capital, abbr, region;
    int pop, regNum;
    String inputString = br1.readLine();
    
    while(inputString != null)
    {
        try{
        state = inputString.substring(0,15).trim();
        capital = inputString.substring(15,30).trim();
        abbr = inputString.substring(30,32).trim();
        pop = Integer.parseInt(inputString.substring(32,40).trim());
        region = inputString.substring(40,55).trim();
        regNum = Integer.parseInt(inputString.substring(55,56));
        unitedStates[numStates] = new States(state, capital, abbr, pop, region, 
                regNum);
        numStates++;
       try{
           if(regNum==2||regNum==4)//if region number is 2 or 4 throw exception
           
               throw new RegionNumbersException();
          }
        catch(RegionNumbersException exception)
                {
                String file  =  "ErrorFile.2.txt"; 
                FileWriter fw = new FileWriter (file, true);        
                BufferedWriter bw = new BufferedWriter (fw);  
                PrintWriter outFile = new PrintWriter (bw);   
                outFile.println(" ");
                outFile.println(inputString);
                outFile.println ("BAD INPUT:   Bad/Invalid Region Number "
                        +regNum+"\n"); 
                outFile.close();
                }
        }//end try
        catch(NumberFormatException exception)
       {//catch statement
                String file  =  "ErrorFile.2.txt"; 
                FileWriter fw = new FileWriter (file, true);        
                BufferedWriter bw = new BufferedWriter (fw);  
                PrintWriter outFile = new PrintWriter (bw); 
                outFile.println(" ");
                outFile.println (inputString);	// writes this as a stream.  
                outFile.println("BAD INPUT: Bad/Invalid Region Number "+
                        (inputString.substring(55,56)));//prints region
                outFile.println("BAD INPUT: Bad/Invalid Population Number "+
                        (inputString.substring(32,40).trim())+"\n");//prints pop
                outFile.close();
        }
        finally
            {
                inputString = br1.readLine();
            }
        } // end while (inputString != null) 
        br1.close();
        }
//------------------------------------------------------------------------------    
public void ArrayList()
    {
        
        for (int index = 0;
                index < (numStates - 1);
                index++)
         {
             if(unitedStates[index].getRegionNumber()==1)
             {
                 regionOne.add(unitedStates[index]);
             }
             else if(unitedStates[index].getRegionNumber()==3)
                     {
                         regionThree.add(unitedStates[index]);
                     }
             else if(unitedStates[index].getRegionNumber()==5)
                     {
                         regionFive.add(unitedStates[index]);
                     }
             else if(unitedStates[index].getRegionNumber()==6)
                     {
                         regionSix.add(unitedStates[index]);
                     }
         }
         for(int i=0; i<numStates; i++)
         
            System.out.println(regionOne.get(i));
         
             
    }
}
    

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5433
  • View blog
  • Posts: 8,747
  • Joined: 19-March 11

Re: Issues with array lists

Posted 21 September 2012 - 01:46 PM

Easiest way would be to just loop over the elements, not the indexes:


for (String s: regionOne)
{
  System.out.println(s);
}


To do this, you'd have to declare your ArrayLists properly:

ArrayList<String> regionOne = new ArrayList<String>();


This tells the compiler what you're going to keep in that list, so it can know what it can take out of the list.

public void ArrayList()



By the way, this is awful. you might call this method something like "populateArrayList" or something. ArrayList is the name of a library class - your method calls are going to look like screwed-up object declarations or constructors to any java programmer.

This post has been edited by jon.kiparsky: 21 September 2012 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#7 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Issues with array lists

Posted 21 September 2012 - 01:50 PM

So the reason why I am trying to do this is so that I can pass objects into a queue and I am taking baby steps coding small things along the way. So the reason why I am going index by index is so that I can eventually achieve that mission. So unfortunately I feel like I am stuck going index by index. So I how do I go through the indexes without running into erros

This post has been edited by Snow1986: 21 September 2012 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5433
  • View blog
  • Posts: 8,747
  • Joined: 19-March 11

Re: Issues with array lists

Posted 21 September 2012 - 01:58 PM

get the indexes from the object you're iterating:


for (int i = 0; i <regionOne.size(); i++){
  System.out.println(regionOne.get(i));
}


EDIT: But you should still declare your lists properly - it'll make your life a lot easier.

This post has been edited by jon.kiparsky: 21 September 2012 - 01:59 PM

Was This Post Helpful? 1
  • +
  • -

#9 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Issues with array lists

Posted 22 September 2012 - 07:28 AM

Okay, so I am now trying to populate the queues from the ArrayLists and I am testing the previous bit of code in a new method and it is giving me a nullpointerexception at run time. This is the way the code looks now.
public void populateQueues()
{
    for(int i=0; i<regionOne.size(); i++)
        System.out.println(regionOne.get(i));
}
}

Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5433
  • View blog
  • Posts: 8,747
  • Joined: 19-March 11

Re: Issues with array lists

Posted 22 September 2012 - 09:22 AM

Has the ArrayList been initialized at this point?
Here's one way to find out:
System.out.println("regionOne is null? " + regionOne==null);


(how would you find out the number of elements in regionOne when this method is called?)
Was This Post Helpful? 0
  • +
  • -

#11 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Issues with array lists

Posted 22 September 2012 - 10:37 AM

wouldn't I just use the .size method again?
Was This Post Helpful? 0
  • +
  • -

#12 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Issues with array lists

Posted 22 September 2012 - 10:43 AM

This is my method revised and I am still getting the nullPointerException. regionOne has been filled, so I don't know why I would continue to get this error.
public void populateQueues()
{

   
        while (regionOne!=null);
        {
            for(int i=0; i<regionOne.size(); i++)
                System.out.print(regionOne.get(i));
        }

}

This post has been edited by Snow1986: 22 September 2012 - 10:50 AM

Was This Post Helpful? 0
  • +
  • -

#13 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Issues with array lists

Posted 22 September 2012 - 11:41 AM

Okay so I have implemented and played around with the ArrayLists and it has occured to me that I may not have even implemented the ArrayLists correctly. I am not sure if am filling the array lists outside of the populateArrayLists() method.
import java.io.*;
import java.util.ArrayList;
import java.lang.*;
public class StatesController {
private States[] unitedStates;
private States[] firstRegion;
private ArrayList regionOne;
private ArrayList regionThree;
private ArrayList regionFive;
private ArrayList regionSix;
private int front;
private int rear;
private int numStates;
private int maxSize;
//------------------------------------------------------------------------------
public StatesController(int maxSize)
{
    unitedStates=new States[maxSize];
    regionOne = new ArrayList<States>();
    front=0;
    rear=-1;
    numStates=0;
}
//------------------------------------------------------------------------------ 
 public void loadData(String filename) throws IOException
{
    FileInputStream fis1 = new FileInputStream("States2.txt");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(fis1));
    String state, capital, abbr, region;
    int pop, regNum;
    String inputString = br1.readLine();
    
    while(inputString != null)
    {
        try{
        state = inputString.substring(0,15).trim();
        capital = inputString.substring(15,30).trim();
        abbr = inputString.substring(30,32).trim();
        pop = Integer.parseInt(inputString.substring(32,40).trim());
        region = inputString.substring(40,55).trim();
        regNum = Integer.parseInt(inputString.substring(55,56));
        unitedStates[numStates] = new States(state, capital, abbr, pop, region, 
                regNum);
        numStates++;
       try{
           if(regNum==2||regNum==4)//if region number is 2 or 4 throw exception
           
               throw new RegionNumbersException();
          }//end if
        catch(RegionNumbersException exception)
                {
                String file  =  "ErrorFile.2.txt"; 
                FileWriter fw = new FileWriter (file, true);        
                BufferedWriter bw = new BufferedWriter (fw);  
                PrintWriter outFile = new PrintWriter (bw);   
                outFile.println(" ");
                outFile.println(inputString);
                outFile.println ("BAD INPUT:   Bad/Invalid Region Number "
                        +regNum+"\n"); 
                outFile.close();
                }//end catch
        }//end try
        catch(NumberFormatException exception)
       {//catch statement
                String file  =  "ErrorFile.2.txt"; 
                FileWriter fw = new FileWriter (file, true);        
                BufferedWriter bw = new BufferedWriter (fw);  
                PrintWriter outFile = new PrintWriter (bw); 
                outFile.println(" ");
                outFile.println (inputString);	// writes this as a stream.  
                outFile.println("BAD INPUT: Bad/Invalid Region Number "+
                        (inputString.substring(55,56)));//prints region
                outFile.println(exception);
                outFile.close();
        }//end catch
        finally
            {
                inputString = br1.readLine();
            }//end finally 
        } // end while 
        br1.close();
        }//end loadData()
//------------------------------------------------------------------------------    
public void populateArrayList()
    {
        ArrayList<States> regionOne = new ArrayList();
        ArrayList regionThree = new ArrayList();
        ArrayList regionFive = new ArrayList();
        ArrayList regionSix = new ArrayList();
        for (int index = 0;
                index < (numStates - 1);
                index++)
         {
             if(unitedStates[index].getRegionNumber()==1)
             {
                 regionOne.add(unitedStates[index]);
             }
             else if(unitedStates[index].getRegionNumber()==3)
                     {
                         regionThree.add(unitedStates[index]);
                     }
             else if(unitedStates[index].getRegionNumber()==5)
                     {
                         regionFive.add(unitedStates[index]);
                     }
             else if(unitedStates[index].getRegionNumber()==6)
                     {
                         regionSix.add(unitedStates[index]);
                     }
          }
    }
//------------------------------------------------------------------------------
public void populateQueues()
{

   System.out.println(regionOne==null);
        while (regionOne!=null)
        {
            System.out.print(regionOne.size());
                
        }

}

}




Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon

Reputation: 5433
  • View blog
  • Posts: 8,747
  • Joined: 19-March 11

Re: Issues with array lists

Posted 22 September 2012 - 11:52 AM

View PostSnow1986, on 22 September 2012 - 12:37 PM, said:

wouldn't I just use the .size method again?


Yes, that's correct.


View PostSnow1986, on 22 September 2012 - 12:43 PM, said:

This is my method revised and I am still getting the nullPointerException. regionOne has been filled, so I don't know why I would continue to get this error.
public void populateQueues()
{

   
        while (regionOne!=null);
        {
            for(int i=0; i<regionOne.size(); i++)
                System.out.print(regionOne.get(i));
        }

}


This is the insidious semicolon of doom error. You have a semicolon at the end of this line:
        while (regionOne!=null);



Which means it'll do nothing at all as long as regionOne is not null. Since it is null, the loop ends, and you then try to apply a method on a null object, and thus your nullpointer exception.

So yes, it is the case that you are not initializing that arraylist correctly.
Was This Post Helpful? 0
  • +
  • -

#15 Snow1986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 01-September 12

Re: Issues with array lists

Posted 22 September 2012 - 11:56 AM

I caught that before my last post and it still is not displaying any of the States objects that I thought I had passed to it.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3