'The local variable may not have been initialized for Cursor android
I am using Cursor to acquire data from the database based on the food name (paramString1) and food group (paramString 2). Food name is from user input.
But there are few errors found.
The local variable localCursor may not have been initialized.
This error occur to every localCursor after the while(true) loop. Same error also happen to occur to arrayofString[i] in the last 2 line.
My code for search food method
public Cursor searchFoods(String paramString1, String paramString2)
{
Cursor localCursor;
String str1 = paramString1.replace("'", "").replace("/", "").replace("\\", "").replace("%", "").replace(">", "").replace("<", "").replace(";", "").replace(":", "").replace("?", "");
String str2 = "";
String[] arrayOfString2;
int i1;
int i3;
int i;
int j;
String[] arrayOfString1;
if (str1.contains(" "))
{
arrayOfString2 = str1.split(" ");
i1 = 0;
int i2 = arrayOfString2.length;
i3 = 0;
if (i3 >= i2)
{
if (paramString2.length() > 0)
str2 = str2 + " AND FdGrp_Cd='" + paramString2 + "' ";
localCursor = db.rawQuery("SELECT _id, FdGrp_Cd, Long_Desc, NDB_No FROM FOOD_DES WHERE " + str2 + " ORDER BY FdGrp_Cd='0900' DESC, FdGrp_Cd='1100' DESC, FdGrp_Cd='0500' DESC, FdGrp_Cd='0100' DESC, FdGrp_Cd='1500' DESC, FdGrp_Cd='1300' DESC, FdGrp_Cd='1700' DESC, Long_Desc ASC LIMIT 300;", null);
i = 0;
j = localCursor.getColumnIndex("Long_Desc");
arrayOfString1 = new String[localCursor.getCount()];
localCursor.moveToFirst();
}
}
while (true)
{
if (localCursor.isAfterLast())
{
return localCursor;
String str4 = arrayOfString2[i3];
if (str4.length() > 1)
{
String str5 = str4.substring(-1 + str4.length(), str4.length());
if ((str5.toLowerCase().equals(str5)) && (str5.toLowerCase().equals("s")))
str4 = str4.substring(0, -1 + str4.length());
}
if (i1 + 1 < arrayOfString2.length);
for (str2 = str2 + " Long_Desc LIKE '%" + str4 + "%' AND "; ; str2 = str2 + " Long_Desc LIKE '%" + str4 + "%' ")
{
i1++;
i3++;
}
if (str1.length() > 1)
{
int k = -1 + str1.length();
int m = str1.length();
String str3 = str1.substring(k, m);
if ((str3.toLowerCase().equals(str3)) && (str3.toLowerCase().equals("s")))
{
int n = -1 + str1.length();
str1 = str1.substring(0, n);
}
}
str2 = " Long_Desc LIKE '%" + str1 + "%'";
}
arrayOfString1[i] = localCursor.getString(j);
i++;
localCursor.moveToNext();
}
}
Thank you for your attention.
Solution 1:[1]
because localCursor and arrayOfString1 possible null, you don't have default values for those variable
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Fahriyal Afif |
