error missing return statement in method - java

I want to get data based on a fragment of the name , but why the error with a description of the missing return statement ?
public Cursor getName (String nama){
Cursor c = db.query(Konstanta.NAMA_TABEL, new String[]{
Konstanta.ID_ARTIS,
Konstanta.NAMA_ARTIS,
Konstanta.GENDER_ARTIS_PRIA,
Konstanta.GENDER_ARTIS_WANITA,
Konstanta.TGLAHIR}, Konstanta.NAMA_ARTIS + "LIKE '%" + nama + "%'",
null, null, null, null, null);
if (c != null){
c.moveToFirst();
return c;
}
}

Your method returns a result only if c is not null. It must return a result in all scenarios, i.e. even if c is null.
You can change
if (c != null){
c.moveToFirst();
return c;
}
to
if (c != null){
c.moveToFirst();
}
return c;

Every method which has a return value must return the described bean. You need to return null or throw a custom exception if c is not null.
public Cursor getName (String nama){
Cursor c = db.query(Konstanta.NAMA_TABEL, new String[]{
Konstanta.ID_ARTIS,
Konstanta.NAMA_ARTIS,
Konstanta.GENDER_ARTIS_PRIA,
Konstanta.GENDER_ARTIS_WANITA,
Konstanta.TGLAHIR}, Konstanta.NAMA_ARTIS + "LIKE '%" + nama + "%'",
null, null, null, null, null);
if (c != null){
c.moveToFirst();
return c;
}
throw new IllegalStateException("c is null");
}

add below line -
return null;
in your code block -
public Cursor getName (String nama){
Cursor c = db.query(Konstanta.NAMA_TABEL, new String[]{
Konstanta.ID_ARTIS,
Konstanta.NAMA_ARTIS,
Konstanta.GENDER_ARTIS_PRIA,
Konstanta.GENDER_ARTIS_WANITA,
Konstanta.TGLAHIR}, Konstanta.NAMA_ARTIS + "LIKE '%" + nama + "%'",
null, null, null, null, null);
if (c != null){
c.moveToFirst();
return c;
}
return null;
}

Your method must return Cursor type value but you are returning value only in a if loop
do this way :
public Cursor getName (String nama){
Cursor c =null;
c = db.query(Konstanta.NAMA_TABEL, new String[]{
Konstanta.ID_ARTIS,
Konstanta.NAMA_ARTIS,
Konstanta.GENDER_ARTIS_PRIA,
Konstanta.GENDER_ARTIS_WANITA,
Konstanta.TGLAHIR}, Konstanta.NAMA_ARTIS + "LIKE '%" + nama + "%'",
null, null, null, null, null);
if (c != null){
c.moveToFirst();
return c;
}
return c;
}

Related

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 when convert uri to path

When I try to convert a uri to a path to use to upload an image to an apis, this function returns
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
public String getPath(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
if( cursor != null && cursor.moveToFirst() ){
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
}
return path;
}
You should check whether you found something with your query or not.
Do someting like this:
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor.moveToFirst() && cursor.getCount() >= 1) {
// rest of your code
// don't forget to check also for your next query
}

Trying to hide null values from being printed

How do I hide null values from being printed? I have one Products table which has 5 columns. ID, Dairy, Fruit, Vegetables, Grains, and Protein.
When I search for dairy it brings up null values. Whats the best way to hide it?
public String dbToStringDairy() {
SQLiteDatabase db = getWritableDatabase();
String dbStringDairy = "";
String query = "SELECT * FROM " + TABLE_NAME + " WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while (!c.isAfterLast()) {
if (c.getString(c.getColumnIndex(COLUMN_ID)) != null) {
dbStringDairy += " ";
dbStringDairy += c.getString(c.getColumnIndex(COLUMN_PRODUCT_DAIRY));
dbStringDairy += "\n";
}
c.moveToNext();
}
db.close();
return dbStringDairy;
}
The Simplest way to return something else instead of null, using an if else statement to check if the return result is null or not, if it is null store something else so that the program wont return and print null.
Here some simple example:
public String dbToStringDairy()
{
SQLiteDatabase db = getWritableDatabase();
String dbStringDairy = "";
String query = "SELECT * FROM " + TABLE_NAME + " WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast())
{
if(c.getString(c.getColumnIndex(COLUMN_ID))!=null)
{
dbStringDairy += " ";
dbStringDairy += c.getString(c.getColumnIndex(COLUMN_PRODUCT_DAIRY));
dbStringDairy += "\n";
}
c.moveToNext();
}
db.close();
//Replace the `null` into other useful information
if(dbStringDairy==null){
dbStringDairy="No Results";
}
return dbStringDairy;
}
There are a lot of way to ensure your program won't print out null value. This method perform in the dbToStringDairy(), there is also way to do it during your Print Implementation.
Anywhere, hope this simple solution could help.

How to search a column in sqlite by a string

