Reputation: 475
I am accessing the internet to get information. The app is force closing when i m accessing the internet.If the app is deleted and re-installed it does not force close when i m accesing the internet .This is happening the first time the app is installed. Please help me figure this out.
This is the stack trace -
java.lang.RuntimeException: Unable to start activity ComponentInfo{ListOfDealSites}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.io.DataInputStream.readLine(DataInputStream.java:309)
at ListOfDealSites.onCreate(ListOfDealSites.java:53)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
It also says source method - DataInputStream.readLine()
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dealsites);
InputStream in = null;
try {
// Connect to the server to get the list of deal sites
in = OpenHttpConnection("Link to deal server");
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
DataInputStream dataIO = new DataInputStream(in);
String strLine = null;
int i = 0;
while((strLine = dataIO.readLine())!= null) // Line 53------
{
count = i;
NEWDEALSITES[i++] = strLine;
}
dataIO.close();
in.close();
}
catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
count_deals = count/2;
newdeals = new String[count_deals+1];
newsites = new String[count_deals+1];
int j, k;
for(j = 0, k = 0; k < count; j++)
{
newdeals[j] = NEWDEALSITES[k++];
newsites[j] = NEWDEALSITES[k++];
}
listOfDealSites = (ListView)findViewById(R.id.ListView01);
listOfDealSites.setAdapter(new ArrayAdapter<String>(this,R.layout.row,newdeals));
}
//Function to connect to the server.
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
}
Upvotes: 1
Views: 230
Reputation: 475
The problem was caused because of no internet connection. I caught the exception and I am giving out a message to check the connection.
Thank you all for your help.
Upvotes: 0
Reputation: 48871
For some reason, OpenHttpConnection
is returning null which basically means you're not getting an OK response here...
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
This means that dataIO will be invalid when you're trying to read data on line 53.
From the DataInputStream constructor docs...
public DataInputStream (InputStream in) Since: API Level 1
Constructs a new DataInputStream on the InputStream in.
All reads are then filtered through this stream. Note that data read by this stream is not in a human readable format and was most likely created by a DataOutputStream.
Warning: passing a null source creates an invalid DataInputStream. All operations on such a stream will fail.
Not sure what would be causing this or how to fix it but you definitely need to check for a null result returned from OpenHttpConnection
before trying to use the 'in' InputStream for creating your DataInputStream.
Upvotes: 1