java解析文件jsoup报错java.lang.NullPointerException,求解决方案!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java解析文件jsoup报错java.lang.NullPointerException,求解决方案!相关的知识,希望对你有一定的参考价值。

public class JsoupAn

public static void main(String[] args) throws Exception

String tag = null;
String time = null;
String detal = null;
String local = null;
String col = null;
String lng = null;
String lat = null;
String type=null;
String floorAll=null;
String d=null;
String direct=null;
//
List<String> list = new ArrayList<String>();

File file = new File("D:\\66960\\house_parser\\");

File[] files = file.listFiles();
FileOutputStream fos = new FileOutputStream("D:\\66960\\house.txt");
OutputStreamWriter os = new OutputStreamWriter(fos);
//BufferedWriter bw=new BufferedWriter(os);
os.write("time,houseType,area,director,floorAll,district,local,age,type,degree,price,downPayment,lng,lat\n");//输出表头

for (File fi : files)
// System.out.println(fi);
// List<String> list1=IOUtils.readLines(new FileInputStream(fi));
// System.out.println(fi.getAbsoluteFile());
Document doc = Jsoup.parse(new File(fi.getAbsolutePath()), null);

tag = doc.getElementsByTag("title").text();

Elements clearfix = doc.getElementsByClass("wrapper-lf clearfix");
Elements item3 = doc.getElementsByClass("block-wrap block-nocopy no-bd-top");

time = doc.getElementsByClass("house-encode").text();
String date=time.substring(time.length()-11);
Elements secondCol = doc.getElementsByClass("second-col detail-col");

for (Element e : secondCol)
detal = e.children().text();
type=detal.replace("房型:", " ").replaceAll("朝向:", ",").replace("面积:", ",").replace("楼层:", ",");

//type=detal.substring(4, 10);
floorAll=detal.substring(detal.length()-8);
//String d=detal.split(" ")[0];

Elements firstCol = doc.getElementsByClass("first-col detail-col");
local = firstCol.text().replace("小区:", ",").replace("位置:", ",").replace("年代:", ",").replace("类型:", ",");

Elements thirdCol = doc.getElementsByClass("third-col detail-col");
col = thirdCol.text().replace("装修程度:", ",").replaceAll("房屋单价", ",").replace("参考首付:", ",").replace(" 参考月供: ", ",");

List<String> str = IOUtils.readLines(new FileInputStream(fi));
for (String lines : str)
if (lines.contains("lng :"))
lng = lines.trim().replace("lng :", ",").replaceAll("\"", "");

if (lines.contains("lat :"))
lat = lines.trim().replace("lat :", ",").replaceAll("\"", "");

// lat : "39.716945",
// lng : "116.411846"


运行时提示Exception in thread "main" java.lang.NullPointerException,我知道这是空指针错误,但是该怎么修改才能解决呢?由于代码过长,所以删除了部分代码,错误行为for (File fi : files)
错误行在//输出表头后for (File fi : files)

那只能是files是一个NullPointer,请检查listFiles返回值 参考技术A files集合为空。

对象逆序列化报错:java.lang.ClassNotFoundException

简单的想从保存的对象中又一次解析出对象。用了逆序列化,但是报错:

java.lang.ClassNotFoundException: xxxxxxxxxxxx
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:622)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1593)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at xxxxxxxxxxxxxxxxx(TestMetadata.java:103)


提示类找不到。但实际上类文件是确确实实存在的。那就上网搜,果然找到答案。

能够參考文章: http://www.javapractices.com/topic/TopicAction.do?Id=45

最主要的两点:

1) 须要相同的包名

2) 相同的序列化ID




















以上是关于java解析文件jsoup报错java.lang.NullPointerException,求解决方案!的主要内容,如果未能解决你的问题,请参考以下文章

Jsoup错误java.lang.NoClassDefFoundError: org.jsoup.Jsoup

jsoup解析页面报错

docker容器中解决java使用ffmpeg解析视频文字内容报错java.lang.UnsatisfiedLinkError:libxcb.so.1

java lang IllegalArgumentException MALFORMED jar解析中文报错问题

idea连接spark集群报错解析:Caused by: java.lang.ClassCastException

对象逆序列化报错:java.lang.ClassNotFoundException