Repository: neo4j/neo4j-java-driver
Issue: NullPointerException at org.neo4j.driver.internal.InternalSession$2.run when an invalid Cypher query is executed

With 1.0.0-M05, this exception is thrown

```
Exception in thread "main" java.lang.NullPointerException
    at org.neo4j.driver.internal.InternalSession$2.run(InternalSession.java:140)
    at org.neo4j.driver.internal.pool.PooledConnection.onDelegateException(PooledConnection.java:197)
    at org.neo4j.driver.internal.pool.PooledConnection.receiveOne(PooledConnection.java:146)
    at org.neo4j.driver.internal.InternalStatementResult.tryFetching(InternalStatementResult.java:344)
    at org.neo4j.driver.internal.InternalStatementResult.hasNext(InternalStatementResult.java:190)
```

when you attempt to execute an invalid Cypher query using session.run. This only happens when a previous transaction has been carried out in the same session.

```
Session session = driver.session();

//Must have a transaction first
Transaction tx = session.beginTransaction();
StatementResult result = tx.run("CREATE (n) RETURN n");
if (result.hasNext()) {
    result.next();
}
result.consume();
tx.success();
tx.close();

//Now the bad query
StatementResult badResult = session.run("CREAT (n) RETURN n");
if (badResult.hasNext()) {
    badResult.next();
}
```

Without the previous `tx`, `badResult` is executed and produces
`Exception in thread "main" org.neo4j.driver.v1.exceptions.ClientException: Invalid input ' ': expected 'e/E' (line 1, column 6 (offset: 5))` as expected.

If `badResult` was a valid query `badResult = session.run("CREATE (n) RETURN n");` then both execute fine.


Changed files:
driver/src/main/java/org/neo4j/driver/internal/InternalSession.java, driver/src/test/java/org/neo4j/driver/v1/integration/TransactionIT.java

diff --git a/driver/src/main/java/org/neo4j/driver/internal/InternalSession.java b/driver/src/main/java/org/neo4j/driver/internal/InternalSession.java
index bd259fe3..7f71f6b3 100644
--- a/driver/src/main/java/org/neo4j/driver/internal/InternalSession.java
+++ b/driver/src/main/java/org/neo4j/driver/internal/InternalSession.java
@@ -137,9 +137,13 @@ public class InternalSession implements Session
             @Override
             public void run()
             {
-                currentTransaction.markAsRolledBack();
-                currentTransaction = null;
-                connection.onError( null );
+                //must check if transaction has been closed
+                if (currentTransaction != null)
+                {
+                    currentTransaction.markAsRolledBack();
+                    currentTransaction = null;
+                    connection.onError( null );
+                }
             }
         });
         return currentTransaction;
diff --git a/driver/src/test/java/org/neo4j/driver/v1/integration/TransactionIT.java b/driver/src/test/java/org/neo4j/driver/v1/integration/TransactionIT.java
index 34346f1f..511efb61 100644
--- a/driver/src/test/java/org/neo4j/driver/v1/integration/TransactionIT.java
+++ b/driver/src/test/java/org/neo4j/driver/v1/integration/TransactionIT.java
@@ -54,7 +54,7 @@ public class TransactionIT
         // Then the outcome of both statements should be visible
         StatementResult result = session.run( "MATCH (n) RETURN count(n)" );
         long nodes = result.single().get( "count(n)" ).asLong();
-        assertThat( nodes, equalTo( 2l ) );
+        assertThat( nodes, equalTo( 2L ) );
     }
 
     @Test
@@ -70,7 +70,7 @@ public class TransactionIT
         // Then there should be no visible effect of the transaction
         StatementResult cursor = session.run( "MATCH (n) RETURN count(n)" );
         long nodes = cursor.single().get( "count(n)" ).asLong();
-        assertThat( nodes, equalTo( 0l ) );
+        assertThat( nodes, equalTo( 0L ) );
     }
 
     @Test
@@ -146,4 +146,22 @@ public class TransactionIT
 
     }
 
+    //See GH #146
+    @Test
+    public void shouldHandleFailureAfterClosingTransaction()
+    {
+        // GIVEN a successful query in a transaction
+        Transaction tx = session.beginTransaction();
+        StatementResult result = tx.run("CREATE (n) RETURN n");
+        result.consume();
+        tx.success();
+        tx.close();
+
+        // EXPECT
+        exception.expect( ClientException.class );
+
+        //WHEN running a malformed query in the original session
+        session.run("CREAT (n) RETURN n").consume();
+    }
+
 }