I've already found a solution to this, so I'm just posting for the benefit of others.
I was going through all the variables of a workflow to see if any were unused. I clicked delete for each variable and just left the ones that the workflow told me were in use.
When I went to publish the workflow, I got the following error: "soap: ServerServer was unable to process request. ---> Object reference not set to an instance of an object."
After a painful process of deleting each variable in turn, then publishing, I found three that would generate this error when deleted. All were collection variables. I looked in the workflow's XML and found that these three were actually in use, even though I was allowed to delete them.
I eventually tracked them down to a Query List action, where they were used to store outputs from the query.
Clearly there is a bit of a bug that allows you to delete variables actually used for storing Query List outputs. Look out for this if you get the "Object reference not set to an instance of an object" error on publishing.
I'm using SharePoint 2013 on-premise, using Nintex Workflow Version: 188.8.131.52.