Shiam Kannan Shiam Kannan - 3 months ago 10
Android Question

java.lang.runtimeExeception unable to start activity componentInfo

I am trying to create an android app that gets poll data from the realclearpolitics website and converts it into a graph. It is supposed to store each of the columns in the poll table on the website into an arraylist using jsoup. When I run the app, however, i'm getting an exception.

package com.pollapp.presidentialelectionpolls;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

@SuppressLint("NewApi")

public class MainActivity extends Activity {

Document doc;
Element table;
Elements rows;
ListView dispData;
ArrayList<String> nameOfSrc = new ArrayList<String>();
ArrayList<String> dateWidth = new ArrayList<String>();
ArrayList<String> Trump = new ArrayList<String>();
ArrayList<String> Clinton = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dispData = (ListView) findViewById(R.id.listView1);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);

try {
doc = Jsoup
.connect(
"http://www.realclearpolitics.com/epolls/2016/president/us/general_election_trump_vs_clinton-5491.html")
.get();
} catch (IOException e) {
e.printStackTrace();
}

table = doc.select("table").get(1);
rows = table.select("tr");

for (int i = 2; i < rows.size(); i++) {
Element row = rows.get(i);
Elements cols = row.select("td");
nameOfSrc.add(cols.get(0).text());
dateWidth.add(cols.get(1).text());
Trump.add(cols.get(4).text());
Clinton.add(cols.get(5).text());
}

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Trump);
dispData.setAdapter(arrayAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


here is my logcat info:

08-10 06:42:13.999: E/AndroidRuntime(1748): FATAL EXCEPTION: main
08-10 06:42:13.999: E/AndroidRuntime(1748): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pollapp.presidentialelectionpolls/com.pollapp.presidentialelectionpolls.MainActivity}: java.lang.NullPointerException
08-10 06:42:13.999: E/AndroidRuntime(1748): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-10 06:42:13.999: E/AndroidRuntime(1748): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-10 06:42:13.999: E/AndroidRuntime(1748): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-10 06:42:13.999: E/AndroidRuntime(1748): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-10 06:42:13.999: E/AndroidRuntime(1748): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 06:42:13.999: E/AndroidRuntime(1748): at android.os.Looper.loop(Looper.java:137)
08-10 06:42:13.999: E/AndroidRuntime(1748): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-10 06:42:13.999: E/AndroidRuntime(1748): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 06:42:13.999: E/AndroidRuntime(1748): at java.lang.reflect.Method.invoke(Method.java:525)
08-10 06:42:13.999: E/AndroidRuntime(1748): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-10 06:42:13.999: E/AndroidRuntime(1748): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-10 06:42:13.999: E/AndroidRuntime(1748): at dalvik.system.NativeStart.main(Native Method)
08-10 06:42:13.999: E/AndroidRuntime(1748): Caused by: java.lang.NullPointerException
08-10 06:42:13.999: E/AndroidRuntime(1748): at com.pollapp.presidentialelectionpolls.MainActivity.onCreate(MainActivity.java:50)
08-10 06:42:13.999: E/AndroidRuntime(1748): at android.app.Activity.performCreate(Activity.java:5133)
08-10 06:42:13.999: E/AndroidRuntime(1748): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-10 06:42:13.999: E/AndroidRuntime(1748): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-10 06:42:13.999: E/AndroidRuntime(1748): ... 11 more

Answer

You are using the wrong table (there are four an the side, not two as assumed) resulting in only two entries in rows.

Try:

table = doc.select("table.data.large").get(1);

I would also recommend to verify doc is valid after the try-catch block and react accordingly or move the following code using the Document also into the try-catch block, otherwise the chance of running into unhandled exceptions is very high (for example if the server is not reachable, etc.).

Comments