如何在NetLogo

时间:2015-05-21 14:19:43

标签: netlogo

我正在尝试用Java扩展我的扩展中的海龟。在默认的命令原语中,我正在执行以下操作:

public void perform(Argument[] argmnts, Context cntxt) throws ExtensionException, LogoException {
    try {
        Manager.world = (World) cntxt.getAgent().world();
        Manager.empirical = readNetwork(new File(filename));
        AgentSet breed = Manager.world.getBreed("TURTLES");
        for(Vertex v:Manager.empirical.getVertices()){
           Turtle turtle = Manager.world.createTurtle(breed);
           turtle.setTurtleOrLinkVariable("NAME", v.getName());
           turtle.setTurtleOrLinkVariable("TYPEOF", v.getTypeof());
           turtle.setTurtleOrLinkVariable("THRESHOLD", v.getThreshold());
           turtle.setTurtleOrLinkVariable("AGE", v.getAge());
        }
    } catch (AgentException ex) {
        throw new ExtensionException("Problem creating turtle " + ex.toString());
    }
}

但在NetLogo中出现此错误:

error (NullPointerException)
while observer running DIFFCER:CREATE-POTTERS
called by procedure SETUP
called by Button 'setup'

NetLogo is unable to supply you with more details about this error. Please report the problem at https://github.com/NetLogo/NetLogo/issues, or to bugs@ccl.northwestern.edu, and paste the contents of this window into your report.

java.lang.NullPointerException
 at org.nlogo.agent.World.getVariablesArraySize(World.java:1191)
 at org.nlogo.agent.Turtle.<init>(Turtle.java:77)
 at org.nlogo.agent.Turtle.<init>(Turtle.java:72)
 at org.nlogo.agent.World.createTurtle(World.java:384)
 at org.nlogo.extensions.diffceram.LoadNetwork.perform(LoadNetwork.java:84)
 at org.nlogo.prim._extern.perform(_extern.java:54)
 at org.nlogo.nvm.Context.stepConcurrent(Context.java:91)
 at org.nlogo.nvm.ConcurrentJob.step(ConcurrentJob.java:82)
 at org.nlogo.job.JobThread.org$nlogo$job$JobThread$$runPrimaryJobs(JobThread.scala:143)
 at org.nlogo.job.JobThread$$anonfun$run$1.apply$mcV$sp(JobThread.scala:78)
 at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76)
 at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76)
 at scala.util.control.Exception$Catch.apply(Exception.scala:88)
 at org.nlogo.util.Exceptions$.handling(Exceptions.scala:41)
 at org.nlogo.job.JobThread.run(JobThread.scala:75)

NetLogo 5.2.0
main: org.nlogo.app.AppFrame
thread: JobThread
Java HotSpot(TM) 64-Bit Server VM 1.6.0_65 (Apple Inc.; 1.6.0_65-b14-466.1-11M4716)
operating system: Mac OS X 10.10.3 (x86_64 processor)
Scala version 2.9.2
JOGL: (3D View not initialized)
OpenGL Graphics: (3D View not initialized)

我想要避免此错误的任何暗示?

1 个答案:

答案 0 :(得分:1)

我设法用这段代码回答了我的问题:

world = (World) cntxt.getAgent().world();
// in my case, I start with a network
empirical = readNetwork(new File(filename));
AgentSet agentset = new org.nlogo.agent.ArrayAgentSet(Turtle.class, Manager.empirical.getVertexCount(), false, Manager.world);
AgentSet breed = Manager.world.turtles();
// add a turtle for every node that I have in the network
for(Vertex v:Manager.empirical.getVertices()){
    Turtle turtle = Manager.world.createTurtle(breed);
    turtle.setTurtleOrLinkVariable("NAME", v.getName());
    turtle.setTurtleOrLinkVariable("TYPEOF", v.getTypeof());
    turtle.setTurtleOrLinkVariable("THRESHOLD", v.getThreshold());
    turtle.setTurtleOrLinkVariable("AGE", v.getAge());
    turtle.setTurtleOrLinkVariable("XCOR", rescaleX(v.getX()));
    turtle.setTurtleOrLinkVariable("YCOR", rescaleY(v.getY()));
    turtle.setTurtleOrLinkVariable("RELIGION", v.getReligion());
    // you add as many variable you have then
    agentset.add(turtle); 
}