Menu

EppsteinPowerLawGenerator problem

Anonymous
2012-08-28
2013-05-29
  • Anonymous

    Anonymous - 2012-08-28

    Hello!
    I am trying to learn the jung library but i am facing some difficulties. I want to generate a graph with EppsteinPowerLawGenerator.
    Here s my code:

    public class Generator {
        int edgecount = 0;
        int numV, numE, iter;
        Factory<Graph<node,edge>> graphFactory;
        Factory<node> nodeFactory = new Factory<node>() {
            public node create() {
                return new node(1);
            }
        };
        Factory<edge> edgeFactory = new Factory<edge>() {
            public edge create() {
                return new edge(3);
            }
        };
    
        public void GraphCreation(int numV, int numE, int iter) {
            EppsteinPowerLawGenerator<node,edge> gg = 
                    new EppsteinPowerLawGenerator<node,edge>(graphFactory, nodeFactory, edgeFactory, numV, numE, iter);
            gg.create();
        }
    
        public static void main(String args[]) {
            Generator g = new Generator();
            g.GraphCreation(10,10,500);
            System.out.println("All good");
        }
    
        class node {
            int id;
            public node(int id) {
                this.id = id;
            }
            public String toString() {
                return "V"+id;
            }
        }
        class edge {
            int id;
            int weight;
            public edge(int weight) {
                this.id = edgecount++;
                this.weight = weight;
            }
            public String toString(){
                return "E"+id;
            }
        }
    
    }
    

    And here is my error message:

    Exception in thread "main" java.lang.NullPointerException
    at edu.uci.ics.jung.algorithms.generators.random.EppsteinPowerLawGenerator.initializeGraph(EppsteinPowerLawGenerator.java:60)
    at edu.uci.ics.jung.algorithms.generators.random.EppsteinPowerLawGenerator.create(EppsteinPowerLawGenerator.java:87)
    at Generator.GraphCreation(Generator.java:25)
    at Generator.main(Generator.java:30)

    Probably i have some rookiw mistake in my java code (because i am a rookie) but i can't figure out the solution.
    Please help!

     
  • Tom Nelson

    Tom Nelson - 2012-08-28

    jung graphs are java collections. For any java object to work properly in a collection, it must implement equals and hashCode. You must properly implement equals and hashCode in your node and edge classes.

     
  • Anonymous

    Anonymous - 2012-08-28

    i don't think i understand, can you gine me an example?

     
  • Joshua O'Madadhain

    Salvador, this is a common Java problem.  Do a search on "java equals hashcode" and you'll find many, many articles about it.

    That said, the problem here is actually even more fundamental: you didn't initialize graphFactory, so calling create() on it (which is what a brief perusal of the code would have shown you was happening) resulted in an NPE.

    After you fix that, you may need to revisit the equals/hashCode issue.

    Joshua

     
  • Anonymous

    Anonymous - 2012-08-28

    ok i solve this problem. Thanks a lot!

    Another issue is that i want to visualize the graph with Gephi. I am trying to write the graph to a .paj file with PajekNetWriter class, but the first argument must be Graph<V,E>.
    I think my generated graph is stored as an factory interface and i can't use it with the aforementioned class. How can i solve this problem?

     
  • Joshua O'Madadhain

    Salvador:

    You need to read the documentation before posting.  Take a look at what the graph generator class actually does.

    I also strongly recommend that you take a look at one of the many available Java tutorials, e.g.: http://docs.oracle.com/javase/tutorial/
    That will help you get more familiar with Java.  As with any other Java library, JUNG is going to be difficult to use properly if you're not very familiar with Java itself.

    Joshua

     

Log in to post a comment.