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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:22:06+00:00 2026-05-23T22:22:06+00:00

I am entering some data into this database and then trying to display that

  • 0

I am entering some data into this database and then trying to display that data, but I get a Null Pointer Exception at getReadableDatabase(). The main activity from where I enter data is as follows:

public class TabListData extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button b = (Button) findViewById(R.id.enter);
        b.setOnClickListener(this);
        Button b1 = (Button) findViewById(R.id.employeeslist);
        b1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        EditText name = (EditText) findViewById(R.id.name);
        EditText mobile = (EditText) findViewById(R.id.mobile);

        String nam = name.getText().toString();
        String mob = mobile.getText().toString();

        Button b = (Button) v;
        Button b1 = (Button) v;
         Database d = new Database(getBaseContext());

         switch (b.getId()) {
         case R.id.enter:
             d.input(nam, mob);
         case R.id.employeeslist:
            /* ArrayList<EmployeesResult> results = d.selectEmployees();
                Toast t = Toast.makeText(getBaseContext(), "# records: "
                        + results.size(), 1000);
                t.show();*/
             Intent i = new Intent(this, EmployeesList.class);
            // i.putExtra("Name", "Name");
             startActivity(i);
         }
        }
    }

This is my Helper Class

public class BaseHelper extends SQLiteOpenHelper {

    public BaseHelper(Context context) {
        super(context, "Employees.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String createtable = "create table Employee " + "(Name TEXT, " + "Mobile TEXT)";
        db.execSQL(createtable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub  
    }
}

This is my Database class. I’m using it for input and data retrieval. It was working fine for input when the selectEmployees() method was not there but now it is showing Null Pointer Exception.

public class Database {

    private BaseHelper helper;
    SQLiteDatabase db;
    public Database(Context c){
        helper = new BaseHelper(c); 
    }

    public void input(String Name, String Mobile){
         db = helper.getWritableDatabase();
        try{
        ContentValues values = new ContentValues();
        values.put("Mobile", Mobile);
        values.put("Name", Name);
        db.insert("Employee", "", values);
        Log.e("ENTERED", "ENTERED   "+values);
        }finally {
            if (db != null)
                db.close();
        }
    }
    public ArrayList<EmployeesResult> selectEmployees() {

         db = helper.getReadableDatabase();
        try{
        ArrayList<EmployeesResult> results = new ArrayList<EmployeesResult>();
         Cursor c = db.rawQuery("select * from Employee", null);

         if (c.getCount() > 0) {
                c.moveToFirst();
                do {
                    results.add(new EmployeesResult(
                            c.getString(c.getColumnIndex("Name"))));
                            //c.getInt(c.getColumnIndex("Mobile"))));
                }while(c.moveToNext());
         }
        return results;
    }finally {
        if (db != null)
            db.close();
    }
    }
}

The EmployeeResult class:

public class EmployeesResult {
    public EmployeesResult(String Name){
        name = Name;
        //mobile = Mobile;  
    }
    public String name;
    public int mobile;
}

This is Logcat

07-15 23:44:11.008: ERROR/AndroidRuntime(292): FATAL EXCEPTION: main
07-15 23:44:11.008: ERROR/AndroidRuntime(292): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.TabListData/com.TabListData.EmployeesList}: java.lang.NullPointerException
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.os.Looper.loop(Looper.java:123)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at java.lang.reflect.Method.invoke(Method.java:521)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at dalvik.system.NativeStart.main(Native Method)
07-15 23:44:11.008: ERROR/AndroidRuntime(292): Caused by: java.lang.NullPointerException
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at com.TabListData.Database.selectEmployees(Database.java:35)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at com.TabListData.EmployeesList.onCreate(EmployeesList.java:30)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-15 23:44:11.008: ERROR/AndroidRuntime(292):     ... 11 more
  • 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-05-23T22:22:06+00:00Added an answer on May 23, 2026 at 10:22 pm

    My problem is solved I found where the problem exactly the problem was in ListActivity and EmployeeResult class

    For ListActivity take a XML layout with only text to display text in ListView
    Like this

    <?xml version="1.0" encoding="UTF-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    android:padding="10dp"    
    android:textSize="16sp" >
    </TextView>
    

    and in ListActivity you just need to set a ListAdapter

    like this in onCreate() method

    List<String> data = dh.SelectStudent();
    setListAdapter(new ArrayAdapter<String>(ListofStudents.this, R.layout.text, data));
    

    and insted of EmployeesResult class take String in ArrayAdapter and List

    the method in DataBase class should be

    public ArrayList<String> SelectStudent() {
        SQLiteDatabase db = _dbHelper.getReadableDatabase();
        try {
            ArrayList<String> results = new ArrayList<String>();
            Cursor c = db.rawQuery("select * from Student", null);
            if (c.getCount() > 0) {
                c.moveToFirst();
                do {
                    results.add(c.getString(1));
                   // results.add(c.getString(4));
    
                } while (c.moveToNext());
            }
            return results;
        } finally {
            if (db != null)
                db.close();
        }
    }
    

    I have passed String in List and ArrayAdepter, If u want more then one string like Name and Mobile Number or a combination of String and Image in the item in List View in that case u need to make a separate class in which u have images and string and you need to pass that class instead of String in List and ArrayAdepter.

    THANX

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I am entering validation code into my model of multi-language publication database. The
so i use mysql_real_escape_string to sanitize some user input before entering it into the
I have a database that tracks employee’s data for the current year and previous
again. I'm having more issues with my script entering info into my database. The
When entering a question, stackoverflow presents you with a list of questions that it
I have an application for entering in serial numbers to a database. A serial
Using MySQL Administrator GUI tool I have exported some data tables retrieved from an
I have a small database that I need help designing. I have a VB.NET
I made some CMS in PHP which manipulates with data from MySQL. In my
EDIT It appears the user has to enter some data for his profile, otherwise

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.