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 8115373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:27:36+00:00 2026-06-06T03:27:36+00:00

Main Activity: private Button btnSubmit; private DataSource mDataSource; private Context mContext; @Override public void

  • 0

Main Activity:

private Button btnSubmit;
private DataSource mDataSource;
private Context mContext;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final EditText first_name = (EditText) findViewById(R.id.first_name);
    final Spinner relation = (Spinner) findViewById(R.id.relation);
    final EditText address = (EditText) findViewById(R.id.address);
    Button  mSubmitButton = (Button) findViewById(R.id.btnSubmit);
    relation.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    mSubmitButton.setOnClickListener(new View.OnClickListener() {

           @Override
           public void onClick(View v) {
                 String fName = first_name.getText().toString();
                 String re_lation = (String) relation.getSelectedItem(); 
                 String add_ress = address.getText().toString();
                 if ((fName.length() <=0) || (re_lation.length()<=0) || (add_ress.length()<=0)) {
                        Toast.makeText(MyAndroidAppActivity.this, "please fill the data", Toast.LENGTH_SHORT).show();
                 } else{
                        Log.i("name",fName);
                        Log.i("rel",re_lation);
                        Log.i("e",add_ress);
                        mDataSource.addUser(fName, re_lation, add_ress);

                 }
           }
     });
}

DataSource.java

private SQLiteDatabase mSQLiteDatabase;
private MySQLiteHelper mSQLiteHelper;
private String[] mAllColumns = {
      MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_FIRST_NAME, MySQLiteHelper.COLUMN_RELATION, MySQLiteHelper.COLUMN_ADDRESS
};

public DataSource (Context context){
      mSQLiteHelper = new MySQLiteHelper(context);
}

public void open() throws SQLiteException {
      mSQLiteDatabase = mSQLiteHelper.getWritableDatabase();
}

public void close() {
      mSQLiteHelper.close();
}

public void addUser(String first_name, String relation, String address){
      ContentValues values = new ContentValues();
      Log.i("name",first_name);
      values.put(MySQLiteHelper.COLUMN_FIRST_NAME, first_name);
      values.put(MySQLiteHelper.COLUMN_RELATION, relation);
      values.put(MySQLiteHelper.COLUMN_ADDRESS, address);
      mSQLiteDatabase.insert(MySQLiteHelper.TABLE_USERS, null, values);
      //Cursor cursor = mSQLiteDatabase.query(MySQLiteHelper.TABLE_USERS, allcolumns, selection, selectionArgs, groupBy, having, orderBy)
}

MySQLiteHelper.java

public class MySQLiteHelper extends SQLiteOpenHelper {
        public static final String TABLE_USERS = "users";
        public static final String COLUMN_ID = "_id";
        public static final String COLUMN_FIRST_NAME = "first_name";
        public static final String COLUMN_RELATION = "relation";
        public static final String COLUMN_ADDRESS = "address";



        private static final String DATABASE_NAME = "users.db";
        private static final int DATABASE_VERSION = 1;

        // Database creation sql statement
        private static final String DATABASE_CREATE = "create table "
                + TABLE_USERS + "( " + COLUMN_ID
                + " integer primary key autoincrement, " + COLUMN_FIRST_NAME
                + " text not null," + COLUMN_RELATION + " text not null," +
                COLUMN_ADDRESS + " text not null);";



        public MySQLiteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS "+TABLE_USERS);
            onCreate(db);

        }
}

Error Logcat:

06-20 11:26:02.043: E/AndroidRuntime(342): FATAL EXCEPTION: main
06-20 11:26:02.043: E/AndroidRuntime(342): java.lang.NullPointerException
06-20 11:26:02.043: E/AndroidRuntime(342):  at com.mkyong.android.MyAndroidAppActivity$1.onClick(MyAndroidAppActivity.java:62)
06-20 11:26:02.043: E/AndroidRuntime(342):  at android.view.View.performClick(View.java:2485)
06-20 11:26:02.043: E/AndroidRuntime(342):  at android.view.View$PerformClick.run(View.java:9080)
06-20 11:26:02.043: E/AndroidRuntime(342):  at android.os.Handler.handleCallback(Handler.java:587)
06-20 11:26:02.043: E/AndroidRuntime(342):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 11:26:02.043: E/AndroidRuntime(342):  at android.os.Looper.loop(Looper.java:123)
06-20 11:26:02.043: E/AndroidRuntime(342):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-20 11:26:02.043: E/AndroidRuntime(342):  at java.lang.reflect.Method.invokeNative(Native Method)
06-20 11:26:02.043: E/AndroidRuntime(342):  at java.lang.reflect.Method.invoke(Method.java:507)
06-20 11:26:02.043: E/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-20 11:26:02.043: E/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)

Here i got the null pointer exeception while inserting the spinner and other text values in sqlite database,in the error line mDataSource.addUser(fName, re_lation, add_ress);

i got the three values.but the database is not created here.i point out the error line and while submiting the values into database,it throws the null pointer exception.what is the error in my program.anybody can tell me?

thanks in advance

  • 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-06T03:27:38+00:00Added an answer on June 6, 2026 at 3:27 am

    mDataSource is not yet initialized

    you should initialize it on your onCreate() callback

    protected void onCreate(Bundle savedInstanceState)
    {
    
    setContentView(R.layout.layout);
    mDataSource = new DataSource(getBaseContext());
    
    // ... The Rest of your code
    }
    

    and in your DataSource’s Constructor call also the open() method

    public DataSource (Context context){
    
    mSQLiteHelper = new MySQLiteHelper(context);
    open();
    }
    
    public void open() throws SQLiteException {
            mSQLiteDatabase = mSQLiteHelper.getWritableDatabase();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package com.android.SMStest; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.Button;
package com.parseador.prueba; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class main extends
I am using this simple code public class Main extends Activity { private ProgressDialog
MakeMissedCallActivity.java: package com.android.main; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import
I've got a custom BaseAdapter and an add button in the main activity. The
Here's my current code: public class MallMapActivity extends Activity { private final static String
The main activity includes some variables with set values. I created a sub-activity with
I called my main activity Main, and Eclipse created Main.java and res/layout/main.xml for the
i have main activity in which i have Four menus. and i have one
I'm having difficulties handling multiple instances of my root (main) activity for my application.

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.