I tried setting both nodes and links at the same time this way:
var force = d3.layout.force() .size([w, h]) .nodes(nodes) .links(connections)
In addition to the answers mentioning the null in the source/target of links, the reason for this could be the assignment of an out-of-range source/target. E.g. you have 10 nodes and you assign the target to be the 11-th indexed node.