I would like to return a true / false value when the string match with the column of sqlite. How can I implement the search function?
How to use cursor to do searching?
public boolean searchLuggage(String code) {
ArrayList<String> luggageCheck = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
try {
Cursor cursor = db.query(true, TABLE_LUGGAGES, new String[] { your field list }, SearchColumnname + "='" + code + "'", null, null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
luggageCheck.add(cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
return true;
}
return false;
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
// Creating Tables
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LUGGAGE_TABLE = "CREATE TABLE " + TABLE_LUGGAGES + "(" + KEY_NAME + " TEXT NOT NULL," + KEY_CODE + " TEXT NOT NULL," + KEY_NUMBER + " TEXT NOT NULL );";
db.execSQL(CREATE_LUGGAGE_TABLE);
}
Replace this
Cursor cursor = db.query(true, TABLE_LUGGAGES, new String[] { your field list }, SearchColumnname + "='" + code + "'", null, null, null, null, null);
with this
Cursor cursor = db.query(true, TABLE_LUGGAGES, null, SearchColumnname + "= ?", new String[] { code }, null, null, null, null);

getting " (1) no such column: _id10 " error

I'm trying to retrieve the data of a particulate row in a sqlite database . and trying to display it to on a EditText but I'm getting this error , Kindly help me in solving the error !
Thanks in advance :)
here is the logcat data showing error
12-31 22:28:17.731: E/2.4(12285): 111111
12-31 22:28:17.741: E/2.5(12285): 111111
12-31 22:28:17.771: E/SQLiteLog(12285): (1) no such column: _id10
12-31 22:28:17.791: D/AndroidRuntime(12285): Shutting down VM
here is the code : ( of the function getName , which is being used to retrieve the data of column 1 ) [getting error after log.e("2.5".... ) ]
public String getName(long l) {
// TODO Auto-generated method stub
Log.e("2.4","111111");
String[] column = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS };
Log.e("2.5","111111");
Cursor c = ourDatabase.query(DATABASE_TABLE, column, KEY_ROWID + "" + l, null, null, null, null);
Log.e("2.6","111111");
if(c != null)
{
Log.e("2.7","111111");
c.moveToFirst();
Log.e("2.8","111111");
String name = c.getString(1);
Log.e("2.9","111111");
return name;
}
return null;
}
public String getHotness(long l)
{
Log.e("3.1","111111");
// TODO Auto-generated method stub
String[] column = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS };
Log.e("3.2","111111");
Cursor c = ourDatabase.query(DATABASE_TABLE, column, KEY_ROWID + "" + l, null, null, null, null);
Log.e("3.3","111111");
if(c != null)
{
Log.e("3.4","111111");
c.moveToFirst();
Log.e("3.5","111111");
String hotness = c.getString(2);
Log.e("3.6","111111");
return hotness;
}
return null;
}
And is the code of the activity which is calling this function
case R.id.bgetInfo:
String s = sqlRow.getText().toString();
long l = Long.parseLong(s);
HotOrNot hon = new HotOrNot(this);
hon.open();
String returnedName = hon.getName(l);
String returnedHotness = hon.getHotness(l);
hon.close();
Log.e("1.2","111111");
sqlName.setText(returnedName);
Log.e("1.3","111111");
sqlHotness.setText(returnedHotness);
Log.e("1.4","111111");
break;
I think you missed a equal sign at:
Cursor c = ourDatabase.query(DATABASE_TABLE, column, KEY_ROWID + "" + l, null, null, null, null);
Change to:
Cursor c = ourDatabase.query(DATABASE_TABLE, column, KEY_ROWID + " = " + l, null, null, null, null);

Android SQLite CursorIndexOutOfBounds

For some reason when the username I input is not found, the application crashes. But when the username is found it seems to run perfect. I even do a check to see if the returned cursor == null. Heres the code
public boolean isUserAuthenticated(String username, String password) {
// TODO Auto-generated method stub
boolean authenticated = false;
String[] columns = new String[] {LOGIN_USERNAME, LOGIN_PASSWORD};
String sqlUsername = "\"" + username + "\"";
Cursor c = ourDatabase.query(LOGIN_TABLE, columns, LOGIN_USERNAME + "="+ sqlUsername, null, null, null, null);
if (c != null) {
c.moveToFirst();
String passwordAttachedToUsername = c.getString(1);
if(passwordAttachedToUsername.equals(password)) {
authenticated = true;
}
}
return authenticated;
}
Your Cursor object may not be null, but the size of its result set is 0. Instead of:
if (c != null) {
...
}
try:
if (c.getCount() > 0) {
...
}
Also, as #mu is too short mentioned, you could just use the return value of c.moveToFirst() in your conditional:
if (c.moveToFirst()) {
String passwordAttachedToUsername = c.getString(1);
if (passwordAttachedToUsername.equals(password)) {
authenticated = true;
}
}
First, the condition should be:
if (c != null && c.getCount() > 0)
Second, you can refactor
String passwordAttachedToUsername = c.getString(1);
if(passwordAttachedToUsername.equals(password)) {
authenticated = true;
}
with this instead:
authenticated = password.equals(c.getString(1));
Change:
if (c != null) {
c.moveToFirst();
...
}
to
if (c != null && c.moveToFirst()) {
...
}
which will return true if c != null and the size of the cursor is greater than 0.
The query command will always return a cursor so your test for null will always fail. You need to check the count the cursor contains using cursor.getCount()
if (cursor != null && cursor.getCount() > 0) {
if (c.moveToFirst()) {
String passwordAttachedToUsername = c.getString(1);
if(passwordAttachedToUsername.equals(password)) {
authenticated = true;
}}
// your logic goes here
} else {
Toast.makeText(getApplicationContext(), "No Record Found", 1000).show();
}

Categories

Resources