Java学习之道:空指针错误求解救????????????

运行下面这个主方法红色代码的地方就报如下错:

https://api.weibo.com/2/users/show.json?access_token=2.00szM9cCc4R_EC6ebcf15062iQbxjD&uid=3237856983
{"id":3237856983,"idstr":"3237856983","class":1,"screen_name":"中银消费金融新易贷","name":"中银消费金融新易贷","province":"31","city":"1","location":"上海 黄浦区","description":"中银消费金融有限公司是经中国银监会批准的全国首批试点消费金融公司,也是上海第一家消费金融公司。由中国银行、百联集团及上海陆家嘴金融发展有...","url":"","profile_image_url":"http://tp4.sinaimg.cn/3237856983/50/40021817994/1","profile_url":"u/3237856983","domain":"","weihao":"","gender":"m","followers_count":11121,"friends_count":1534,"statuses_count":692,"favourites_count":1,"created_at":"Wed Mar 27 11:26:06 +0800 2013","following":false,"allow_all_act_msg":false,"geo_enabled":true,"verified":true,"verified_type":2,"remark":"","status":{"created_at":"Tue Aug 27 15:30:09 +0800 2013","id":3615931068822420,"mid":"3615931068822420","idstr":"3615931068822420","text":"#易生活#【室内除味小锦囊】在日常生活中,无论是在厨房还是卫生间总会或多或少产生一些奇怪的异味,影响我们的心情。所以,小易今天就教大家几招轻松去异味的方法,赶紧学习一下吧!房子里面有异味的时候就可以见招拆招咯!","source":"<a href=\"http://app.weibo.com/t/feed/33p22E\" rel=\"nofollow\">DSRM</a>","favorited":false,"truncated":false,"in_reply_to_status_id":"","in_reply_to_user_id":"","in_reply_to_screen_name":"","pic_urls":[{"thumbnail_pic":"http://ww4.sinaimg.cn/thumbnail/c0fdc6d7jw1e81923kty9j20c80yote5.jpg"}],"thumbnail_pic":"http://ww4.sinaimg.cn/thumbnail/c0fdc6d7jw1e81923kty9j20c80yote5.jpg","bmiddle_pic":"http://ww4.sinaimg.cn/bmiddle/c0fdc6d7jw1e81923kty9j20c80yote5.jpg","original_pic":"http://ww4.sinaimg.cn/large/c0fdc6d7jw1e81923kty9j20c80yote5.jpg","geo":null,"reposts_count":0,"comments_count":0,"attitudes_count":0,"mlevel":0,"visible":{"type":0,"list_id":0}},"ptype":0,"allow_all_comment":true,"avatar_large":"http://tp4.sinaimg.cn/3237856983/180/40021817994/1","avatar_hd":"http://tp4.sinaimg.cn/3237856983/180/40021817994/1","verified_reason":"","follow_me":true,"online_status":1,"bi_followers_count":548,"lang":"zh-cn","star":0,"mbtype":0,"mbrank":0,"block_word":0}
{"remark":"","location":"上海 黄浦区","verified_type":2,"verified_reason":"","statuses_count":692,"lang":"zh-cn","city":"1","id":3237856983,"following":false,"favourites_count":1,"idstr":"3237856983","description":"中银消费金融有限公司是经中国银监会批准的全国首批试点消费金融公司,也是上海第一家消费金融公司。由中国银行、百联集团及上海陆家嘴金融发展有...","verified":true,"name":"中银消费金融新易贷","domain":"","province":"31","gender":"m","created_at":"Wed Mar 27 11:26:06 +0800 2013","weihao":"","followers_count":11121,"online_status":1,"profile_url":"u/3237856983","bi_followers_count":548,"status":{"comments_count":0,"text":"#易生活#【室内除味小锦囊】在日常生活中,无论是在厨房还是卫生间总会或多或少产生一些奇怪的异味,影响我们的心情。所以,小易今天就教大家几招轻松去异味的方法,赶紧学习一下吧!房子里面有异味的时候就可以见招拆招咯!","pic_urls":[{"thumbnail_pic":"http://ww4.sinaimg.cn/thumbnail/c0fdc6d7jw1e81923kty9j20c80yote5.jpg"}],"geo":null,"visible":{"type":0,"list_id":0},"attitudes_count":0,"in_reply_to_screen_name":"","mlevel":0,"truncated":false,"bmiddle_pic":"http://ww4.sinaimg.cn/bmiddle/c0fdc6d7jw1e81923kty9j20c80yote5.jpg","id":3615931068822420,"thumbnail_pic":"http://ww4.sinaimg.cn/thumbnail/c0fdc6d7jw1e81923kty9j20c80yote5.jpg","idstr":"3615931068822420","source":"<a href=\"http://app.weibo.com/t/feed/33p22E\" rel=\"nofollow\">DSRM<\/a>","original_pic":"http://ww4.sinaimg.cn/large/c0fdc6d7jw1e81923kty9j20c80yote5.jpg","favorited":false,"in_reply_to_status_id":"","reposts_count":0,"created_at":"Tue Aug 27 15:30:09 +0800 2013","in_reply_to_user_id":"","mid":"3615931068822420"},"geo_enabled":true,"star":0,"class":1,"mbrank":0,"avatar_hd":"http://tp4.sinaimg.cn/3237856983/180/40021817994/1","allow_all_comment":true,"allow_all_act_msg":false,"avatar_large":"http://tp4.sinaimg.cn/3237856983/180/40021817994/1","url":"","friends_count":1534,"mbtype":0,"screen_name":"中银消费金融新易贷","block_word":0,"follow_me":true,"profile_image_url":"http://tp4.sinaimg.cn/3237856983/50/40021817994/1","ptype":0}
java.lang.NullPointerException
at com.zy.test.Main.downloadStatuses(Main.java:88)
at com.zy.test.Main.main(Main.java:49)
java.lang.NullPointerException
at com.zy.test.Main.downloadStatuses(Main.java:88)
at com.zy.test.Main.main(Main.java:49)
java.lang.NullPointerException
at com.zy.test.Main.downloadStatuses(Main.java:88)
at com.zy.test.Main.main(Main.java:49)
java.lang.NullPointerException
at com.zy.test.Main.downloadStatuses(Main.java:88)
at com.zy.test.Main.main(Main.java:49)
java.lang.NullPointerException
at com.zy.test.Main.downloadStatuses(Main.java:88)
at com.zy.test.Main.main(Main.java:49)
java.lang.NullPointerException
at com.zy.test.Main.downloadStatuses(Main.java:88)
at com.zy.test.Main.main(Main.java:49)
java.lang.NullPointerException
at com.zy.test.Main.downloadStatuses(Main.java:88)
at com.zy.test.Main.main(Main.java:49)
java.lang.NullPointerException
at com.zy.test.Main.downloadFans(Main.java:227)
at com.zy.test.Main.main(Main.java:58)
{"error":"source paramter(appkey) is missing","error_code":10006,"request":"/2/friendships/followers.json"}
{"error":"source paramter(appkey) is missing","error_code":10006,"request":"/2/friendships/followers.json"}
java.lang.NullPointerException
at com.zy.test.Main.downloadFans(Main.java:227)
at com.zy.test.Main.main(Main.java:58)
{"error":"source paramter(appkey) is missing","error_code":10006,"request":"/2/friendships/followers.json"}
java.lang.NullPointerException
at com.zy.test.Main.downloadFans(Main.java:227)
at com.zy.test.Main.main(Main.java:58)


