John Ernest Guadalupe
John Ernest Guadalupe

Reputation: 6629

Why is my android applciation force closing?

I have an android application that would use an SQLite database. But when it gets to the select statement it force closes. I have a class file that I use for doing this :

package com.thesis.menubook;

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;
import java.util.ArrayList;
import java.util.List;

public class DBConnect {

        int id = 0;
        public static final String KEY_ROWID = "_id";
        public static final String KEY_IP = "saved_ip_address";
        private static final String TAG = "DBConnect";

        private static final String DATABASE_NAME = "MenuBook";
        private static final String DATABASE_TABLE_1 = "ipaddress";
       //private static final String DATABASE_TABLE_2 = "menudb";
       //private static final String DATABASE_TABLE_3 = "recipelist";
        private static final int DATABASE_VERSION = 1;

        private static final String DATABASE_CREATE_TABLE_1 =
            "CREATE TABLE ipaddress (_id integer primary key autoincrement, " +
                                 "saved_ip_address text not null " +
                                 "); ";
        private static final String DATABASE_CREATE_TABLE_2 =
            "CREATE TABLE menudb (menu_ID varchar primary key not null, " +
                                 "menu_name longtext, " +
                                 "menu_price double null default, " +
                                 "menu_description longtext, " +
                                 "menu_category text, " +
                                 "menu_status text " +
                                 "); ";
        private static final String DATABASE_CREATE_TABLE_3 =
            "CREATE TABLE recipelist (recipe_ID integer primary key not null autoincrement, " +
                                 "menu_ID varchar null default, " +
                                 "stock_ID varchar null default, " +
                                 "recipe_quantity double null defualt " +
                                 ");" ;

        private final Context context;

        private static DatabaseHelper DBHelper;
        private static SQLiteDatabase db;

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

        public 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_TABLE_1);
                db.execSQL(DATABASE_CREATE_TABLE_2);
                db.execSQL(DATABASE_CREATE_TABLE_3);
            }

            @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 ipaddress");
                db.execSQL("DROP TABLE IF EXISTS menudb");
                db.execSQL("DROP TABLE IF EXISTS recipelist");
                onCreate(db);
            }

        }

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

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

        //---insert a title into the database---
        public long insertIPAddress(String ipaddress)
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_IP, ipaddress);
            return db.insert(DATABASE_TABLE_1, null, initialValues);
        }

        public String getIP()
        {
            String retrievedIP;

            String query = "SELECT saved_ip_address FROM ipaddress WHERE _id = (SELECT MAX(_id) FROM ipaddress)";
            Cursor cursor = db.rawQuery(query, null);
            retrievedIP= cursor.getString(cursor.getColumnIndex("saved_ip_address"));
            cursor.close();
            return retrievedIP;
        }

        public List<ColumnValue[]> select(final String query)
        {
            List<ColumnValue[]> result = null;
            Cursor cursor = db.rawQuery(query, null);

            if (cursor.moveToFirst())
            {
                result = new ArrayList<ColumnValue[]>();

                do
                {
                    int columns = cursor.getColumnCount();
                    ColumnValue[] cvarray = new ColumnValue[columns];

                    for (int i=0; i<columns; i++)
                    {
                        String key = cursor.getColumnName(i);
                        String value = cursor.getString(i);
                        ColumnValue cv = new ColumnValue(key, value);
                        cvarray[i] = cv;
                    }

                    result.add(cvarray);
                }
                while (cursor.moveToNext());
            }

            cursor.close();

            return result;
        }

        public class ColumnValue
        {
            public String column, value;

            public ColumnValue(String c, String v)
            {
                column = c; value = v;
            }
        }


}

Now when I look at the LogCat, I notice that the "red" errors, on which I really don't know what the colors mean, start where the application starts to use the database which is in the onCreate.

I can't see any error on my sql. Is this because of Eclipse, my system, or are there really errors on my create statements? Do point it out!

Here is my LogCat where the start of error is bolded out.

Thank You

01-25 22:58:30.245: DEBUG/AndroidRuntime(521): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
01-25 22:58:30.248: DEBUG/AndroidRuntime(521): CheckJNI is ON
01-25 22:58:34.103: DEBUG/AndroidRuntime(521): Calling main entry com.android.commands.pm.Pm
01-25 23:03:39.518: INFO/ActivityManager(83): Starting: Intent { cmp=com.thesis.menubook/.ChooseTable } from pid 541
01-25 23:03:40.161: WARN/ActivityManager(83): Activity pause timeout for ActivityRecord{4078a9e0 com.thesis.menubook/.IPAddress}

