We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Trouble Migrating to 1.0
Page Index Toggle Pages: 1
Trouble Migrating to 1.0 (Read 457 times)
Trouble Migrating to 1.0
Dec 30th, 2008, 8:49pm
 
The following code compiled fine under Processing 00135, but it fails with a NullPointerException in 1.0.1.

It fails in the following code around the second "n.applyForce(f);"

Any ideas what could be the problem?

-- Ian


void doLayout() {
 
 //calculate forces on each node
 //calculate spring forces on each node
 for (int i=0; i<g.getNodes().size(); i++) {
   ForcedNode n = (ForcedNode)g.getNodes().get(i);
   ArrayList edges = (ArrayList)g.getEdgesFrom(n);
   n.setForce(new Vector3D(0,0,0));
   for (int j=0; edges != null && j<edges.size(); j++) {
     SpringEdge e = (SpringEdge)edges.get(j);
     Vector3D f = e.getForceFrom();
     n.applyForce(f);
   }
   
   edges = (ArrayList)g.getEdgesTo(n);
   for (int j=0; edges != null && j<edges.size(); j++) {
     SpringEdge e = (SpringEdge)edges.get(j);
     Vector3D f = e.getForceTo();
     n.applyForce(f);
   }
   
 }
 
 
 
 //calculate the anti-gravitational forces on each node
 //this is the N^2 shittiness that needs to be optimized
 //TODO: at least make it N^2/2 since forces are symmetrical
 for (int i=0; i<g.getNodes().size(); i++) {
   ForcedNode a = (ForcedNode)g.getNodes().get(i);
   for (int j=0; j<g.getNodes().size(); j++) {
     ForcedNode b = (ForcedNode)g.getNodes().get(j);
     if (b != a) {
       float dx = b.getX() - a.getX();
       float dy = b.getY() - a.getY();
       float r = sqrt(dx*dx + dy*dy)/0.25;
       //F = G*m1*m2/r^2
       
       if (r != 0) { //don't divide by zero.
         float f = 100*(a.getMass()*b.getMass()/(r*r));
         Vector3D vf = new Vector3D(-dx*f, -dy*f, 0);
         a.applyForce(vf);
       }              
     }
   }
 }
 
 // Calculate the repulsive forces on each node
 // from the edge of the screen
 for (int i=0; i<g.getNodes().size(); i++) {
   
   ForcedNode a = (ForcedNode)g.getNodes().get(i);
   
   ForcedNode[] screenEdge = new ForcedNode[4];
   screenEdge[0] = new ForcedNode(new Vector3D(a.getX(), -500, 0));
   screenEdge[1] = new ForcedNode(new Vector3D(a.getX(), width + 500, 0));
   screenEdge[2] = new ForcedNode(new Vector3D(-500, a.getY(), 0));
   screenEdge[3] = new ForcedNode(new Vector3D(height + 500, a.getY(), 0));
   
   for (int j = 0; j < screenEdge.length; j++)
   {
       screenEdge[j].setMass(a.getMass() * (600/(a.getMass())));
       float dx = screenEdge[j].getX() - a.getX();
       float dy = screenEdge[j].getY() - a.getY();
       float r = sqrt(dx*dx + dy*dy)/0.25;
       //F = G*m1*m2/r^2  
       
       if (r != 0) { //don't divide by zero.
         float f = 100*(a.getMass()*screenEdge[j].getMass()/(r*r));
         Vector3D vf = new Vector3D(-dx*f, -dy*f, 0);
         a.applyForce(vf);
       }
   }
   
 }
 
 //move nodes according to forces
 for (int i=0; i<g.getNodes().size(); i++) {
   ForcedNode n = (ForcedNode)g.getNodes().get(i);
   if (n != g.getDragNode()) {
     n.setPosition(n.getPosition().add(n.getForce()));
     if (n.getPosition().getX() > width)
       n.getPosition().setX(width - 10);
     if (n.getPosition().getX() < 0)
       n.getPosition().setX(10);
     if (n.getPosition().getY() > height)
       n.getPosition().setY(height - 10);
     if (n.getPosition().getY() < 0)
       n.getPosition().setY(10);
   }
 }
}


Re: Trouble Migrating to 1.0
Reply #1 - Dec 30th, 2008, 10:35pm
 
I'm guessing you're using an outdated library..
Page Index Toggle Pages: 1