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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:54:07+00:00 2026-06-12T19:54:07+00:00

I’m trying to insert data into a database when there is no network, but

  • 0

I’m trying to insert data into a database when there is no network, but it gives me a NullPointerException. When it goes online, it gives some others and crashes my app.

LogCat Error

    10-13 01:27:07.614: E/Service(13862): Service Created.. 
    10-13 01:27:08.054: E/Service Started(13862): Successful
    10-13 01:27:08.094: E/Insertion(13862): java.lang.NullPointerException
    10-13 01:27:09.134: E/Service Network(13862): Network is offline
    10-13 01:27:14.174: E/Service Network(13862): Network is offline
    10-13 01:28:29.154: E/Service Network(13862): Network is offline
    10-13 01:28:35.924: E/Service Network(13862): Network is offline
    10-13 01:28:39.174: E/Service Network(13862): Network is online
    10-13 01:28:39.204: E/AndroidRuntime(13862): FATAL EXCEPTION: Timer-0
    10-13 01:28:39.204: E/AndroidRuntime(13862): java.lang.NullPointerException
    10-13 01:28:39.204: E/AndroidRuntime(13862):    at com.remote.synchronizer.haris.SQLiteAdapter.getAllContacts(SQLiteAdapter.java:94)
    10-13 01:28:39.204: E/AndroidRuntime(13862):    at com.remote.synchronizer.haris.OfflineDataService.DataSender(OfflineDataService.java:96)
    10-13 01:28:39.204: E/AndroidRuntime(13862):    at com.remote.synchronizer.haris.OfflineDataService.access$0(OfflineDataService.java:90)
    10-13 01:28:39.204: E/AndroidRuntime(13862):    at com.remote.synchronizer.haris.OfflineDataService$1.run(OfflineDataService.java:67)
    10-13 01:28:39.204: E/AndroidRuntime(13862):    at java.util.Timer$TimerImpl.run(Timer.java:284)

OfflineDataService.java

package com.remote.synchronizer.haris;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.NameValuePair;

import android.app.IntentService;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

public class OfflineDataService extends IntentService {

    boolean wifi,edge;
    private Timer timer= new Timer();
    SQLiteDatabase db;
    String un,shop,city,date,order;
    private SQLiteAdapter mySQLiteAdapter;
    Cursor cursor;

    public OfflineDataService() {
        super("OfflineDataService");
    }

    @Override   
    public void onCreate() {

        super.onCreate();
        Log.e("Service", "Service Created.. ");

         mySQLiteAdapter = new SQLiteAdapter(this);
         mySQLiteAdapter.getWritableDatabase();

}   

    @Override
    protected void onHandleIntent(Intent intent) {

        Bundle bundle=intent.getExtras();
        un=bundle.getString("un");
        shop=bundle.getString("shop");
        city=bundle.getString("city");
        date=bundle.getString("date");
        order=bundle.getString("order");

        Log.e("Service Started", "Successful");

        //Inserting New Record
        mySQLiteAdapter.insert(un,shop,city,date,order);

        timer.scheduleAtFixedRate(new TimerTask(){

            @Override
            public void run() {

                //Checking network connectivity
                   wifi=NetworkInfo.Wifi(OfflineDataService.this);
                   edge=NetworkInfo.EDGE(OfflineDataService.this);

                       if(wifi==true||edge==true)
                       {
                           Log.e("Service Network", "Network is online");
                           DataSender();
                       }
                       else
                       {
                           Log.e("Service Network", "Network is offline");
                       }
            }

        }, 1000, 5000);


    }

    /*@Override
    public void onDestroy() {

        super.onDestroy();
      //  mySQLiteAdapter.deleteAll();
        Log.e("Service Destroy", "Successful");


    }*/

