Michael Stachowsky Michael Stachowsky - 1 year ago 69
Groovy Question

Why is gremlin outputting "null" at the of my script?

I'm learning to use Gremlin and am just starting to make basic graphs using the command line. I've learned that I can put this into a script, which is basically just a bunch of groovy (which I'm also just becoming familiar with). I run the following script, and everything works great except I get `==> null' once the script is done running (in addition to all of the correct messages regarding the nodes/edges that are made). What did I do wrong?

graph = TinkerGraph.open()
g = graph.traversal()
//The nodes
curCharge = graph.addVertex(id, 1, label, "Topic", "name","Current")
pow = graph.addVertex(id, 2, label, "Topic", "name","power")
powFromNRG = graph.addVertex(id, 3, label, "Topic", "name","powerFromNRG")
NRGFromPower = graph.addVertex(id, 4, label, "Topic", "name","NRGFromPower")
powFromVI = graph.addVertex(id,5,label,"Topic","name","powerFromIV")
powInRes = graph.addVertex(id,6,label,"Topic","name","powerInResistors")
passSign = graph.addVertex(id,7,label,"Topic","name","passiveSignPower")
ohmLaw = graph.addVertex(id,8,label,"Topic","name","ohmsLaw")

//the pre-requisite edges

//just some test queries
nL = g.V().has("name","passiveSignPower").inE("prereq").outV()
if(nL != null)
println "Hey!"

Answer Source

The Gremlin Console returns the result of each line that gets executed. In your case, the last line executed is println "Hey!" which is a void method, so it returns null.

Here's the result that you're seeing:

gremlin> if(nL != null)
......1>     println "Hey!"

You can see that Hey! was printed out to the screen, but null is the result.

If you change your script to simply return the string Hey!, you can avoid getting a null result and get the string result instead:

gremlin> if(nL != null)
......1>     "Hey!"

Checkout the Gremlin Console Tutorial for some more discussion on auto-iteration.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download