SQLite 数据库没有被创建..为什么?

发布于 2024-12-23 00:19:14 字数 6114 浏览 1 评论 0原文

我试图将从某些特定消息收到的数据添加到 SQLite 数据库中。 但是当我运行我的应用程序时,我收到错误消息... Fatal Exception : Main Caused by Java Null Pointer exception at the InsertTitle Function in my DBAdapter.java

这是我的 DBAdapter 类

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;
}
}

这是从 BroadcastReceiver 扩展的类,它是调用 inserttitle 函数。

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;
}
}

我还检查了数据库没有被创建。 那么这个问题的可能原因是什么以及我该如何解决这个问题?请帮助我摆脱这个麻烦。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过气美图社 2024-12-30 00:19:14

问题出在 insertTitle() 方法的 db.insert() 行中。 您必须在使用之前分配 db 的值,因此请在使用之前通过调用 open() 打开数据库 作为 insertTitle() 方法中的第一个语句

您的代码将类似于以下内容

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);
}

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);
}
破晓 2024-12-30 00:19:14

我猜你还没有调用 db.open(); 这就是这个方法

public DBAdapter open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}

I guess you haven't called db.open(); that is this method

public DBAdapter open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}
芸娘子的小脾气 2024-12-30 00:19:14

遵循sunil & 给出的上述2条建议。拉利特
还包括以下一项,即放置 db.execSQL(DATABASE_CREATE);在 try catch 块中

public void onCreate(SQLiteDatabase db) {
System.out.println("table created....");
          try{
        db.execSQL(DATABASE_CREATE);
          }catch (Exception e) {
            // TODO: handle exception
        }
    }

Follow the above 2 suggestions given by sunil & lalit
include the below one also i.e place db.execSQL(DATABASE_CREATE); in try catch block

public void onCreate(SQLiteDatabase db) {
System.out.println("table created....");
          try{
        db.execSQL(DATABASE_CREATE);
          }catch (Exception e) {
            // TODO: handle exception
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文