I have a couple of errors in my code and need some help figuring out how to fix it. Any help would be greatly appreciated!
Here are the errors:
symptom: java.lang.NullPointerException at assignment4.AdjacencyGraph.getHighestDegreeNode(AdjacencyGraph.java:136)
symptom: java.lang.NullPointerException at assignment4.AdjacencyGraph.addEdge(AdjacencyGraph.java:85)
IllegalStateForMatrix should be raised when calling getNumberOfEdges with 0 edges.
IllegalStateForMatrix should be called if getNumberOfEdges is called before createAdjacencyMatrix.
public class IllegalStateForMatrixException extends Exception {
public IllegalStateForMatrixException(String el) {
super("Illegal call to "+el + " before the matrix was created (must call createAdjacencyMatrix)");
}
}
class AdjacencyGraph extends Graph {
private ArrayList nodes = new ArrayList();
private int noEdges = 0;
public int[][] adjM;
/**
* Adjacency Matrix with integers.
*/
@Override
void createAdjacencyMatrix() {
adjM = new int[nodes.size()][nodes.size()];
int i;
int j;
for (i = 0; i < nodes.size(); i++) {
for (j = 0; j < nodes.size(); j++) {
adjM[i][j] = -1;
}
}
}
/**
* Adds the node.
*
* @param nodeName
*/
@Override
@SuppressWarnings("unchecked")
void addNode(String nodeName) {
nodes.add(nodeName);
}
/**
* Adds the edge.
*
* @param fromNode
* @param toNode
* @param weight
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
void addEdge(String fromNode, String toNode, int weight)
throws ElementNotFoundException, IllegalStateForMatrixException {
{
try {
int i;
int j;
for (i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(fromNode)) {
break;
}
}
if (i == nodes.size()) {
throw new ElementNotFoundException("Exception");
}
for (j = 0; j < nodes.size(); j++) {
if (nodes.get(j).equals(toNode)) {
break;
}
}
if (j == nodes.size()) {
throw new ElementNotFoundException("Exception");
}
adjM[i][j] = weight;
adjM[j][i] = weight;
noEdges++;
}
catch (ElementNotFoundException e) {
System.out.println("Exception found");
}
}
}
/**
* Returns the number of nodes.
*
* @return number of nodes
*/
@Override
int getNumberOfNodes() {
return nodes.size();
}
/**
* Returns the number of edges
*
* @return number of edges
* @throws IllegalStateForMatrixException
*/
@Override
int getNumberOfEdges() throws IllegalStateForMatrixException {
if (nodes.size() <= 0)
throw new IllegalStateForMatrixException("Error");
return noEdges;
}
/**
* Returns the highest degree node.
*
* @return highest degree node.
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
public String getHighestDegreeNode()
throws ElementNotFoundException, IllegalStateForMatrixException {
int i;
int ansIndex = 0;
int j;
int ansCount = 0;
for (i = 0; i < nodes.size(); i++) {
int k = 0;
for (j = 0; j < nodes.size(); j++) {
if (adjM[i][j] != -1) {
k++;
}
}
if (k > ansCount) {
ansCount = k;
ansIndex = i;
}
}
return (String) nodes.get(ansIndex);
}
/**
* Cost of the edge between nodes.
*
* @param fromNode
* @param toNode
* @return returns -1
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
int costOfEdgeBetween(String fromNode, String toNode)
throws ElementNotFoundException, IllegalStateForMatrixException {
try {
int i;
int j;
for (i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(fromNode)) {
break;
}
}
if (i == nodes.size()) {
throw new ElementNotFoundException("Exception");
}
for (j = 0; j < nodes.size(); j++) {
if (nodes.get(j).equals(toNode)) {
break;
}
}
if (j == nodes.size()) {
throw new ElementNotFoundException("Exception");
}
return adjM[i][j];
}
catch (ElementNotFoundException e) {
System.out.println("Exception found");
}
return -1;
}
/**
*
* @param fromName
* @param toName
* @return false
* @throws ElementNotFoundException
* @throws IllegalStateForMatrixException
*/
@Override
boolean hasPathBetween(String fromName, String toName)
throws ElementNotFoundException, IllegalStateForMatrixException {
try {
ArrayStack<Integer> st = new ArrayStack<Integer>();
int[] visited = new int[nodes.size()];
int i;
int j;
int start;
int end;
for (i = 0; i < nodes.size(); i++) {
visited[i] = 0;
}
for (i = 0; i < nodes.size(); i++) {
if (nodes.get(i).equals(fromName)) {
break;
}
}
start = i;
for (j = 0; j < nodes.size(); j++) {
if (nodes.get(j).equals(toName)) {
break;
}
}
end = j;
st.push(start);
visited[start] = 1;
while (st.isEmpty() != true) {
i = st.pop();
if (i == end) {
return true;
}
for (j = 0; j < nodes.size(); j++) {
if (adjM[i][j] != -1 && visited[j] == 0) {
visited[j] = 1;
st.push(j);
}
}
}
return false;
}
catch (Exception e) {
System.out.println("Exception found");
}
return false;
}
/**
* The number of Isolated points
*
* @return ans
* @throws IllegalStateForMatrixException
*/
@Override
int numIsolatedPoints() throws IllegalStateForMatrixException {
int i;
int j;
int ans = 0;
for (i = 0; i < nodes.size(); i++) {
for (j = 0; j < nodes.size(); j++) {
if (adjM[i][j] != -1) {
break;
}
}
if (j == nodes.size()) {
ans++;
}
}
return ans;
}
/**
* Inclusiveness is the percentage of points in the graph that are not
* isolated
*
* @return ((float)i)/nodes.size()
* @throws IllegalStateForMatrixException
*/
@Override
float inclusiveness() throws IllegalStateForMatrixException {
int i = nodes.size() - numIsolatedPoints();
return ((float) i) / nodes.size();
}
/**
* Density of matrix
*
* @return (2*(float)noEdges)/(nodes.size()*(nodes.size()-1))
* @throws IllegalStateForMatrixException
*/
@Override
float density() throws IllegalStateForMatrixException {
return (2 * (float) noEdges) / (nodes.size() * (nodes.size() - 1));
}
public static void main(String[] args) throws IllegalStateForMatrixException, ElementNotFoundException {
AdjacencyGraph ag = new AdjacencyGraph();
ag.addNode("A");
ag.addNode("B");
ag.addNode("C");
ag.addNode("D");
ag.createAdjacencyMatrix();
ag.addEdge("A", "B", 5);
ag.addEdge("A", "C", 2);
ag.addEdge("B", "C", 1);
ag.addEdge("B", "D", 2);
ag.print();
System.out.println("Num nodes:"+ag.getNumberOfNodes());
System.out.println("Num edges:"+ag.getNumberOfEdges());
System.out.println("Highest degree node = "+ag.getHighestDegreeNode());
System.out.println("Cost of edge from A to B = "+ag.costOfEdgeBetween("A", "B"));
System.out.println("Cost of edge from A to C = "+ag.costOfEdgeBetween("A", "C"));
System.out.println("Cost of edge from B to C = "+ag.costOfEdgeBetween("B", "C"));
System.out.println("Cost of edge from A to D = "+ag.costOfEdgeBetween("A", "D"));
System.out.println("Has path from A to B = "+ag.hasPathBetween("A", "B"));
System.out.println("Has path from A to C = "+ag.hasPathBetween("A", "C"));
System.out.println("Has path from B to C = "+ag.hasPathBetween("B", "C"));
System.out.println("Has path from A to D = "+ag.hasPathBetween("A", "D"));
System.out.println("Number of isolated points is: "+ag.numIsolatedPoints());
System.out.println("Graph inclusiveness is: "+ag.inclusiveness());
System.out.println("Graph density is: "+ag.density());
}
}