{"id":5277,"date":"2014-03-30T20:16:55","date_gmt":"2014-03-30T20:16:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/android-app-crashing-when-trying-to-convert-inputstream-to-string-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:16:55","modified_gmt":"2014-03-30T20:16:55","slug":"android-app-crashing-when-trying-to-convert-inputstream-to-string-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/android-app-crashing-when-trying-to-convert-inputstream-to-string-collection-of-common-programming-errors\/","title":{"rendered":"android app crashing when trying to convert inputstream to string-Collection of common programming errors"},"content":{"rendered":"<p>in the following code, i am trying to perform a GET operation on a webservice that i have coded and hosted on localhost. The method OpenHttpConnection is working just fine because i have put toasts in between to check if there was sth wrong in there. the app crashes when i try to convert the input stream into a string using the bufferreader. Please have a look and see if you can spot the error.<\/p>\n<p>Thanks \ud83d\ude42<\/p>\n<pre><code>public class ServicetestActivity extends Activity {\n\n\n\npublic static String iStream_to_String(InputStream is1)\n{\n     BufferedReader rd = new BufferedReader(new InputStreamReader(is1));\n     String line;\n     StringBuilder sb =  new StringBuilder();\n     try {\n        while ((line = rd.readLine()) != null) {\n                sb.append(line);\n         }\n         rd.close();\n\n    } catch (IOException e) {\n        \/\/ TODO Auto-generated catch block\n        e.printStackTrace();\n    }\n     String contentOfMyInputStream = sb.toString();\n     return contentOfMyInputStream;\n}\n\n\n\n\n\n\n\/** Called when the activity is first created. *\/\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.main);\n    InputStream is=null;\n    try {\n         is=OpenHttpConnection(\"http:\/\/localhost\/webservice.php?device=ayaz\");\n    } catch (IOException e) {\n        \/\/ TODO Auto-generated catch block\n        e.printStackTrace();\n    }\n    String line= iStream_to_String(is);\n    Toast.makeText(this, line, Toast.LENGTH_LONG).show(); \n}\n\n\n    private InputStream OpenHttpConnection(String link) throws IOException {\n        \/\/ TODO Auto-generated method stub\n        InputStream inputStream = null;\n        int response = -1;\n        Toast.makeText(this, \"1\", Toast.LENGTH_SHORT).show();\n        URL url = new URL(link);\n        URLConnection connection = url.openConnection();\n        Toast.makeText(this, \"2\", Toast.LENGTH_SHORT).show();\n        if (!(connection instanceof HttpURLConnection))\n          throw new IOException(\"Not a HTTP connection\");\n        try {\n            HttpURLConnection httpURLConnection = (HttpURLConnection) connection;\n            Toast.makeText(this, \"3\", Toast.LENGTH_SHORT).show();\n            httpURLConnection.setAllowUserInteraction(false);\n            Toast.makeText(this, \"4\", Toast.LENGTH_SHORT).show();\n            httpURLConnection.setInstanceFollowRedirects(true);\n            httpURLConnection.setRequestMethod(\"GET\");\n            httpURLConnection.connect();\n            response = httpURLConnection.getResponseCode();\n            if (response == HttpURLConnection.HTTP_OK) {\n                inputStream = httpURLConnection.getInputStream();\n            }\n\n        } catch (Exception e) {\n            \/\/ TODO: handle exception\n            throw new IOException(\"Error connecting\");\n        }\n        return inputStream;}\n\n\/\/ see http:\/\/androidsnippets.com\/executing-a-http-get-request-with-httpclient\n<\/code><\/pre>\n<p>}<\/p>\n<p>Also the log cat is as follows:<\/p>\n<pre><code>10-14 20:54:13.660: E\/AndroidRuntime(1348): FATAL EXCEPTION: main\n10-14 20:54:13.660: E\/AndroidRuntime(1348): java.lang.RuntimeException: Unable to start activity ComponentInfo{web.service\/web.service.ServicetestActivity}: java.lang.NullPointerException\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at android.os.Handler.dispatchMessage(Handler.java:99)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at android.os.Looper.loop(Looper.java:130)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at android.app.ActivityThread.main(ActivityThread.java:3683)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at java.lang.reflect.Method.invokeNative(Native Method)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at java.lang.reflect.Method.invoke(Method.java:507)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at dalvik.system.NativeStart.main(Native Method)\n10-14 20:54:13.660: E\/AndroidRuntime(1348): Caused by: java.lang.NullPointerException\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at java.io.Reader.(Reader.java:65)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at java.io.InputStreamReader.(InputStreamReader.java:122)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at java.io.InputStreamReader.(InputStreamReader.java:59)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at web.service.ServicetestActivity.iStream_to_String(ServicetestActivity.java:31)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at web.service.ServicetestActivity.onCreate(ServicetestActivity.java:65)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)\n10-14 20:54:13.660: E\/AndroidRuntime(1348):     ... 11 more\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>in the following code, i am trying to perform a GET operation on a webservice that i have coded and hosted on localhost. The method OpenHttpConnection is working just fine because i have put toasts in between to check if there was sth wrong in there. the app crashes when i try to convert the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5277","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5277"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5277\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}