title: How to fix java.lang.IllegalStateException
body: <p>I'm currently working on my A Level Computer Science controlled assessment, making a Scout Manager App in Android Studio, using Java and XML - none of which I have used before.  I'm now working on a n event creator. </p>
  
  <p>I'm trying to create a record, which will show which transport has been selected for which event, however I have run into a problem - the LogCat is giving me the following error:</p>
  
  <pre><code>04-02 15:25:42.663 18482-18482/com.example.atomi.scoutmanagerprototype E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.atomi.scoutmanagerprototype, PID: 18482
      java.lang.IllegalStateException: Could not execute method for android:onClick
          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
          at android.view.View.performClick(View.java:4761)
          at android.view.View$PerformClick.run(View.java:19767)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5310)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
       Caused by: java.lang.reflect.InvocationTargetException
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
          at android.view.View.performClick(View.java:4761) 
          at android.view.View$PerformClick.run(View.java:19767) 
          at android.os.Handler.handleCallback(Handler.java:739) 
          at android.os.Handler.dispatchMessage(Handler.java:95) 
          at android.os.Looper.loop(Looper.java:135) 
          at android.app.ActivityThread.main(ActivityThread.java:5310) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at java.lang.reflect.Method.invoke(Method.java:372) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 
       Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.atomi.scoutmanagerprototype/databases/ScoutManager.db
          at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
          at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1158)
          at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
          at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
          at com.example.samogonovsky.scoutmanager.databaseHandler.getRandomTransport(databaseHandler.java:649)
          at com.example.samogonovsky.scoutmanager.frmCreateEvent2.saveAndExit(frmCreateEvent2.java:528)
  </code></pre>
  
  <p>However, I have not closed the cursor in either of these two places. </p>
  
  <p>Here is the code in the "databaseHandler" class:</p>
  
  <pre><code>//Select a random mode of transport
      public Cursor getRandomTransport()
      {
          Cursor cursor=db.query(TABLE_TRANSPORT, null, null, null, null, null, "RANDOM()limit 1");
          if (cursor!=null&amp;&amp;cursor.moveToFirst())
          {
              cursor.moveToFirst();
          }
          else
          {
              cursor=null;
          }
          return cursor;
      }
  </code></pre>
  
  <p>And here is the code in the "frmAddEvent2" class:</p>
  
  <pre><code>double TotalTransportCost;
          if (providingtransport==true)
          {
              int peopletotransport=noOfPeople;
              TotalTransportCost = 0;
              do
              {
  
                  int eventtransportid=createEventTransportId();
  
                  Cursor ModeOfTransport = DatabaseHandler.getRandomTransport();
  
                  if (ModeOfTransport.moveToFirst()) {
  
                      int transportid=ModeOfTransport.getInt(ModeOfTransport.getColumnIndex(
                              DatabaseHandler.COLUMN_TRID));
  
                      double PriceForHiring = ModeOfTransport.getDouble(ModeOfTransport.getColumnIndex(
                              DatabaseHandler.COLUMN_HCOST));
  
                      double PricePerMile = ModeOfTransport.getDouble(ModeOfTransport.getColumnIndex(
                              DatabaseHandler.COLUMN_MCOST));
  
                      int NoOfSeats = ModeOfTransport.getInt(ModeOfTransport.getColumnIndex(
                              DatabaseHandler.COLUMN_SEATNO));
  
                      double ModeCost = ((PricePerMile*distancefrombase)+PriceForHiring);
  
                      eventTransportDetails eventtransport = new eventTransportDetails(eventtransportid,
                              id, transportid);
  
                      DatabaseHandler.createEventTransport(eventtransport);
  
                      BigDecimal bigdecimalmodecost=new BigDecimal(ModeCost);
                      BigDecimal bigdecimalpropermodecost=((bigdecimalmodecost.setScale(2,bigdecimalmodecost.ROUND_DOWN)));
                      double propermodecost=bigdecimalpropermodecost.doubleValue();
                      TotalTransportCost = TotalTransportCost + propermodecost;
                      peopletotransport = (peopletotransport-NoOfSeats);
                      ModeOfTransport.close();
                  }
                  else
                  {
                      Toast.makeText(
                              context,
                              "Error - you have not added any lunches, therefore food " +
                                      "cannot be added.",
                              Toast.LENGTH_SHORT
                      ).show();
                      return;
                  }
              }
              while (peopletotransport&gt;0);
          }
          else
          {
              TotalTransportCost=0;
          }
  </code></pre>
  
  <p>I was wondering if anybody could point me in the right direction?</p>
  
  <p>Also, feel free to suggest any improvements to my question - as stated, a lot of this is new to me, so I may have left out some pretty important material.</p>