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

    Post Java.lang.ArrayIndexOutOfBoundsException


    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( 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( 515)
            at genGraph.main(
    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)
    [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;
                    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);
                        topology topo = new topology();
          [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)                                 
    	[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));
    		if (nodesExplored.size() < nodeList.size())
    			return false; // underlying network not connected
    			return true; // underlying network connected

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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


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