CalendarParse parser = new CalendarParse();
parser.countSundays(this);
public class CalendarParse {
String calString = "";
Document cal = null;
InputStream is = null;
public void countSundays(Context context)
{
AssetManager assMan = context.getAssets();
try
{
System.out.println("Trying");
InputStream is = assMan.open("assets/brownbear.html");
if (is == null){
Log.i("FAIL","File not found.");
}
else{
Log.i("SUCCESS","We did it");
}
Document cal = Jsoup.parse(is, "utf-8", "ob stuff");
Element calBlock = cal.select("table#CalBlock").first();
Element calRows = calBlock.select("tr").get(0);
Log.i("SUCCESS","Parsing has occurred");
System.out.println(calRows.toString());
}
catch(IOException e){
System.out.println("IO Exception at local calendar asset");
}
}
brownbear.html is in the assets folder, and this written into the Main Activity. Originally it was a separate .java class, but I haven't been able to hammer out the precisely how "Context" affects AssetManager when it's outside of the Main Activity and I'm trying to extenuate the unknowns while troubleshooting.
As written, "Trying" prints out, and no Log.i prints out (presumably because we're failing at
InputStream is = assMan.open("assets/brownbear.html");
) "IO Exception at local calendar asset" always prints out.
If I change that line to
assMan.open("brownbear.html");
then "SUCCESS" "We did it" runs, but a Fatal Exception occurs:
2019-02-01 02:28:06.069 5637-5637/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.myapplication.schedulo, PID: 5637 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapplication.schedulo/com.myapplication.schedulo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'org.jsoup.select.Elements org.jsoup.nodes.Element.select(java.lang.String)' on a null object reference
What am I doing wrong??
Element calBlock = cal.select("table#CalBlock").first();
Element calRows = calBlock.select("tr").get(0);
The NullPointerException is in the second line because the result of the first one (calBlock
) is null. You should check why the selector "table#CalBlock"
doesn't match what you want. Try to analyze the HTML of the document cal
and check if it contains the <table ...>
with id CalBlock
. Are you sure the page you're parsing doesn't use JavaScript to load some fragments dynamically/asynchronously?