diff --git a/src/quick/items/qquickspritesequence.cpp b/src/quick/items/qquickspritesequence.cpp
index 0be6486bf06809b6adb6646bc0e6e2f6a869bdda..a9a823c2cedebb8b12fdc993c4701edec4805a5c 100644
--- a/src/quick/items/qquickspritesequence.cpp
+++ b/src/quick/items/qquickspritesequence.cpp
@@ -243,7 +243,8 @@ void QQuickSpriteSequence::setGoalSprite(const QString &sprite)
     if (m_goalState != sprite){
         m_goalState = sprite;
         emit goalSpriteChanged(sprite);
-        m_spriteEngine->setGoal(m_spriteEngine->stateIndex(sprite));
+        if (m_spriteEngine)
+            m_spriteEngine->setGoal(m_spriteEngine->stateIndex(sprite));
     }
 }
 
@@ -257,10 +258,13 @@ void QQuickSpriteSequence::createEngine()
     //TODO: delay until component complete
     if (m_spriteEngine)
         delete m_spriteEngine;
-    if (m_sprites.count())
+    if (m_sprites.count()) {
         m_spriteEngine = new QQuickSpriteEngine(m_sprites, this);
-    else
+        if (!m_goalState.isEmpty())
+            m_spriteEngine->setGoal(m_spriteEngine->stateIndex(m_goalState));
+    } else {
         m_spriteEngine = 0;
+    }
     reset();
 }
 
diff --git a/tests/auto/quick/qquickspritesequence/data/spriteaftergoal.qml b/tests/auto/quick/qquickspritesequence/data/spriteaftergoal.qml
new file mode 100644
index 0000000000000000000000000000000000000000..189458ad90a6ea2e89fb942369ab0d0ae488df3a
--- /dev/null
+++ b/tests/auto/quick/qquickspritesequence/data/spriteaftergoal.qml
@@ -0,0 +1,60 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL21$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+// QTBUG-40595
+import QtQuick 2.0
+
+Rectangle {
+    width: 320
+    height: 320
+
+    SpriteSequence
+    {
+        anchors.centerIn: parent
+
+        width: 300
+        height: 300
+
+        goalSprite: "foobar"
+
+        sprites:
+        [
+            Sprite
+            {
+                name: "foobar"
+                source: "squarefacesprite.png"
+            }
+        ]
+    }
+}
+
diff --git a/tests/auto/quick/qquickspritesequence/data/spritebeforegoal.qml b/tests/auto/quick/qquickspritesequence/data/spritebeforegoal.qml
new file mode 100644
index 0000000000000000000000000000000000000000..23326fb5cc2b678b1ce0cb50ba27c440cc6e5bb6
--- /dev/null
+++ b/tests/auto/quick/qquickspritesequence/data/spritebeforegoal.qml
@@ -0,0 +1,60 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL21$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+// QTBUG-40595
+import QtQuick 2.0
+
+Rectangle {
+    width: 320
+    height: 320
+
+    SpriteSequence
+    {
+        anchors.centerIn: parent
+
+        width: 300
+        height: 300
+
+        sprites:
+        [
+            Sprite
+            {
+                name: "foobar"
+                source: "squarefacesprite.png"
+            }
+        ]
+
+        goalSprite: "foobar"
+    }
+}
+
diff --git a/tests/auto/quick/qquickspritesequence/tst_qquickspritesequence.cpp b/tests/auto/quick/qquickspritesequence/tst_qquickspritesequence.cpp
index 6cfdc9036416c77fcce784c5852b4cc96c736f85..26ddff1009782aa7d60766463136f46505fcb35f 100644
--- a/tests/auto/quick/qquickspritesequence/tst_qquickspritesequence.cpp
+++ b/tests/auto/quick/qquickspritesequence/tst_qquickspritesequence.cpp
@@ -46,6 +46,8 @@ private slots:
     void test_framerateAdvance();//Separate codepath for QQuickSpriteEngine
     void test_huge();//Separate codepath for QQuickSpriteEngine
     void test_jumpToCrash();
+    void test_spriteBeforeGoal();
+    void test_spriteAfterGoal();
 };
 
 void tst_qquickspritesequence::test_properties()
@@ -119,6 +121,30 @@ void tst_qquickspritesequence::test_jumpToCrash()
     delete window;
 }
 
+void tst_qquickspritesequence::test_spriteBeforeGoal()
+{
+    QQuickView *window = new QQuickView(0);
+
+    window->setSource(testFileUrl("spritebeforegoal.qml"));
+    window->show();
+    QVERIFY(QTest::qWaitForWindowExposed(window));
+    //verify: Don't crash
+
+    delete window;
+}
+
+void tst_qquickspritesequence::test_spriteAfterGoal()
+{
+    QQuickView *window = new QQuickView(0);
+
+    window->setSource(testFileUrl("spriteaftergoal.qml"));
+    window->show();
+    QVERIFY(QTest::qWaitForWindowExposed(window));
+    //verify: Don't crash
+
+    delete window;
+}
+
 QTEST_MAIN(tst_qquickspritesequence)
 
 #include "tst_qquickspritesequence.moc"