Results 1 to 2 of 2
  1. #1
    Ladyjest1 is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Post Java.lang.ArrayIndexOutOfBoundsException

    Hi,

    I am somewhat of a semi-intermediate Java programmer. I am having problems with an Array Index Out of Bounds Exception=0 at genGraph2.main(genGraph2.java:312) and I don't really see what the problem is. I've looked and looked for possible errors. I am trying to use a file and read multiple lines in order to compare a source node and multiple destination nodes in order to determine if edges exist. This code is somewhat outdated but I am implementing changes in it. I am using NetBeans but I am running it in the command prompt. In the command prompt I am receiving the following errors:

    Java Code:
            java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0 
            at java util.Vector.get(Unknown Source)
            at genGraph.checkconnected(genGraph.java: 515)
            at genGraph.main(genGraph.java:381)
    Here is the code with the line areas highlighted.


    Java Code:
    class genGraph2
    {
    
    	static TreeMap[] nodePositions;
    	static int numNodes = -1;
    	static double maxTransRange = 250;
            static double distThreshold = -1;
    	static Random randN = new Random(System.currentTimeMillis()*10);
    	static double samplingFreq = 0.25;
            static double simTime = -1;
    
            public static void main(String[] args)
    	{
           
              try
                {
    
            
    [COLOR="Red"](line 312)FileInputStream fis = new FileInputStream(args[0]);[/COLOR]
    		simTime = Double.parseDouble(args[1]);
                    //distThreshold = Double.parseDouble(args[2]);
    
    		ObjectInputStream ois = new ObjectInputStream(fis);
    
    		nodePositions = (TreeMap[]) ois.readObject();
    		numNodes = nodePositions.length;
    
    		ois.close();
    		fis.close(); 
    
                    Vector sdPairs = new Vector();
    
    		FileReader fr = new FileReader("singleSD.txt");
    		BufferedReader br = new BufferedReader(fr);
    
    		String line = null;
    
    		while ( (line = br.readLine() )!= null)
    		{
                        StringTokenizer stk = new StringTokenizer(line);
    
                        int source = Integer.parseInt(stk.nextToken());
                        int dest = Integer.parseInt(stk.nextToken());
                        sdPairs.add(source+" "+dest);
    		}
    
                  if(!pathExists){
                        topology topo = new topology();
                        topo.construct(currentTime);
    
          [COLOR="Red"](line 381) if (!checkConnected(topo.getNodeList(), topo.getAdjList())){[/COLOR]
                            System.out.println(" ");
    			System.out.println(" the topology is not connected at time: "+currentTime);
                            System.out.println("Avg Lifetime:" + (totalLifetime/lifetimeCounter));
    			continue timeLoop;
                        }        
    
    
    
    
    static boolean checkConnected(Vector nodeList, TreeMap adjList)
    	{
    		int rootID = (int) (Math.floor(randN.nextDouble()*nodeList.size()));
    
    		Vector nodesExplored = new Vector();
    		Vector nodesFIFOQueue = new Vector();
    
    		nodesExplored.add(new Integer(rootID));
    		nodesFIFOQueue.add(new Integer(rootID));
    
    		while (nodesFIFOQueue.size() > 0)
    		{
    			int firstNodeID = ( (Integer)                                 
                            nodesFIFOQueue.get(0)).intValue();
    	[COLOR="red"](line 515)	Node firstNode = (Node) nodeList.get(firstNodeID);[/COLOR]
    			Vector neighbList = (Vector) adjList.get(new            Integer(firstNodeID));
    
    			for (int neighbIndex = 0; neighbIndex < neighbList.size(); neighbIndex++)
    			{
    				int neighbID = ( (Integer) neighbList.get(neighbIndex)).intValue();
    				
    				if (!nodesExplored.contains(new Integer(neighbID)))
    				{
    					nodesExplored.add(new Integer(neighbID));
    					nodesFIFOQueue.add(new Integer(neighbID));
    				}
    
    			}
    
    			nodesFIFOQueue.remove(0);
    		}
    
    		if (nodesExplored.size() < nodeList.size())
    			return false; // underlying network not connected
    		else
    			return true; // underlying network connected
    		
    
    	}

    I could really use some advice thank you in advance.
    Last edited by Fubarable; 07-12-2010 at 11:27 PM. Reason: Moderator edit: code tags added

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    args[0]
    What is the length of the args array? Is it greater than 0?

    IF you expect args to your program, you need to test if they are present and if not,
    either set some default values
    or give an error message and exit.

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException: 6
    By pyat77 in forum Java Applets
    Replies: 6
    Last Post: 03-06-2010, 02:12 PM
  2. java.lang.ArrayIndexOutOfBoundsException
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 08:29 AM
  3. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 10:09 AM
  4. java.lang.ArrayIndexOutOfBoundsException
    By riccian in forum New To Java
    Replies: 0
    Last Post: 03-18-2008, 10:38 AM
  5. java.lang.ArrayIndexOutOfBoundsException
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-02-2007, 10:40 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
  •