Android/Java - IO 异常 - 似乎无法加载 HTML 资产
Android/Java - IO Exception - can't seem to load HTML asset
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在assets文件夹里,这个写进MainActivity。最初它是一个单独的 .java class,但是当它在 Main Activity 之外时,我无法准确地确定 "Context" 如何影响 AssetManager 并且我在故障排除时试图减轻未知数。
如所写,"Trying" 打印出来,没有 Log.i 打印出来(大概是因为我们在
处失败了
InputStream is = assMan.open("assets/brownbear.html");
) "IO Exception at local calendar asset" 总是打印出来。
如果我将该行更改为
assMan.open("brownbear.html");
然后 "SUCCESS" "We did it" 运行,但发生致命异常:
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
我做错了什么??
Element calBlock = cal.select("table#CalBlock").first();
Element calRows = calBlock.select("tr").get(0);
NullPointerException 在第二行,因为第一行 (calBlock
) 的结果为空。您应该检查为什么选择器 "table#CalBlock"
与您想要的不匹配。尝试分析文档 cal
的 HTML 并检查它是否包含 ID 为 CalBlock
的 <table ...>
。您确定您正在解析的页面没有使用 JavaScript 加载一些片段 dynamically/asynchronously 吗?
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在assets文件夹里,这个写进MainActivity。最初它是一个单独的 .java class,但是当它在 Main Activity 之外时,我无法准确地确定 "Context" 如何影响 AssetManager 并且我在故障排除时试图减轻未知数。
如所写,"Trying" 打印出来,没有 Log.i 打印出来(大概是因为我们在
处失败了InputStream is = assMan.open("assets/brownbear.html");
) "IO Exception at local calendar asset" 总是打印出来。
如果我将该行更改为
assMan.open("brownbear.html");
然后 "SUCCESS" "We did it" 运行,但发生致命异常:
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
我做错了什么??
Element calBlock = cal.select("table#CalBlock").first();
Element calRows = calBlock.select("tr").get(0);
NullPointerException 在第二行,因为第一行 (calBlock
) 的结果为空。您应该检查为什么选择器 "table#CalBlock"
与您想要的不匹配。尝试分析文档 cal
的 HTML 并检查它是否包含 ID 为 CalBlock
的 <table ...>
。您确定您正在解析的页面没有使用 JavaScript 加载一些片段 dynamically/asynchronously 吗?