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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:40:55+00:00 2026-06-15T16:40:55+00:00

I am getting data from SQlite db and storing it in a array list

  • 0

I am getting data from SQlite db and storing it in a array list and filling data in list view but i am getting null pointer exception in logcat and application force closes, i don;t understanding what is the mistake, kindly somebody guide me that where i am making mistake.

here is my activity class code:

package testing.testapp;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;


public class Administrator extends Activity {

    List<User> usersList;
    SimpleCursorAdapter cursorAdapter;
    List<User> userList;
    TextView fullName;
    TextView cellNumber;
    ListView list;
    AdministratorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.administrator);

        list=(ListView)findViewById(R.id.fullname);

        DatabaseHandler h = new DatabaseHandler(this);
        usersList = h.getAllUsers();

        adapter = new AdministratorAdapter(this, R.layout.row, userList);
        list.setAdapter(adapter);



    }


    public class AdministratorAdapter extends ArrayAdapter<User>
    {
        Context context;
        Administrator holder;
        int layoutResourceId;
        List<User> userList;


        public AdministratorAdapter(Context context, int layoutResourceId,List<User> userList)
        {
            super(context, layoutResourceId, userList);         
            this.context=context;
            this.layoutResourceId=layoutResourceId;
            this.userList=userList;
        }


        @Override
        public View getView(int position,View convertView,ViewGroup parent)
        {
            View row=convertView;

            if(row==null)
            {
                LayoutInflater inflater=((Activity)context).getLayoutInflater();
                row=inflater.inflate(layoutResourceId, parent,false);

                holder=new Administrator();
                holder.fullName=(TextView)row.findViewById(R.id.fulname);
                holder.cellNumber=(TextView)row.findViewById(R.id.mobile);

                row.setTag(holder);
            }
            else
            {
                holder=(Administrator)row.getTag();
            }
            holder.fullName.setText(userList.get(position).getUserName());
            holder.cellNumber.setText(userList.get(position).getPassword());    
            return row;
        }

and here is my database handling class:

package testing.testapp;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 4;

    public static final String DATABASE_NAME = "usersManager";

    public static final String TABLE_USERS = "users";

    public static final String KEY_USER_ID = "_id";

    public static final String KEY_USERNAME = "username";

    public static final String KEY_PASSWORD = "password";

    public static final String KEY_ROLE = "role";

    public static final String KEY_GPA = "gpa";

    public static final String KEY_FIRST_NAME = "firstname";

    public static final String KEY_LAST_NAME = "lastname";

    public static final String KEY_GENDER = "gender";

    public static final String KEY_CELL_NUMBER = "cellnumber";




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


    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
        String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS + "("
               + KEY_USER_ID + " INTEGER PRIMARY KEY," + KEY_USERNAME + " TEXT,"
               + KEY_PASSWORD +" TEXT," + KEY_ROLE + " TEXT," + KEY_GPA + " TEXT,"
               + KEY_FIRST_NAME + " TEXT," + KEY_LAST_NAME + " TEXT," 
               + KEY_GENDER + " TEXT," + KEY_CELL_NUMBER + " TEXT" + ")";

//       String CREATE_USERS_TABLE = "create table users ("
//          + "_id integer  primary key autoincrement, "
//          + "username  string  not null, "
//          + "password string  not null, "
//          + ");";

        db.execSQL(CREATE_USERS_TABLE);
    }


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

        onCreate(db);

    }

    public void addUser(User user)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        int f =db.getVersion();

        Log.d("version", Integer.toString(f));
        ContentValues values = new ContentValues();
        values.put(KEY_USERNAME,user.getUserName());
        values.put(KEY_PASSWORD, user.getPassword());
        values.put(KEY_ROLE, user.getRole());

        db.insert(TABLE_USERS, null, values);
        db.close();

    }


    public int Login(String username,String password,String role)
    {
        try
        {
            SQLiteDatabase db = this.getWritableDatabase();
            int i = 0;
            Cursor c = null;
            String query = "SELECT * FROM " + TABLE_USERS + " WHERE " + KEY_USERNAME+ "='" + username +"'" + " AND " +KEY_PASSWORD+ "='"+password+"'" + " AND " +KEY_ROLE+ "='"+role+"'" ; 
            //String query = "SELECT * FROM users WHERE username = '"+username+"' AND password = '"+password+"'";
            //String query = "SELECT * FROM USERS";
            c = db.rawQuery(query, null);
            c.moveToFirst();
            i = c.getCount(); 
            c.close(); 
            db.close();
            return i;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    public void AddInfo(User user)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        int f =db.getVersion();

        Log.d("version", Integer.toString(f));
        ContentValues values = new ContentValues();
        values.put(KEY_FIRST_NAME,user.getFirstName());
        values.put(KEY_LAST_NAME, user.getLastName());
        values.put(KEY_GENDER, user.getGender());
        values.put(KEY_GPA,user.getGpa());
        values.put(KEY_CELL_NUMBER, user.getCellNumber());

        db.insert(TABLE_USERS, null, values);
        db.close();
    }

    public List<User> getAllUsers()
    {
        List<User> usersList = new ArrayList<User>();

        String query = "SELECT * FROM " + TABLE_USERS ;
        //+ " WHERE " + KEY_ROLE+ "=Student"

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        //return cursor;
        if(cursor.moveToFirst())
        {
            do
            {
            User user = new User();
            user.setUserId(Integer.parseInt(cursor.getString(0)));
            user.setUserName(cursor.getString(1));
            user.setPassword(cursor.getString(2));
            user.setRole(cursor.getString(3));
            user.setGpa(cursor.getString(4));
            user.setFirstName(cursor.getString(5));
            user.setLastName(cursor.getString(6));
            user.setGender(cursor.getString(7));
            user.setCellNumber(cursor.getString(8));

            usersList.add(user);
            }while(cursor.moveToNext());

        }
        db.close();
        return usersList;

    }

    public User getUserDetail(String username)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        //String query = "SELECT " + KEY_USERNAME+" , " + KEY_PASSWORD + " FROM " + TABLE_USERS + " WHERE " + KEY_USERNAME + " = '" + username + "' ";
        String query = "SELECT * FROM " + TABLE_USERS;
        Cursor cursor = db.rawQuery(query, null);

        User user = new User();

        if(cursor!=null)
        {
            user.setUserId(Integer.parseInt(cursor.getString(0)));
            user.setUserName(cursor.getString(1));
            user.setPassword(cursor.getString(2));
        }

        return user;

    }

}



    }



}

