Android : null pointer exception while working in device but not in emulator

on Wednesday, August 6, 2014


I got Null value in jsonBranchArray while working in device. But I got value for the same while working in emulator.



String jsonBranchArray = appUtils
.getDataFromUrl("---URL---");


And AppUtils class is..



public class AppUtils {


public String getDataFromUrl(String url) {

BufferedReader reader = null;
String str = null;

try {

URL url2 = new URL(url);
InputStream ins = url2.openStream();
reader = new BufferedReader(new InputStreamReader(ins));
StringBuilder builder = new StringBuilder();
while ((str = reader.readLine()) != null) {
builder.append(str);
}
str = builder.toString();
} catch (Exception e) {
e.printStackTrace();
str = null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return str;
}}


Please help on it. Thanks in advance.


0 comments:

Post a Comment