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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:35:36+00:00 2026-05-25T18:35:36+00:00

I am writing an app that checks if the first value in the database

  • 0

I am writing an app that checks if the first value in the database matches a string. When I try to connect to the database, I get a NullPointerException. This happens on both getReadableDatabase() and getWritableDatabase()

Here’s my code:

public class DataManager extends SQLiteOpenHelper
{
    private final static String DB_TABLE = "Nums";
    private final static String COL_KEY      = "KEY";
    private final static String COL_VALUE= "VALUE";
    private static Context context;
    private ContentValues initialValues;
    private SQLiteDatabase db;
    private static DataManager dm;

    public static DataManager getInstance(Context _context)
    {
        if (dm==null)
        {dm=new DataManager(_context);}
        return dm;
    }

    private DataManager()
    {
        super(context, DB_TABLE, null, 1);
    }
    private DataManager(Context _context)
    {
        super(_context, DB_TABLE, null, 1);
        context=_context;
        initialValues = new ContentValues();
        if (db==null)
        {db=getWritableDatabase();}
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        StringBuilder Query = new StringBuilder();
        Query.append("CREATE TABLE IF NOT EXISTS ");
        Query.append(DB_TABLE);
        Query.append('(');
        Query.append(COL_KEY);
            Query.append(" TEXT PRIMARY KEY,");
        Query.append(COL_VALUE);
            Query.append(" TEXT);");
        Log.i(Constants.TAG,"CREATE STRING: "+Query.toString());
        db.execSQL(Query.toString());

        if(tableEmpty())
        {setDefault();}
    }

    /**
     * Populate the database with numbers 1->MAXNUM giving each a value of 0.
     */
    private void setDefault()
    {
        for (int i=1;i<=Constants.MAXNUM;i++)
        {
            setValue(String.valueOf(i),"0");
        }
    }

    /**
     * Method to get the values, ordered by frequency
     * @return Comma seperated 
     */
    public String[] getAllValues()
    {
        Cursor c=null;
        int counter=0;
        String[] val = new String[Constants.MAXNUM];

        try
        {
            c = getReadableDatabase().query(DB_TABLE, null,null, null, null, null, COL_VALUE);
            c.moveToFirst();
            //Ensure there is something in the database
            if(c.getCount()>0)
                {   // Make sure the cursor never goes over the edge
                    while(!c.isAfterLast())
                    {   // Append each value in order, seperated by a comma
                        val[counter++]=c.getString(1);
                        c.moveToNext();
                    }
                }
        }
        catch(SQLiteException e){
            Log.e(Constants.TAG,"getValue::SQLiteException::"+e.getMessage());
            e.printStackTrace();
        }
        finally {
            c.close();  // Tidy up
        }
        return val;
    }

    public String getValueByKey(String _key)
    {
        String val = "";
        try
        {
            Log.i(Constants.TAG,"key is: "+_key);
            Cursor c=getReadableDatabase().query(DB_TABLE,new String[]{COL_VALUE},COL_KEY + " LIKE ?",new String[]{_key},null,null,null);
            c.moveToFirst();
            if(c.getCount()>0)
                {   val = c.getString(0);   }
            c.close();
        }
        catch(SQLiteException e)
        {
            Log.e(Constants.TAG,"SQLiteException::"+e.getMessage());
            e.printStackTrace();
        }

        return val;
    }

    /**
     * Method checks to see if there are any records in the database
     * @return Boolean true if empty, false if not empty
     */
    private boolean tableEmpty()
    {
        boolean result = false;

        if(!(getValueByKey("1") == "0") )
        {
            result=true;
        }

        return  result;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {}  
}

My database has 2 columns. The left column has the key, and the right has the value. I am trying to get the value of the first record and return it. The error message is a generic NullPointerError so I don’t know much about the error, other than the fact that its to do with getReadableDatabase(). Can anyone see what I’m doing wrong?

Thanks.

EDIT: I’ve added the full code. Here’s the stacktrace:

09-23 15:16:27.450: ERROR/AndroidRuntime(11825): FATAL EXCEPTION: main
09-23 15:16:27.450: ERROR/AndroidRuntime(11825): java.lang.NullPointerException
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.openDatabase(MyClass.java:137)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.getFrequency(MyClass.java:204)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.tableEmpty(MyClass.java:252)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.getAllValues(MyClass.java:169)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.setupNumbers(MyClass.java:48)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.MyClass.<init>(MyClass.java:38)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.eoinzy.myApp.ButtonControl.onClick(ButtonControl.java:56)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.view.View.performClick(View.java:2501)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.view.View$PerformClick.run(View.java:9107)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.os.Handler.handleCallback(Handler.java:587)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.os.Looper.loop(Looper.java:130)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at android.app.ActivityThread.main(ActivityThread.java:3835)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at java.lang.reflect.Method.invokeNative(Native Method)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at java.lang.reflect.Method.invoke(Method.java:507)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
09-23 15:16:27.450: ERROR/AndroidRuntime(11825):     at dalvik.system.NativeStart.main(Native Method)
  • 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-25T18:35:37+00:00Added an answer on May 25, 2026 at 6:35 pm

    Try this way of Coding so that you can easily Debug you Error.

    Also Null Pointer comes for Lot of reason for Null values.

    private Cursor cursor_value;
    private DbHelper mDbHelper;
    mDbHelper = new DbHelper(this);
    mDbHelper.open();
    
     try{   
        String cursor_link_value,cursor_time_value,cursor_name_value;
        cursor_value = mDbHelper.fetch_message_values();
        Log.v("cursor_value", ""+cursor_value.getCount());
        startManagingCursor(cursor_value);
        if (cursor_value.moveToFirst()) {
        do {
            value =cursor_value.getString(cursor_value.getColumnIndex(Column_Name));
        } while (cursor_value.moveToNext());
       }
     }
     catch(Exception e){
        Log.v("Excepqqqqqqqqqqqqqqqqqqqq", ""+e);
     }
    

    For DB :

    Write your Query inside of DbHelper Class :

    public Cursor fetch_message_values() {
        // TODO Auto-generated method stub
         Cursor c=mDb.rawQuery("Your Query",null);
         Log.v("Cursor Count for Draft Table", ""+c.getCount());
        return c;
      }
    

    For Further Reference Check this link for Notepad Example for Database

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

Sidebar

Related Questions

I'm writing an app for Android that spawns a service that periodically checks the
I'm writing an iPhone app (which will be my first ios app) that has
Hello I'm writing a little Android app (Version 2.3.3). Now i get this strange
I am writing a small app that has to perform some 'sanity checks' before
I'm writing an app that will need to make use of Timer s, but
I'm writing an app that contains the following tables: (1) employee_type, (2) employee and
I'm writing an app that lets a diabetic user enter his/her blood glucose readings,
I'm writing an app that supposed to copy a bunch of files from one
I am writing an app that allows people to schedule things for certain times
I'm writing an App that basically uses 5 business entities, A, B C, D

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.