commit 1d6c1eea4417d787a98e78d731e00b3f513d29db
Author: Leszek Koltunski <leszek@koltunski.pl>
Date:   Sat Oct 3 13:11:53 2020 +0100

    Work around a rare crash on startup

diff --git a/src/main/java/org/distorted/objects/Cubit.java b/src/main/java/org/distorted/objects/Cubit.java
index e893c1bc..ccc4d96e 100644
--- a/src/main/java/org/distorted/objects/Cubit.java
+++ b/src/main/java/org/distorted/objects/Cubit.java
@@ -110,27 +110,6 @@ class Cubit
     return ret;
     }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-  private void modifyCurrentPosition(Static4D quat)
-    {
-    float cubitCenterX = mCurrentPosition.get0();
-    float cubitCenterY = mCurrentPosition.get1();
-    float cubitCenterZ = mCurrentPosition.get2();
-
-    Static4D cubitCenter =  new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
-    Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
-
-    float rotatedX = rotatedCenter.get0();
-    float rotatedY = rotatedCenter.get1();
-    float rotatedZ = rotatedCenter.get2();
-
-    mCurrentPosition.set(rotatedX, rotatedY, rotatedZ);
-    mParent.clampPos(mCurrentPosition);
-
-    computeRotationRow();
-    }
-
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 // cast current position on axis; use mStart and mStep to compute the rotation row for each axis.
 
@@ -157,6 +136,27 @@ class Cubit
     return mOrigPosition;
     }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+  void modifyCurrentPosition(Static4D quat)
+    {
+    float cubitCenterX = mCurrentPosition.get0();
+    float cubitCenterY = mCurrentPosition.get1();
+    float cubitCenterZ = mCurrentPosition.get2();
+
+    Static4D cubitCenter =  new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
+    Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
+
+    float rotatedX = rotatedCenter.get0();
+    float rotatedY = rotatedCenter.get1();
+    float rotatedZ = rotatedCenter.get2();
+
+    mCurrentPosition.set(rotatedX, rotatedY, rotatedZ);
+    mParent.clampPos(mCurrentPosition);
+
+    computeRotationRow();
+    }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
   int computeAssociation()
@@ -187,9 +187,14 @@ class Cubit
     {
     String number = mOrigPosition.get0()+"_"+mOrigPosition.get1()+"_"+mOrigPosition.get2();
     mQuatIndex = preferences.getInt("q_"+number, 0);
-    modifyCurrentPosition(mParent.QUATS[mQuatIndex]);
 
-    return mQuatIndex;
+    if( mQuatIndex < mParent.QUATS.length )
+      {
+      modifyCurrentPosition(mParent.QUATS[mQuatIndex]);
+      return mQuatIndex;
+      }
+
+    return -1;
     }
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/main/java/org/distorted/objects/TwistyObject.java b/src/main/java/org/distorted/objects/TwistyObject.java
index b1b954c9..3c490cf6 100644
--- a/src/main/java/org/distorted/objects/TwistyObject.java
+++ b/src/main/java/org/distorted/objects/TwistyObject.java
@@ -556,6 +556,17 @@ public abstract class TwistyObject extends DistortedNode
     for(int i=0; i<NUM_CUBITS; i++)
       {
       int index = CUBITS[i].restorePreferences(preferences);
+
+      if( index<0 )
+        {
+        for(int j=0; j<NUM_CUBITS; j++)
+          {
+          CUBITS[j].modifyCurrentPosition(QUATS[0]);
+          mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),0);
+          }
+        break;
+        }
+
       mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
       }
     }