01-25 23:03:40.769: INFO/SqliteDatabaseCpp(541): sqlite returned: error code = 1, msg = near "autoincrement": syntax error, db=/data/data/com.thesis.menubook/databases/MenuBook

01-25 23:03:40.849: WARN/System.err(541): android.database.sqlite.SQLiteException: near "autoincrement": syntax error
01-25 23:03:40.889: WARN/System.err(541):     at android.database.sqlite.SQLiteStatement.native_executeSql(Native Method)
01-25 23:03:40.889: WARN/System.err(541):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:89)
01-25 23:03:40.889: WARN/System.err(541):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1933)
01-25 23:03:40.939: WARN/System.err(541):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1864)
01-25 23:03:40.939: WARN/System.err(541):     at com.thesis.menubook.DBConnect$DatabaseHelper.onCreate(DBConnect.java:68)
01-25 23:03:40.958: WARN/System.err(541):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
01-25 23:03:40.958: WARN/System.err(541):     at com.thesis.menubook.DBConnect.open(DBConnect.java:89)
01-25 23:03:40.958: WARN/System.err(541):     at com.thesis.menubook.IPAddress$InsertIPAddress$1.run(IPAddress.java:92)
01-25 23:03:40.978: WARN/System.err(541):     at android.os.Handler.handleCallback(Handler.java:587)
01-25 23:03:40.978: WARN/System.err(541):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 23:03:40.978: WARN/System.err(541):     at android.os.Looper.loop(Looper.java:132)
01-25 23:03:40.978: WARN/System.err(541):     at android.app.ActivityThread.main(ActivityThread.java:4025)
01-25 23:03:41.046: WARN/System.err(541):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 23:03:41.049: WARN/System.err(541):     at java.lang.reflect.Method.invoke(Method.java:491)
01-25 23:03:41.068: WARN/System.err(541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-25 23:03:41.078: WARN/System.err(541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-25 23:03:41.124: WARN/System.err(541):     at dalvik.system.NativeStart.main(Native Method)
01-25 23:03:49.440: INFO/ActivityManager(83): Displayed com.thesis.menubook/.ChooseTable: +9s277ms
01-25 23:03:49.678: WARN/ActivityManager(83): Launch timeout has expired, giving up wake lock!
01-25 23:03:50.244: WARN/ActivityManager(83): Activity idle timeout for ActivityRecord{408897c0 com.thesis.menubook/.ChooseTable}
01-25 23:03:51.832: WARN/InputConnectionWrapper.ICC(136): Timed out waiting on IInputContextCallback
01-25 23:03:53.015: INFO/InputConnectionWrapper.ICC(136): Got out-of-sequence callback 189 (expected 190) in setCursorCapsMode, ignoring.
01-25 23:04:47.188: INFO/SqliteDatabaseCpp(541): sqlite returned: error code = 1, msg = near "autoincrement": syntax error, db=/data/data/com.thesis.menubook/databases/MenuBook
01-25 23:04:47.258: WARN/System.err(541): android.database.sqlite.SQLiteException: near "autoincrement": syntax error
01-25 23:04:47.327: WARN/System.err(541):     at android.database.sqlite.SQLiteStatement.native_executeSql(Native Method)
01-25 23:04:47.329: WARN/System.err(541):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:89)
01-25 23:04:47.359: WARN/System.err(541):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1933)
01-25 23:04:47.359: WARN/System.err(541):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1864)
01-25 23:04:47.389: WARN/System.err(541):     at com.thesis.menubook.DBConnect$DatabaseHelper.onCreate(DBConnect.java:68)
01-25 23:04:47.398: WARN/System.err(541):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
01-25 23:04:47.398: WARN/System.err(541):     at com.thesis.menubook.DBConnect.open(DBConnect.java:89)
01-25 23:04:47.448: WARN/System.err(541):     at com.thesis.menubook.ChooseTable$GetTableDetails$1.run(ChooseTable.java:102)
01-25 23:04:47.448: WARN/System.err(541):     at android.os.Handler.handleCallback(Handler.java:587)
01-25 23:04:47.448: WARN/System.err(541):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 23:04:47.448: WARN/System.err(541):     at android.os.Looper.loop(Looper.java:132)
01-25 23:04:47.458: WARN/System.err(541):     at android.app.ActivityThread.main(ActivityThread.java:4025)
01-25 23:04:47.471: WARN/System.err(541):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 23:04:47.471: WARN/System.err(541):     at java.lang.reflect.Method.invoke(Method.java:491)
01-25 23:04:47.499: WARN/System.err(541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-25 23:04:47.499: WARN/System.err(541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-25 23:04:47.499: WARN/System.err(541):     at dalvik.system.NativeStart.main(Native Method)
01-25 23:04:47.751: DEBUG/AndroidRuntime(541): Shutting down VM
01-25 23:04:47.751: WARN/dalvikvm(541): threadid=1: thread exiting with uncaught exception (group=0x40014760)
01-25 23:04:47.838: ERROR/AndroidRuntime(541): FATAL EXCEPTION: main
01-25 23:04:47.838: ERROR/AndroidRuntime(541): java.lang.IllegalArgumentException: Host name may not be null
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at org.apache.http.HttpHost.<init>(HttpHost.java:83)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at com.thesis.menubook.JSONParser.makeHttpRequest(JSONParser.java:62)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at com.thesis.menubook.ChooseTable$GetTableDetails$1.run(ChooseTable.java:119)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at android.os.Handler.handleCallback(Handler.java:587)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at android.os.Looper.loop(Looper.java:132)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at android.app.ActivityThread.main(ActivityThread.java:4025)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at java.lang.reflect.Method.invoke(Method.java:491)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-25 23:04:47.838: ERROR/AndroidRuntime(541):     at dalvik.system.NativeStart.main(Native Method)
01-25 23:04:47.950: WARN/ActivityManager(83):   Force finishing activity com.thesis.menubook/.ChooseTable
01-25 23:04:48.575: WARN/ActivityManager(83): Activity pause timeout for ActivityRecord{408897c0 com.thesis.menubook/.ChooseTable}
01-25 23:04:52.609: INFO/Process(541): Sending signal. PID: 541 SIG: 9
01-25 23:04:53INFO.259: /ActivityManager(83): Process com.thesis.menubook (pid 541) has died.
01-25 23:04:53.279: ERROR/InputDispatcher(83): channel '4099fdb0 com.thesis.menubook/com.thesis.menubook.IPAddress (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
01-25 23:04:53.279: ERROR/InputDispatcher(83): channel '4099fdb0 com.thesis.menubook/com.thesis.menubook.IPAddress (server)' ~ Channel is unrecoverably broken and will be disposed!
01-25 23:04:53.420: INFO/WindowManager(83): WIN DEATH: Window{4099fdb0 com.thesis.menubook/com.thesis.menubook.IPAddress paused=false}
01-25 23:04:54.193: INFO/WindowManager(83): WIN DEATH: Window{408347c8 com.thesis.menubook/com.thesis.menubook.ChooseTable paused=false}
01-25 23:04:54.819: INFO/WindowManager(83): WIN DEATH: Window{407c8fa8 com.thesis.menubook/com.thesis.menubook.ChooseTable paused=false}
01-25 23:04:55.620: INFO/WindowManager(83): WINDOW DIED Window{4099fdb0 com.thesis.menubook/com.thesis.menubook.IPAddress paused=false}
01-25 23:04:55.690: ERROR/InputDispatcher(83): Received spurious receive callback for unknown input channel.  fd=187, events=0x8
01-25 23:04:55.690: ERROR/InputDispatcher(83): Received spurious receive callback for unknown input channel.  fd=190, events=0x8
01-25 23:04:56.698: WARN/InputManagerService(83): Got RemoteException sending setActive(false) notification to pid 541 uid 10004
01-25 23:04:58.078: WARN/ActivityManager(83): Launch timeout has expired, giving up wake lock!
01-25 23:04:59.405: DEBUG/dalvikvm(147): GC_EXPLICIT freed 2K, 4% free 12941K/13383K, paused 76ms+28ms
01-25 23:07:14.329: DEBUG/SntpClient(83): request time failed: java.net.SocketException: Address family not supported by protocol
01-25 23:08:58.401: DEBUG/dalvikvm(83): GC_CONCURRENT freed 1467K, 15% free 12265K/14279K, paused 28ms+36ms
01-25 23:12:14.398: DEBUG/SntpClient(83): request time failed: java.net.SocketException: Address family not supported by protocol

Upvotes: 0

Views: 225

Answers (2)

Munish Katoch
Munish Katoch

Reputation: 517

There is no need to add (not null) with autoincrement primary key, can can just use/add below declaration. Please note that once you declare a field as autoincrement, you do not have to insert any value into that field. It will be non null and incremental value.

INTEGER PRIMARY KEY AUTOINCREMENT

For more details, please check the link below: http://www.sqlite.org/faq.html#q1

Upvotes: 0

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

You have two 'create' statements. One contains

_id integer primary key autoincrement

which is correct according to the documentation. The other has

recipe_ID integer primary key not null autoincrement

I suspect that the extra 'not null' is the problem. Since it's a primary key, it should inherently be 'not null', so this is probably your syntax error.

You could also use the debugger to find out which statement execution is causing the problem. A try/catch block around each of your sql statement executions would probably be a good idea too.

Upvotes: 3

Related Questions