Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8724965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:56:04+00:00 2026-06-13T07:56:04+00:00

I have a problem, I am trying to connect to a MySQL database using

  • 0

I have a problem, I am trying to connect to a MySQL database using AsyncTask. I put the connection code in the button event but it gives me error,

Here is the code:

    package com.example.mysqlconnect;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;





import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

  private static final String url = "jdbc:mysql://cool/app";
  private static final String user = "user";
  private static final String pass = "password";

/** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
                Connect();
            }
        });
    }

  TextView tv = (TextView)this.findViewById(R.id.textView1);
  private class Connect extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
      String response = "";

      try {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con = DriverManager.getConnection(url, user, pass);
          // System.out.println("Database connection success"); 

          String result = "Database connection success\n";
          Statement st = con.createStatement();
          ResultSet rs = st.executeQuery("select * from users");
          ResultSetMetaData rsmd = rs.getMetaData();

          while(rs.next()) {
            result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
            result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
            result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
          }
          tv.setText(result);
      }
      catch(Exception e) {
          e.printStackTrace();
          tv.setText(e.toString());
      }
    return response;   


    }

    @Override
    protected void onPostExecute(String result) {
        tv.setText(result);
    }
  }

  public void Connect() {
    Connect task = new Connect();
      task.execute();

    }
} 

I’m new to AsyncTask, can someone help me finding the errors?

Here’s the logcat:

    10-23 19:08:41.168: W/dalvikvm(1514): threadid=1: thread exiting with uncaught exception (group=0xb40ca180)
10-23 19:08:41.178: E/AndroidRuntime(1514): FATAL EXCEPTION: main
10-23 19:08:41.178: E/AndroidRuntime(1514): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mysqlconnect/com.example.mysqlconnect.MainActivity}: java.lang.NullPointerException
10-23 19:08:41.178: E/AndroidRuntime(1514):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at android.os.Looper.loop(Looper.java:137)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at java.lang.reflect.Method.invokeNative(Native Method)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at java.lang.reflect.Method.invoke(Method.java:511)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at dalvik.system.NativeStart.main(Native Method)
10-23 19:08:41.178: E/AndroidRuntime(1514): Caused by: java.lang.NullPointerException
10-23 19:08:41.178: E/AndroidRuntime(1514):     at android.app.Activity.findViewById(Activity.java:1794)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at com.example.mysqlconnect.MainActivity.<init>(MainActivity.java:51)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at java.lang.Class.newInstanceImpl(Native Method)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at java.lang.Class.newInstance(Class.java:1319)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
10-23 19:08:41.178: E/AndroidRuntime(1514):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)

I removed the second on create and now i get this error in logcat:

10-23 19:10:29.129: E/AndroidRuntime(1565): FATAL EXCEPTION: main
10-23 19:10:29.129: E/AndroidRuntime(1565): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mysqlconnect/com.example.mysqlconnect.MainActivity}: java.lang.NullPointerException
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:56:05+00:00Added an answer on June 13, 2026 at 7:56 am

    Your error is: TextView tv = (TextView)this.findViewById(R.id.textView1); because it is declared outside of onCreate.

    So this is what you have to do, declare TextView tv; above onCreate. Then inside onCreate you add tv = (TextView) findViewById(R.id.textView1);

    And remove the duplicate of:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to connect to a mysql database using Connector/J but get the same
I have some problem in my JDBC code. I am trying to connect through
I have a problem trying to turn my python code into an executable using
I have a problem when trying to count in PHP using a button and
I've been recently trying to connect to a hosted MySQL using Java but can't
I have been trying to connect to mysql all evening using PHP. I know
I'm trying to connect to a mySQL database at http://bluesql.net , but when I
I'm trying to connect to a MySQL database on a remote server using MySQLdb
I have a problem trying to register my own Event/Listener to the event dispatcher.
I have a problem when trying to execute this update statement (below) using C#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.