Q:

Interactive Brokers API placingOrder in Wrapper function causes disconnect

I am using the Java API and I've made a simple Swing GUI with a push button. When I push the button it executes: 
anInstance.connection.reqRealTimeBars(id, contract, 5,"TRADES",false); 
// note that connection = new EClientSocket(Wrapper) which is assigned in constructor`

which causes my overrided Wrapper function realtimeBar to execute.
I use Joda Time and getSecondOfMinute to convert the Time in the wrapper function into what second it is. If it is 55 seconds, I want to place a trade. (I.e. I want my program to place a trade every minute at the 55 second mark).
However whenever I place the trade, there is something which disconnects my connection with the API.  
I think it might have to do with synchronization and perhaps some deadlock? Anyways, the error closes my connection to the API which is not what I want since the point of my program is to trade constantly. 
Does anyone know why this issue is coming up? More importantly, how do I get around this? 
Sample Code
someWrapper.java which overrides EWrapper: 
public class someWrapper implements EWrapper {
    private IBProgram anInstance;

public void setReference(IBProgram anInstance){
    this.anInstance = anInstance    
 }

// overide and implement various EWrapper methods. 
// The Ewrapper method below is a sample of placing an Order 
// causing problems in an EWrapper method

public void updatePortfolio(Contract contract, int position, double marketPrice, double marketValue,
        double averageCost, double unrealizedPNL, double realizedPNL, String accountName){

Order myFakeOrder = new Order();
Contract myFakeContract = new Contract();
myFakeContract.m_symbol = "GOOG";
myFakeContract.m_exchange = "SMART";
myFakeContract.m_secType = "STK";
myFakeContract.m_currency = "USD";
myFakeOrder.m_action = "BUY";
myFakeOrder.m_totalQuantity = 1;
myFakeOrder.m_orderType = "LMT";
myFakeOrder.m_lmtPrice = 1;
myFakeOrder.m_tif = "DAY";
anInstance.connection.placeOrder(2124124,myFakeContract,myFakeOrder);
}
}

IBProgram.java
public class IBProgram {
 someWrapper wrapper;
 public EClientSocket connection;

 public IBProgram() {

 this.wrapper = new someWrapper();
 this.connection = new EClientSocket(wrapper);
 }
}

GUI
public class GUI extends javax.swing.JFrame{
     private IBProgram IBProgramInstance;

public GUI(){
     initComponents();
}

private void initComponents() {
     myButton = new javax.swing.JButton();
     myButton.setText("Start Trading");
     myButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            myButtonActionPerformed(evt);
        }
    });
}

public void setReference(IBProgram Instance){
      this.IBProgramInstance = Instance;
}

private void myButtonActionPerformed (java.awt.event.ActionEvent evt) {     
          IBProgramInstance.connection.reqAccountUpdates(true,"myaccountid");
}

public static void main(String args[]){
       IBProgram IBPInstance = new IBProgram();
       IBProgramInstance.wrapper.setReference(IBPInstance);
       IBProgram.connection.eConnect("127.0.0.1",7496,12);
       GUI guiInstance = new GUI();
       guiInstance.setReference(IBPInstance);
       guiInstance.setVisible(true);

}

Adding Error Message
Exception in thread "EReader" java.lang.Error: Interrupted attempt to aquire write lock
at javax.swing.text.AbstractDocument.writeLock(AbstractDocument.java:1349)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:659)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1718)
at IBconnect.IBTradeGui.setConnectionText(IBTradeGui.java:698)
at IBconnect.someWrapper.connectionClosed(someWrapper.java:63)
at com.ib.client.EClientSocket.close(EClientSocket.java:2004)
at com.ib.client.EReader.run(EReader.java:78)

A:

Since most of the Swing objects are not thread safe, you have to create the GUI instance using this code in the main() method:
SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        GUI guiInstance = new GUI();
    }
});

See this link (and the following page) for further information about Swing thread safety and the Event Dispatch Thread (AKA EDT).

Unrelated to the concrete problem, pay more attention to use Uppercase names for classes, and camelCase names for class instances. Avoid public properties in classes, because they violate the encapsulation principle. Catch all exceptions in the class implementing EWrapper, otherwise IB will close the connection. And more importantly, pay a lot of attention when you insert orders in the updatePortfolio() method or any other method overriding the methods of EWrapper interface, because they are triggered by IB socket and you never know when they are invoked.

