Click to See Complete Forum and Search --> : [RESOLVED] NullPointerException frustration...


ChetC
April 23rd, 2010, 03:51 PM
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.


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
***************************************************/

keang
April 23rd, 2010, 04:04 PM
You problem is here:
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:
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.

ChetC
April 23rd, 2010, 04:10 PM
Thank you very much! I will take you advice.