I am having this error when running the below code trying to parse the website using JSoup.
ERROR
java.lang.RuntimeException: An error occurred while executing doInBackground()
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.jsoup.select.Elements org.jsoup.nodes.Element.getElementsByTag(java.lang.String)' on a null object reference
The website doesn't have table with the specified id (getElementById('ctl00_ContentPlaceHolder1'). How to fix this exception where the table isn't present?
CODE
@Override
protected Void doInBackground(Void... voids) {
try {
Document evalPage = Jsoup.connect("http://site.example.com")
.cookies(loginCookies)
.timeout(30 * 1000)
.execute().parse();
//TODO FIX THE NO TABLE REFERENCE
table = llPage.getElementById("ctl00_ContentPlaceHolder1").getElementsByTag("table").get(2);
} catch (IOException ex) {
ex.printStackTrace();
parsingSuccessful = false;
}
return null;
}