I have 4 classes: edge, graph, node, and shortestpath. I want to call methods from my graph class in the static main of my shortestpath class. The error i am getting is “Cannot make a static reference to the non-static method readFile() from the type graph”. I would appreciate any help im stuck :(!
public class edge<E>{}
public class node<E> extends edge<E>{}
public class graph<E> {
public node<E> BFS(E value)
{
if (adjList.isEmpty())
return (null);
ArrayList<node<E>> visitedNodes = new ArrayList<node<E>>();
node<E> sourceNode = adjList.get(0);
sourceNode.setVisited(true);
visitedNodes.add(sourceNode);
node<E> currNode = null;
while(!visitedNodes.isEmpty())
{
currNode = visitedNodes.get(0);
visitedNodes.remove(0);
if(currNode.getData() == value)
return (currNode);
//ListIterator<edge<E>> itr = currNode.incidentEdges.listIterator();
for(node<E> adjNode : adjList)
{
adjNode = adjNode.getChild();
if(!adjNode.isVisited())
{
adjNode.setVisited(true);
visitedNodes.add(adjNode);
}
}
}
return (null);
}
public void readFile()
{
File file = new File("Enron-Email.txt");
try
{
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
if(line.trim().startsWith("#"))
{
continue;
}
String[] tokens = line.split("\\t");
Integer parent = Integer.parseInt(tokens[0]);
Integer child = Integer.parseInt(tokens[1]);
addEdge((E) parent, (E) child);
}
scanner.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
public class shortestpath{
public static void main(Integer source, Integer dest) {
graph<E> myGraph = new graph<E>();
myGraph.readFile();
myGraph.BFS(source);
}
}
To invoke non-static methods from static methods you need to create an instance of the class and call that method on that instance.
so, In your case, you need to create an instance of Graph class and call
readFile()method from your main method of ShortestPath class.EDIT:
should be