Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:12:32+00:00 2026-06-14T12:12:32+00:00

I have 4 classes: edge, graph, node, and shortestpath. I want to call methods

  • 0

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);    
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:12:34+00:00Added an answer on June 14, 2026 at 12:12 pm

    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.

    Graph<YourType> g = new Graph<YourType>(); //replace YourType with the type you want to pass like Integer, blah blah...
    g.readFile();
    

    EDIT:

    graph<E> myGraph = new graph<E>(); 
    

    should be

    graph<Integer> myGraph = new graph<Integer>(); //Or some other valid types. 
    
    E is just an representation of Element. provide a valid element type
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have classes which are inherited from abstract Packet( this class has abstract method
I have a class Node, and a struct Edge. But when the struct is
I have classes generated from a WCF soap service. public partial class Job {
i have classes like this class A : public QDialog { A(QWidget *parent); static
I have classes structured like this: Public MustInherit Class A ' several properties End
Say I have classes class A{ //code for class A } class B{ //code
I have a situation where i have classes JobDescription and Image . I want
If I have classes of Type A and B: public class A { public
I get an error whenever I try to create a simple edge from a
I have classes like these: class MyDate { int year, month, day; } class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.