【问题标题】:Database access gives a java.null pointer exception数据库访问产生 java.null 指针异常
【发布时间】:2012-11-28 23:11:16
【问题描述】:

我创建了我的第一个 Android 应用程序。一切正常,除了一个问题。当我在我的应用程序中打开数据库时。在我的 oncreate 活动中,它运行良好。但是当我使用相同的代码检索方法中的值时,我的应用程序崩溃并给出java.null pointer exception

所以我的问题是,是否有另一种方法可以打开数据库,或者我做错了什么。这是我的代码。

这里我的方法给出了 java.null 指针异常。

public class SendGlass extends Activity{

  static String replay;

   public void sms (String phone,String Sms )
   {
        SmsManager smsManager = SmsManager.getDefault();

        String phoneNo = phone;
        String sms = Sms;
        String api = "You Entered one";
        String api2 = "You Entered two";
        String api3 = "Nothing great";
        String rep = sms.replace(phoneNo+" :", "");

        SQLiteDatabase db = openOrCreateDatabase("ComDB", MODE_PRIVATE, null);
        Cursor c = db.rawQuery("SELECT REPLAYS FROM Rtm WHERE ID = 1", null);

        while(c.moveToNext())
        {
            replay = c.getString(c.getColumnIndex("REPLAYS"));

            if(rep.equals("1"))
            {
                smsManager.sendTextMessage(phoneNo, null, replay, null, null);
            }
            if(rep.equals("2"))
            {
                smsManager.sendTextMessage(phoneNo, null, replay, null, null);
            }
            if(rep.equals("3"))
            {
                smsManager.sendTextMessage(phoneNo, null, replay, null, null);
            }
            if(rep.equals("4"))
            {
                smsManager.sendTextMessage(phoneNo, null, replay, null, null);
            }
            if(rep.equals("5"))
            {
                smsManager.sendTextMessage(phoneNo, null, replay, null, null);
            }
            if(rep.equals("6"))
            {
                smsManager.sendTextMessage(phoneNo, null, replay, null, null);
            }
            if(rep.equals("7"))
            {
                smsManager.sendTextMessage(phoneNo, null, replay, null, null);
            }
            if(rep.equals("8"))
            {
                smsManager.sendTextMessage(phoneNo, null, replay, null, null);
            }
       }
 }  

我通过

为这段代码提供参数
     class obj = new Class();
     obj.sms (value,value )

【问题讨论】:

  • 你从哪里得到异常。也发布 logcat
  • 哪一行得到空指针异常?
  • 您也可以为“rep”变量使用 switch case。

标签: java android oop android-sqlite


【解决方案1】:

试试这个

c.moveToFirst():

做{ 你的代码 ////

} while(c.moveToNext)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2019-03-31
    相关资源
    最近更新 更多