thread报空指针异常,请教给我大神解决方法

public class NewsGetThread extends Thread {
private String url;
private Handler handler;
public static final String KEY_HTTP_ACK="KEY_HTTP_ACK";
public NewsGetThread(String url, Handler handler) {
this.url=url;
this.handler=handler;
}

@Override
public void run() {

    try {
        //获取网页文档
        String urlBase="http://tw.wzu.edu.cn";
        Document doc = Jsoup.connect(url).timeout(10000).get();
        Elements els = doc.getElementsByAttributeValue("class","box_right_main");//解析数据
        Element el = els.get(0);
        Elements lis = el.select("li");
        ArrayList<NewsItem> newslist = new ArrayList<>();
        for (int i=0;i<lis.size();i++){
            //根据html标签获取需要的内容
            Element li = lis.get(i);
            Element el_a = li.select("a").get(0);
            //标题及其对应的链接
            String title = el_a.text();
            String href0= el_a.attr("href");
            String href = urlBase+href0.substring(5);
            //发布时间
            Element span = li.select("span").get(0);
            String pubTime = span.text();
            Log.d("test",pubTime);
            Log.d("test",href);
            Log.d("test",href0);
            Log.d("test",title);

            NewsItem newsItem = new NewsItem(pubTime,title,href);
            newslist.add(newsItem);
        }
        String str = el.toString();
        Message msg = handler.obtainMessage();
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_HTTP_ACK,newslist);

        msg.setData(bundle);
        handler.sendMessage(msg);
    } catch (IOException e) {
        e.printStackTrace();

    }

}

}

12-19 11:47:20.797 9563-9646/com.example.yangjie.yj_final_work E/AndroidRuntime: FATAL EXCEPTION: Thread-175
Process: com.example.yangjie.yj_final_work, PID: 9563
java.lang.NullPointerException
at com.example.yangjie.yj_final_work.NewsGetThread.run(NewsGetThread.java:60)

3个回答

你把调用的位置发一下,哪里new的这个thread

weixin_44154472
weixin_44154472 public class MainActivity extends AppCompatActivity { ListView lv_title; Handler handler; String url="http://tw.wzu.edu.cn/xbwzlm/twgk/ggl.htm"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String[] titles = new String[]{"新闻公告","资源动态","培训讲座","新生专栏","经典阅读"}; lv_title = (ListView) findViewById(R.id.listView); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,titles); lv_title.setAdapter(adapter); lv_title.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { new NewsGetThread(url,handler).start(); } }); } }
接近 2 年之前 回复

报错误的那行是哪一行 ,标注出来啊

weixin_42841968
你用debug 查一下应该是你的handler没有初始化吧。。
接近 2 年之前 回复
weixin_44154472
weixin_44154472 Message msg = handler.obtainMessage();
接近 2 年之前 回复

空指针是最好解决一个bug,直接debug,看看对象就成了,没啥技术含量或者打印一下对象地址

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