Okay so I have no idea why this is happening. Its not even a question about methodology I am just wondering if I should build a new project or something at this point because I can't see how/why I am getting an error. Here is my code:
Driver:
package graphalgorithms; import java.io.*; import java.util.*; public class GraphAlgorithms { public static void main(String[] args) throws IOException { PrimsAlgorithm graph = new PrimsAlgorithm(); String inputFile = "src/input/input.txt"; String fName = inputFile; String thisLine; String[][] data = new String[100][100]; int count = 0; int size = 0; FileInputStream geek = new FileInputStream(fName); BufferedReader in = new BufferedReader(new InputStreamReader(geek, "UTF-16")); while ((thisLine = in.readLine()) != null) { String[] array = thisLine.split(","); System.arraycopy(array, 0, data[count], 0, array.length); size = array.length; count++; } String[] vert = new String[size]; System.arraycopy(data[0], 0, vert, 0, size); String[][] arrayData = new String[size][size]; for (int i = 0; i < size; i++) { System.arraycopy(data[i + 1], 0, arrayData[i], 0, size); } Scanner sc = new Scanner(System.in); System.out.println("Please select the algorithm you wish to run: " + "1 for Prim's algorithm, 2 for Kruskal's algorithm" + "or 3 for Floyd-Warshall’s algorithm "); while (sc.hasNext()) { int i = sc.nextInt(); switch (i) { case 1: System.out.println("You chose Prim's algorithm."); graph.initializeData(arrayData,vert); break; case 2: System.out.println("You chose Kruskal's algorithm."); break; case 3: System.out.println("You chose Floyd-Warshall's algorithm."); break; default: System.out.println("Invalid entry"); break; } System.out.println(); System.out.println("Please select the algorithm you wish to run: " + "1 for Prim's algorithm, 2 for Kruskal's algorithm" + "or 3 for Floyd-Warshall’s algorithm "); } } }
Class:
package graphalgorithms; public class PrimsAlgorithm { private String[] verticies; private int[][] actualData; private final int max = Integer.MAX_VALUE; private final int V = verticies.length; public void initializeData(String[][] incomingArray, String [] incomingVerticies) { verticies = incomingVerticies; actualData = new int[incomingArray.length-1][incomingArray.length]; for(int i = 0; i < actualData.length; i++){ for(int j = 0; j < incomingArray.length; j++){ if (incomingArray[i][j].equals("\u221E")) { actualData[i][j] = 999999; //if inf symbol is seen replace with high value weight } else { actualData[i][j] = Integer.parseInt(incomingArray[i][j]); } } } //primsMST(actualData); } }
Now when I run the program it errors at line 10 where I create an instance of class PrimsAlgorithm even though I have ran the program successfully before. I am not sure when this stared happening as a change a few names around but not much then all of a sudden...
This is the error its giving me:
run:
Exception in thread "main" java.lang.NullPointerException
at graphalgorithms.PrimsAlgorithm.<init>(PrimsAlgorit hm.java:8)
at graphalgorithms.GraphAlgorithms.main(GraphAlgorith ms.java:10)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Any ideas? This is line 10:
PrimsAlgorithm graph = new PrimsAlgorithm();