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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:03:13+00:00 2026-05-27T16:03:13+00:00

I was trying to add the data i receive from some specific messages to

  • 0

I was trying to add the data i receive from some specific messages to SQLite database..
But when i run my application i am getting error which says … Fatal Exception : Main caused by Java Null Pointer exception at the InsertTitle Function in my DBAdapter.java

This is my DBAdapter Class

package com.lalsoft.janko;

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

public class DBAdapter 
{
public static final String KEY_ROWID = "_id";
public static final String KEY_GQTY = "gqty";
public static final String KEY_NQTY = "nqty";
public static final String KEY_DATE = "ddate";    
private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "lalaqua";
private static final String DATABASE_TABLE = "nsales";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
    "create table titles (_id integer primary key autoincrement, "
    + "gqty text not null, nqty text not null, " 
    + "ddate text not null);";

private final Context context; 

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

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

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

//---opens the database---
public DBAdapter open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---    
public void close() 
{
    DBHelper.close();
}

//---insert a title into the database---
public long insertTitle(String gqty, String nqty, String ddate) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_GQTY, gqty);
    initialValues.put(KEY_NQTY, nqty);
    initialValues.put(KEY_DATE, ddate);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

//---deletes a particular title---
public boolean deleteTitle(long rowId) 
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + 
            "=" + rowId, null) > 0;
}

//---retrieves all the titles---
public Cursor getAllTitles() 
{
    return db.query(DATABASE_TABLE, new String[] {
            KEY_ROWID, 
            KEY_GQTY,
            KEY_NQTY,
            KEY_DATE}, 
            null, 
            null, 
            null, 
            null, 
            null);
}

//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[] {
                    KEY_ROWID,
                    KEY_GQTY, 
                    KEY_NQTY,
                    KEY_DATE
                    }, 
                    KEY_ROWID + "=" + rowId, 
                    null,
                    null, 
                    null, 
                    null, 
                    null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

//---updates a title---
public boolean updateTitle(long rowId, String gqtr, 
String nqty, String ddate) 
{
    ContentValues args = new ContentValues();
    args.put(KEY_GQTY, gqtr);
    args.put(KEY_NQTY, nqty);
    args.put(KEY_DATE, ddate);
    return db.update(DATABASE_TABLE, args, 
                     KEY_ROWID + "=" + rowId, null) > 0;
}
}

This is the class which extends from BroadcastReceiver, which is calling the inserttitle function..

package com.lalsoft.janko;

import java.util.Calendar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;
import android.util.Log;

public class SMSReceiver extends BroadcastReceiver
{
public String SendMsgBody;
private static final String LOG_TAG = "JankoSMS";
public DBAdapter db; 
public Integer isDone=0;
public Double GrossQty,NetQty;

@Override
public void onReceive(Context context, Intent intent) 
{
    db = new DBAdapter(context); 
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";
    String PhNo;
    String MsgBody;
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();
            PhNo=msgs[i].getOriginatingAddress();
            str += " :";
            str += msgs[i].getMessageBody().toString();
            MsgBody=msgs[i].getMessageBody().toString();
            str += "\n";
           // EncodeSMS(MsgBody);

             String GQtyS,NQtyS,sDate;
             GQtyS="Ok";
             NQtyS="Done";
             sDate=getDate();
             Log.i(LOG_TAG, "Date" +" "+ sDate +" "+ GQtyS +" "+ NQtyS);
             long id;
             id = db.insertTitle(GQtyS ,NQtyS,sDate);    

        }

    }                         
}
public static String getDate()
{
    Calendar c = Calendar.getInstance();

    String sDate = c.get(Calendar.DAY_OF_MONTH) + "-" 
    + c.get(Calendar.MONTH)
    + "-" + c.get(Calendar.YEAR); 

    //+ " at " + c.get(Calendar.HOUR_OF_DAY) 
    //+ ":" + c.get(Calendar.MINUTE);
    return sDate;
}
}

Also i have checked the database is not getting created..
So what should be the possible cause of this issue and how can i solve this?? Please help me out of this trouble.

  • 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-27T16:03:13+00:00Added an answer on May 27, 2026 at 4:03 pm

    The problem is in line db.insert() of insertTitle() method. You have to assign the value of db before using so use open the database before using by calling open() as first statement inside insertTitle() method

    Your code will be something like the below

    public long insertTitle(String gqty, String nqty, String ddate) 
    {
        open();
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_GQTY, gqty);
        initialValues.put(KEY_NQTY, nqty);
        initialValues.put(KEY_DATE, ddate);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add some data received from a form to an XML
I'm trying to get the video data from this youtube playlist feed and add
I am trying to make an asynchronous request to get some data from my
I am trying to use SDO2SHP.exe in order to export some spatial data from
I'm trying to use WCF Data Service with Subsonic, but ran into this error
I am trying to add data to a newly created column... DataSet ds =
I have a list and I am trying to add the data to the
I'm trying to add multiple jQuery data entries to a single element. I suspected
I'm trying to add an ADO.NET data service to a vanilla ASP.NET-MVC project. Does
I'm trying to add a Row to my data.frame to spit out the average

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.