package com.zy.test;



import java.net.HttpURLConnection;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Locale;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.zy.dao.ZYDAO;
import com.zy.util.DateUtil;
import com.zy.util.HttpUtil;
import com.zy.util.TokenUtil;


public class Main {
public static void main(String[] args) {
//3237856983
//token:2.00TOS_VCWUnlBEee2df0f1b8IIlMUD

try {
// String token="2.00TOS_VC0p8duY2fbaf03a5aBHdPCC";
String uid="3237856983";

StringBuffer sb=new StringBuffer("https://api.weibo.com/2/users/show.json");
sb.append("?access_token=").append(TokenUtil.getToken());
sb.append("&uid=").append(uid);


System.out.println(sb.toString());

String body=HttpUtil.request(sb.toString(), "GET", null, null, null);
System.out.println(body);

JSONObject obj=new JSONObject(body);
System.out.println(obj);

/*******************************抓取微博*************************************/
Integer size=obj.getInt("statuses_count");//微博条数

Integer count=100;//每页显示长度

Integer page = size % count == 0 ? size / count : size / count + 1;//页数

for (int i = 1; i <= page; i++) {
downloadStatuses(TokenUtil.getToken(), uid, count, i);
}


/**************************获取用户的粉丝列表********************************/
Integer fsize=obj.getInt("followers_count");//粉丝数
Integer fcount=200;//每页显示长度
Integer fpage = fsize % fcount == 0 ? fsize / fcount : fsize / fcount + 1;
Integer cursor=0;
for (int i = 0; i <= fpage; i++) {
cursor=downloadFans(TokenUtil.getToken(), uid, fcount, cursor);
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 获取微博信息
*/
public static void downloadStatuses(String token,String uid,Integer count,Integer page){
try {
//1. 获取微薄
//2. 获取评论
//3. 获取粉丝
//4. 转帖列表 

StringBuffer url=new StringBuffer("https://api.weibo.com/2/statuses/user_timeline.json?");
url.append("access_token=").append(token);//=2.00TOS_VCWUnlBEee2df0f1b8IIlMUD");
url.append("&uid=").append(uid);//3237856983");
url.append("&count=").append(count);
url.append("&page=").append(page);

// String param="2.00TOS_VCWUnlBEee2df0f1b8IIlMUD";

String body=HttpUtil.request(url.toString(), "GET", null, null, null);

JSONObject obj=new JSONObject(body);
JSONArray ary=obj.getJSONArray("statuses");

for (int i = 0; i < ary.length(); i++) {
JSONObject item=ary.getJSONObject(i);
HashMap<String, Object> parms=jSONObjectToHashMap(item);
HashMap<String, Object> user=jSONObjectToHashMap(item.getJSONObject("user"));


ZYDAO.saveOrUpdateUser(user);
parms.put("user_id", user.get("id"));
ZYDAO.saveOrUpdateStatuse(parms);

/**********************************************************/
Integer size=item.getInt("comments_count");//评论数
Integer cmt=50;
Integer pg = size % cmt == 0 ? size / cmt : size / cmt + 1;

for (int j = 1; j <= pg; j++) {
downloadComments(token,item.getString("id"),cmt,j);
}

/**********************************************************/
Integer repSize=item.getInt("reposts_count"); //转发数

Integer rcount=200;
Integer rpg = repSize % rcount == 0 ? repSize / rcount : repSize / rcount + 1;
for (int j = 1; j <= rpg; j++) {
downloadForwordStatuses(token,item.getString("id"),rcount,j);
}

}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 获取转发微博信息
*/
public static void downloadForwordStatuses(String token,String id,Integer count,Integer page){
try {

StringBuffer url=new StringBuffer("https://api.weibo.com/2/statuses/repost_timeline.json?");
url.append("access_token=").append(token);//=2.00TOS_VCWUnlBEee2df0f1b8IIlMUD");
url.append("&id=").append(id);//3237856983");
url.append("&count=").append(count);
url.append("&page=").append(page);

// String param="2.00TOS_VCWUnlBEee2df0f1b8IIlMUD";

String body=HttpUtil.request(url.toString(), "GET", null, null, null);

JSONObject obj=new JSONObject(body);
// System.out.println(obj);

JSONArray ary=obj.getJSONArray("reposts");

for (int i = 0; i < ary.length(); i++) {
JSONObject item=ary.getJSONObject(i);
HashMap<String, Object> parms=jSONObjectToHashMap(item);
HashMap<String, Object> user=jSONObjectToHashMap(item.getJSONObject("user"));

ZYDAO.saveOrUpdateUser(user);
parms.put("user_id", user.get("id"));
ZYDAO.saveOrUpdateStatuse(parms);

Integer size=item.getInt("comments_count");//评论数
Integer cmt=50;
Integer pg = size % cmt == 0 ? size / cmt : size / cmt + 1;

for (int j = 1; j <= pg; j++) {
downloadComments(token,item.getString("id"),cmt,j);
}
}

} catch (Exception e) {

e.printStackTrace();
}
}

/**
* 获取评论信息
*/
public static void downloadComments(String token,String id,Integer count,Integer page){
try {

StringBuffer url=new StringBuffer("https://api.weibo.com/2/comments/show.json");
url.append("?access_token=").append(token);//=2.00TOS_VCWUnlBEee2df0f1b8IIlMUD");
url.append("&id=").append(id);//3237856983");
url.append("&count=").append(count);
url.append("&page=").append(page);

// String param="2.00TOS_VCWUnlBEee2df0f1b8IIlMUD";


String body=HttpUtil.request(url.toString(), "GET", null, null, null);

JSONObject obj=new JSONObject(body);
JSONArray ary=obj.getJSONArray("comments");


for (int i = 0; i < ary.length(); i++) {
JSONObject item=ary.getJSONObject(i);
HashMap<String, Object> parms=jSONObjectToHashMap(item);
parms.put("reply_status_id", item.getJSONObject("status").get("id"));

JSONObject user=item.getJSONObject("user");

HashMap<String, Object> uparm=jSONObjectToHashMap(user);

ZYDAO.saveOrUpdateUser(uparm);//插入用户
parms.put("user_id", user.get("id"));
ZYDAO.saveOrUpdateComments(parms);//插入评论
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 获取用户的粉丝列表
* @param token :
* @param uid :
* @param count :
* @param page :
*/
public static Integer downloadFans(String token,String uid,Integer count,Integer page){
Integer cursor=null;
try {
StringBuffer url=new StringBuffer("https://api.weibo.com/2/friendships/followers.json");
url.append("?access_token=").append(token);//=2.00TOS_VCWUnlBEee2df0f1b8IIlMUD");
url.append("&uid=").append(uid);//3237856983");
url.append("&count=").append(count);
url.append("&cursor=").append(page);
url.append("&trim_status=0");

String body=HttpUtil.request(url.toString(), "GET", null, null, null);
System.out.println(body);
JSONObject obj=new JSONObject(body);
cursor=obj.getInt("next_cursor");

JSONArray ary=obj.getJSONArray("users");


for (int i = 0; i < ary.length(); i++) {

JSONObject user=ary.getJSONObject(i);

HashMap<String, Object> uparm=jSONObjectToHashMap(user);
System.out.println(user);
if(user.getJSONObject("status")!=null){
String cat=user.getJSONObject("status").getString("created_at");
uparm.put("last_status_create_at",DateUtil.parsetmp(cat, "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US) );
}
//frends_count
ZYDAO.saveOrUpdateUser(uparm);//插入用户
HashMap<String, Object> fans=new HashMap<String, Object>();
fans.put("weibo", uid);
fans.put("fans", uparm.get("id"));
ZYDAO.saveOrUpdateFans(fans);//插入粉丝
}
} catch (Exception e) {
e.printStackTrace();
}
return cursor;
}


/**
* 转换类型
* @param obj
* @return
* @throws JSONException
* @throws ParseException
*/
public static HashMap<String, Object> jSONObjectToHashMap(JSONObject obj) throws JSONException, ParseException{
HashMap<String, Object> parms=new HashMap<String, Object>();
Iterator<String> keys=obj.keys();
while(keys.hasNext()){
String key=keys.next();


Object value=obj.get(key);
if("created_at".equals(key)){
String date=value.toString();
value=DateUtil.parsetmp(date, "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
}
parms.put(key, value);
}

if(parms.get("retweeted_status")==null){
//原帖
parms.put("type", "0");
}else{
//转帖
parms.put("type", "1");
JSONObject ret=obj.optJSONObject("retweeted_status");
parms.put("retweeted_status_id", ret.get("id"));
}

return parms;
}
}

你可能感兴趣的:(Java学习)