I am trying to fetch the currency quotes using Yahoo Finance API in my android app on a button click. My code:
String urlStr = "http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X&f=l1&e=.csv";
httpGet = new HttpGet(urlStr);
InputStreamReader is = null;
String value = "";
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
is = new InputStreamReader(response.getEntity().getContent());
BufferedReader reader = new BufferedReader(is);
String line = reader.readLine();
String[] RowData = line.split(",");
value = RowData[0];
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
} finally {
try {
is.close();
} catch (IOException e) {
// handle exception
}
}
((TextView) findViewById(R.id.value)).setText(value);
The apps crashes giving java.lang.IllegalStateException. What am I missing here?
stack trace:
06-25 16:40:19.154: E/AndroidRuntime(2426): FATAL EXCEPTION: main
06-25 16:40:19.154: E/AndroidRuntime(2426): java.lang.IllegalStateException: Could not execute method of the activity
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.view.View$1.onClick(View.java:3599)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.view.View.performClick(View.java:4204)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.view.View$PerformClick.run(View.java:17355)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.os.Handler.handleCallback(Handler.java:725)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.os.Looper.loop(Looper.java:137)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-25 16:40:19.154: E/AndroidRuntime(2426): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 16:40:19.154: E/AndroidRuntime(2426): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 16:40:19.154: E/AndroidRuntime(2426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 16:40:19.154: E/AndroidRuntime(2426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 16:40:19.154: E/AndroidRuntime(2426): at dalvik.system.NativeStart.main(Native Method)
06-25 16:40:19.154: E/AndroidRuntime(2426): Caused by: java.lang.reflect.InvocationTargetException
06-25 16:40:19.154: E/AndroidRuntime(2426): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 16:40:19.154: E/AndroidRuntime(2426): at java.lang.reflect.Method.invoke(Method.java:511)
06-25 16:40:19.154: E/AndroidRuntime(2426): at android.view.View$1.onClick(View.java:3594)
06-25 16:40:19.154: E/AndroidRuntime(2426): ... 11 more
06-25 16:40:19.154: E/AndroidRuntime(2426): Caused by: java.lang.NullPointerException
06-25 16:40:19.154: E/AndroidRuntime(2426): at com.fuzzy.currencyconverter.CurrencyConverterActivity.fetch(CurrencyConverterActivity.java:79)
06-25 16:40:19.154: E/AndroidRuntime(2426): ... 14 more
If line#79 is this code:
is.close();
Then the variable is
is null, meaning that the code:
is = new InputStreamReader(response.getEntity().getContent());
was not executed, so the error comes in this line:
HttpResponse response = httpClient.execute(httpGet, localContext);
That line throws an error so the code inside finally
is executed, because is
is null
, then calling is.close()
throws a NullPointerException
.
You must check if is
is null
before accesing it, because there are chances that is not initialized. Also you must check what is the error returned by HttpResponse response = httpClient.execute(httpGet, localContext);
so you know what happens.
Change this:
} catch (IOException e) {
Log.d("YourApp", e.getMessage());
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
// handle exception
}
}
And check your LogCat for YourApp
tag.