Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 即使没有数据,如何防止arrayindexoutofbound异常?[sqlite android]_Java_Android - Fatal编程技术网

Java 即使没有数据,如何防止arrayindexoutofbound异常?[sqlite android]

Java 即使没有数据,如何防止arrayindexoutofbound异常?[sqlite android],java,android,Java,Android,首先,我只创建数据。 第二,我不添加任何数据来保持表为空 那么我就称之为评论 Cursor cursor = sqlite.fetchdata(2, DatabaseHelper.TABLE_COMMENT); String getcontent = cursor.getString(0); 这肯定会给我一个例外 String getcontent = ""; try { getcontent = cursor.getString(0);

首先,我只创建数据。 第二,我不添加任何数据来保持表为空 那么我就称之为评论

Cursor cursor = sqlite.fetchdata(2, DatabaseHelper.TABLE_COMMENT);
String getcontent = cursor.getString(0);
这肯定会给我一个例外

String getcontent = "";
        try {
            getcontent = cursor.getString(0);
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }

所以我的问题是,即使有空表,如何获得字符串?因为默认情况下我的应用程序是空的。

在第一行后面打勾

if(cursor!=null && cursor.moveToFirst()) {
    //Your computation here
}
或者将语句放入try-catch块以处理异常

String getcontent = "";
        try {
            getcontent = cursor.getString(0);
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }

希望对您有所帮助。

我应该先获取数据然后仅检查还是先检查仅获取?如果是getInt2;还有,cursor.moveToFirst?