I have this code to retrieve data from file then make some treatment, and finally set the result in an EditTextPreference.
try {
//open the specified input file and create a reader
FileInputStream fIn = context.openFileInput(fileDir+fileName);
InputStreamReader ipsr = new InputStreamReader(fIn);
BufferedReader b = new BufferedReader(ipsr);
String ligne;
while ((ligne = b.readLine()) != null) {
String prix = ligne.split(" ")[2];
//prix = prix.substring(0, prix.length() - 2);
Log.d("PRIX",prix);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String prix_total = settings.getString("prix_total", "0");
int a= Integer.valueOf(prix_total);
int c= Integer.valueOf(prix);
a= c + a;
String z=String.valueOf(a);
Log.d("A",z);
EditTextPreference myEditTextPreference = (EditTextPreference) findPreference("prix_total");
myEditTextPreference.setText(z);
}
//done, cleanup and return
fIn.close();
ipsr.close();
}
catch (Exception e)
{
Log.e("blah", "Exception", e);
}
However, i’m encountering this problem(logcat):
09-12 01:09:10.941: ERROR/blah(10866): Exception
09-12 01:09:10.941: ERROR/blah(10866): java.lang.NumberFormatException: unable to parse '' as integer
09-12 01:09:10.941: ERROR/blah(10866): at java.lang.Integer.parseInt(Integer.java:362)
09-12 01:09:10.941: ERROR/blah(10866): at java.lang.Integer.parseInt(Integer.java:332)
09-12 01:09:10.941: ERROR/blah(10866): at java.lang.Integer.valueOf(Integer.java:506)
09-12 01:09:10.941: ERROR/blah(10866): at carburant.android.com.Settings.onCreate(Settings.java:46)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-12 01:09:10.941: ERROR/blah(10866): at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 01:09:10.941: ERROR/blah(10866): at android.os.Looper.loop(Looper.java:130)
09-12 01:09:10.941: ERROR/blah(10866): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-12 01:09:10.941: ERROR/blah(10866): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 01:09:10.941: ERROR/blah(10866): at java.lang.reflect.Method.invoke(Method.java:507)
09-12 01:09:10.941: ERROR/blah(10866): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-12 01:09:10.941: ERROR/blah(10866): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-12 01:09:10.941: ERROR/blah(10866): at dalvik.system.NativeStart.main(Native Method)
What could be the problem? I debugged “prix” and founding that it outputs '' :.
Thanks for helping.
PS: The data has that form:
data = date + ": " + y + "L/100KM "+ " " + value1 + "dt "+ value2 + "KM\n";
valueOf and parseInt can’t handle empty strings or nulls. Add some checks such as using TextUtils.IsEmpty(string) to verify you are not passing empty data.
Also I see you are surrounding this with a try block, consider adding a catch (NumberFormatException ex) block and logging the error if it is never supposed to be empty.