CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2010
    Posts
    2

    [RESOLVED] NullPointerException frustration...

    Hello,

    I'm a student working on some homework and I can't get past or understand this small error. I'm only part way through so please ignore any other mistakes that you see but I'm having issues. I know which line of code is causing them but I don't understand why.

    Exception in thread "main" java.lang.NullPointerException
    at bonusProgram1.bonusProgram.readGraph(bonusProgram.java:87)
    at bonusProgram1.bonusProgram.main(bonusProgram.java:21)

    adjacencyMatrix[k].add(j); is causing the error. Any insight you can offer would be greatly appreciated.
    Code:
    package bonusProgram1;
    
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.File;
    import java.lang.reflect.Array;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class bonusProgram {
    
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
    		// TODO Auto-generated method stub
    		readGraph();
    		//printGraph();
    		//shortestPath();
    		
    			
    	
    	}
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////	
    	private static void shortestPath() {
    		// TODO Auto-generated method stub
    		
    	}
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    	private static void printGraph() {
    		// TODO Auto-generated method stub
    		
    	}
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    	
    	
    	@SuppressWarnings("unchecked")
    	private static void readGraph() {
    		// TODO Auto-generated method stub
    		FileReader file = null;
    		try {
    			file = new FileReader("graph.txt");
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		Scanner scanner = new Scanner(file);
    		ArrayList<Integer> listBegin =  new ArrayList<Integer>();
    		ArrayList[] adjacencyMatrix;
    		adjacencyMatrix = new ArrayList[10];
    		for(int i =0;i < 9 ; i++){
    			adjacencyMatrix [i] = new ArrayList<Integer>(); 
        	}
    		int scan = 0;
    		
    		System.out.println("AM!!   " + adjacencyMatrix);
    		while (scanner.hasNextLine()) {
    			int line = scanner.nextInt();
    			listBegin.add(line);
    			scan += 1;
    			}
    		
    		for (int i1 = 0; i1 < listBegin.size(); i1++){
    			System.out.println("Reading line: " + listBegin.get(i1));
    			}
    		System.out.println("AM!!   " + adjacencyMatrix);
    		
    		
    		for (int i2 = 0; i2 < listBegin.size(); i2 += 2){
    			int j = (int)listBegin.get(i2);
    			int h = i2 +1;
    			int k = (int)listBegin.get(h);
    			adjacencyMatrix[j].add(k);
    			adjacencyMatrix[k].add(j);
    			}
    		
    		
    		for (int i = 0; i < adjacencyMatrix.length; i++){
    			System.out.println("AM!! "+ i + adjacencyMatrix[i]);
    			}
    		
    	}
    }
    
    /**********************************************
     * The sample input
     * 0 9
     * 1 2
     * 1 3
     * 2 4
     * 2 5
     * 3 6
     * 4 5
     * 4 7
     * 4 8
     * 5 6
     * 5 9
     * 8 9
    ***************************************************/

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: NullPointerException frustration...

    You problem is here:
    Code:
    		for(int i =0;i < 9 ; i++){
    			adjacencyMatrix [i] = new ArrayList<Integer>();
    You are only creating ArrayList objects for the first 9 elements of your 10 element array.

    It is good practice to do the following when filling arrays:
    Code:
    		for(int i =0;i < adjacencyMatrix.length ; i++){
    			adjacencyMatrix [i] = new ArrayList<Integer>();
    If you use this approach then you don't have to worry what size the array is it just fills every element. Another advantage is if you decide the array should be a different size your code will still work without you having to change the for loop.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2010
    Posts
    2

    Re: NullPointerException frustration...

    Thank you very much! I will take you advice.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured