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

  • Home
  • SEARCH
  • 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 8531535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:30:25+00:00 2026-06-11T09:30:25+00:00

I have to call, from a Service android, a method for opening connection with

  • 0

I have to call, from a Service android, a method for opening connection with internal android database (sqlite). I’m using the following code:

public class myService extends Service{

public void setDB(DB_DatabaseManager db){
    dbManager = db;
}

public DB_DatabaseManager dbManager;
private Timer timer; 

public IBinder onBind(Intent intent){
    return null;
}

public void onCreate(){
    super.onCreate();
    Log.i("my attempt","service activated");
    TimerTask task = new TimerTask(){
        public void run(){
            Log.i("my attempt","ANDROID SERVICE RUNNING!");
            dbManager.open(); // <--this statement generates error!
                            // --- in this section I start the communication with database

        }

    };
    timer = new Timer();
    timer.schedule(task, 0, 20000);
}

public void onDestroy(){
    super.onDestroy();
    timer.cancel();
    timer = null;
    Log.i("my attampt", "service stopped");
}

open() is defined as:

    SQLiteDatabase mDb=mDbHelper.getWritableDatabase();

For calling the service I use:

    myService serv = new myService();
    serv.setDB(dbManager); 
    startService(new Intent(this,myService.class));

all work fine if I delete the row dbManager.open();
however the method dbManager.open() in other parts of my coode, works good. But in this situation i obtain the following error in logcat:

   09-14 23:44:32.287: E/AndroidRuntime(1115): FATAL EXCEPTION: Timer-0
   09-14 23:44:32.287: E/AndroidRuntime(1115): java.lang.NullPointerException
   09-14 23:44:32.287: E/AndroidRuntime(1115):  at host.framework.myService$1.run(myService.java:49)
   09-14 23:44:32.287: E/AndroidRuntime(1115):  at java.util.Timer$TimerImpl.run(Timer.java:284)
  • 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-11T09:30:26+00:00Added an answer on June 11, 2026 at 9:30 am

    Do not try to do this…

    myService serv = new myService();
    

    Similar to the Activity class, Service is a “special case” class and you should never try to create an instance using new.

    A Service is created using either bindService(...) or startService(...) and it is the responsibility of the OS to create/instantiate it.

    In your case it seems you need your Service to be able to access a database “helper” class and there are a number of ways you could do this.

    One way is to extend the Application class and have it create an instance of the db helper and hold a static reference to it. In this way in the Service, you can simply use…

    MyApp.mDbHelper.open();
    

    Alternatively use the singleton model for your db helper which would allow you to do something like the following in the Service…

    DB_DatabaseManager.getInstance().open();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using android 2.3.3, I have a background Service which has a socket connection. There's
I'm trying to call a RESTful web service from an Android application using the
Right now i have used SOAP method to consume web service from android eclipse.
We have a Java web service that we call from our application. When we
I am trying to call a web service from asp.net 3.5 application. I have
I have a JSON Object that is returned from a web service call that
I have the following function call from a thread: Thread Move = new Thread(){
I am trying to retrieve information from a web service call. The following is
Calling a .net SOAP1.1 web service from android using ksoap2 lib I met a
Currently i am consuming web service from android by the method SOAP.Here i need

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.