Multiple thread error


I have an app where I need to use AsyncTask but I am having the issue that the main thread is going too far and ends up crashing my app because my Connect1 thread has not retrieved the information yet needed to continue. I was wondering how could I have the thread wait till the AsyncTask thread dies out and then the main thread can continue.


private void gNameOriginTag() {

    TextView tV;

    Connect1 connect1 = new Connect1();

    // Set the long name for the chosen.
    tV = (TextView) view.findViewById(;
    tV.setText(columns.get(4));      //<<<< Error is here.


Connect1 AsyncTask:

private class Connect1 extends AsyncTask<String, Void, String>{

    protected String doInBackground(String... params) {

        Connection conn = null;
        Statement stmt = null;

        try {
            // STEP 2: Register JDBC driver

            // STEP 3: Open a connection
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // STEP 4: Execute a query
            stmt = conn.createStatement();

            // STEP 5a: Extract data from result set
            ResultSet rs = stmt.executeQuery("SELECT * FROM gs WHERE name ='"
                    + gSelected + "'");
            ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
            int x = 1;
            while ( {
                while (x < rsmd.getColumnCount()) {

                    // Retrieve Strings & Add it to the ArrayList.


        } catch (SQLException se) {
            // Handle errors for JDBC
        } catch (Exception e) {
            // Handle errors for Class.forName
        } finally {
            // finally block used to close resources
            try {
                if (stmt != null)
            } catch (SQLException se2) {
            }// nothing we can do
            try {
                if (conn != null)
            } catch (SQLException se) {
            }// end finally try
        }// end try

        return "";

The reason why I am doing this is because android 2.3.3 doesn't run mysql on the main thread from what I learned so I am trying to learn AsyncTask, so what I am trying to accomplish in "Code:" is somehow make the main thread wait/join (I also read up that pausing the main thread is bad so I am even more lost on what to do) up for the Connect1 thread to finish so that:


can be retrieved so my app does not crash.

Define a method that is called by the AsyncTask when it is finished and that is then run on the UIThread again.

MainThread: calls AsyncTask

AsyncTask: does the work and calls doWhenDoneWithBackgroundWork

doWhenDoneWithBackgroundWork: does some work on MainThread again