    private void DataSender()
    {
        //timer.cancel();

        List<NameValuePair> contacts=new ArrayList<NameValuePair>();

        contacts=mySQLiteAdapter.getAllContacts();

         String url="http://10.0.2.2:3325/Product/Create?"; 

         int response = 0;

            try 
            {
                response = CustomHttpClient.executeHttpPost(url, contacts);

                if(response==200)
                {
                    Log.e("Data Sent", "Response 200");
                }

                else{

                    Log.e("Service Data", "Faield to upload data" );
                }

            }
            catch (Exception e)
            {
                Log.e("Data Sending", e.toString());
                e.printStackTrace();
            }
    }

}

SQLiteAdapter.java

package com.remote.synchronizer.haris;

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

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

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 SQLiteAdapter extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "Product";
    public static final String TABLE_NAME = "Orders";
    public static final int DATABASE_VERSION = 1;
    public static final String KEY_ID = " _id";  //
    public static final String KEY_NAME = "name";
    public static final String KEY_SHOP = "shop";
    private static final String KEY_CITY = "city";
    private static final String KEY_DATE = "datee";
    private static final String KEY_ORDER = "orderr";


    private static final String CREATE_TABLE =
        "CREATE TABLE " + TABLE_NAME + " ("
        + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + KEY_NAME + " VARCHAR,"
        + KEY_SHOP + " VARCHAR," + KEY_CITY + " VARCHAR, " + KEY_DATE + " VARCHAR, " + KEY_ORDER + " VARCHAR" +");";


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

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

    private SQLiteDatabase sqLiteDatabase;
    private SQLiteAdapter sqLiteHelper=this;

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

    public void insert(String name, String shop, String city, String datee, String orderr){

        try
        {
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_NAME, name);
        contentValues.put(KEY_SHOP, shop);
        contentValues.put(KEY_CITY, city);
        contentValues.put(KEY_DATE, datee);
        contentValues.put(KEY_ORDER, orderr);
        sqLiteDatabase.insertOrThrow(TABLE_NAME, KEY_ID, contentValues);
        //sqLiteDatabase.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            Log.e("Insertion", e.toString());

            }
    }

    public void deleteAll(){
         sqLiteDatabase.delete(TABLE_NAME, null, null);
    }

    public void delete_byID(int id){
        sqLiteDatabase.delete(TABLE_NAME, KEY_ID+"="+id, null);
    }

    /*public Cursor queueAll(){
        String[] columns = new String[]{KEY_ID, KEY_NAME, KEY_SHOP, KEY_CITY,  KEY_DATE, KEY_ORDER};
        Cursor cursor = sqLiteDatabase.query(TABLE_NAME, columns, 
                null, null, null, null, null);

        return cursor;
    }*/

    public List<NameValuePair> getAllContacts() {
        List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

        String selectQuery = "SELECT  * FROM " + TABLE_NAME;
        sqLiteHelper.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);

        if(cursor.moveToFirst()){
            while(!cursor.isAfterLast())
            {
                postParameters.add(new BasicNameValuePair("User", cursor.getString(cursor.getColumnIndex(KEY_NAME))));
                postParameters.add(new BasicNameValuePair("ShopName", cursor.getString(cursor.getColumnIndex(KEY_SHOP))));
                postParameters.add(new BasicNameValuePair("city", cursor.getString(cursor.getColumnIndex(KEY_CITY))));
                postParameters.add(new BasicNameValuePair("OrderDate", cursor.getString(cursor.getColumnIndex(KEY_DATE))));
                postParameters.add(new BasicNameValuePair("OrderDetail", cursor.getString(cursor.getColumnIndex(KEY_ORDER))));
                cursor.moveToNext();
            }

        }
        //cursor.close();
        return postParameters;
    };



    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(SQLiteAdapter.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE);
            onCreate(db);
    }

}

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-12T19:54:08+00:00Added an answer on June 12, 2026 at 7:54 pm

    You never initialize sqLiteDatabase in your SQLiteAdapter class.

    Change your onCreate method to this:

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
        sqLiteDatabase = db;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

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.