here is my logcat :

12-06 11:05:47.651: E/AndroidRuntime(634): FATAL EXCEPTION: main
12-06 11:05:47.651: E/AndroidRuntime(634): java.lang.RuntimeException: Unable to start activity ComponentInfo{testing.testapp/testing.testapp.Administrator}: java.lang.NullPointerException
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.os.Looper.loop(Looper.java:123)
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-06 11:05:47.651: E/AndroidRuntime(634):  at java.lang.reflect.Method.invokeNative(Native Method)
12-06 11:05:47.651: E/AndroidRuntime(634):  at java.lang.reflect.Method.invoke(Method.java:507)
12-06 11:05:47.651: E/AndroidRuntime(634):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-06 11:05:47.651: E/AndroidRuntime(634):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-06 11:05:47.651: E/AndroidRuntime(634):  at dalvik.system.NativeStart.main(Native Method)
12-06 11:05:47.651: E/AndroidRuntime(634): Caused by: java.lang.NullPointerException
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.widget.ListView.setAdapter(ListView.java:454)
12-06 11:05:47.651: E/AndroidRuntime(634):  at testing.testapp.Administrator.onCreate(Administrator.java:37)
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-06 11:05:47.651: E/AndroidRuntime(634):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-06 11:05:47.651: E/AndroidRuntime(634):  ... 11 more
12-06 11:10:47.981: I/Process(634): Sending signal. PID: 634 SIG: 9
  • 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-15T16:40:56+00:00Added an answer on June 15, 2026 at 4:40 pm

    You are getting NPE at line 37 in Administrator.java, which is:

    list.setAdapter(adapter);
    

    do you have a listView named fullname in the layout R.layout.administrator

    also check the usersList = h.getAllUsers(); out come.

    Log.d("size = ", usersList.size);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom list view that is getting data from server and changing
How to retrieve data from sqlite database? Getting return value zero. -(id)init{ if(self==[super init]){
I am getting data from iphone using php but when i insert post data
I am getting the data from sqlite database and trying to display it in
I have a little problem with getting all data from SQLite database and showing
I'm trying to output a list from an SQLite database and am getting this
The application interact with PHP Classes to get data from MySQL and using SQLite
Im getting data from a server by executing this code: - (void) sendGeneral:(NSString *)
how can i getting data from listbox in second form named listform while i
I am getting data from server it gives in response a NSString which hase

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.