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学习之道:空指针错误求解救????????????的更多相关文章
- 摘自:java夜未眠之java学习之道
目前Java可以说是产业界和学术界最热门的语言,许多读者都很急切想把Java学好.除非是武侠小说中的运功传送内力的方式,否则花上一段时间苦学是免不了的.花时间,不打紧,就是怕方法错误,事倍功半.我认为 ...
- Java学习之道:Java中十个常见的违规编码
近期,我给Java项目做了一次代码清理工作.经过清理后,我发现一组常见的违规代码(指不规范的代码并不表示代码错误)反复出如今代码中.因此,我把常见的这些违规编码总结成一份列表,分享给大家以帮助Java ...
- Java学习之道:Java项目打包发布
Java项目打包发布 如果只想发布为一个可执行的jar包,使用eclipse的Export功能就可以了 使用eclipse的Export功能,将项目中的所有package打包为一个pet.jar文件, ...
- Java学习之道:jdk环境变量配置方法
JDK(Java Development Kit)是整个Java的核心,包含了Java执行环境.Java工具和Java基础类库.JDK作为JAVA开发的环境,无论是做JAVA开发还是做安卓开发,都必须 ...
- Java学习之道:Java 导出EXCEL
1.Apache POI简单介绍 Apache POI是Apache软件基金会的开放源代码函式库.POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开 ...
- Java学习之道:Java操作Excel之导出下载
页面放置一个button进行点击导出事件 <h:commandLink target="_parent" value="导出" ac ...
- 空指针错误 java.lang.NullPointerException
使用基本的JAVA数据类型,变量的值要么已经是默认值,如果没有对其正常赋值,程序便 不能通过编译,因此使用基本的JAVA数据类型(double,float,boolean,char,int,long) ...
- 《Java学习笔记(第8版)》学习指导
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
- Java学习笔记4
Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...
随机推荐
- Sencha Touch 2 在MAC下详细的开发流程
在不久的将来我相信Web App会流行的非常广, 能看到未来才能主宰未来.对于我们开发人员来说我觉得想成就一件伟大的事情,需要过硬的技术和好的想法,再加上决不放弃的精神,一定可以成功的. 以下在Mac ...
- js验证日期
寻寻觅觅,Web开发里,对日期的验证太多了,网上好多是用正则表达式来验证,但是这种验证也只能验证格式,没办法验证有效性,比如平年(2月28天)和闰年(2月29天).平时用得多,以前经常用一次写一次,腻 ...
- C#分析URL参数获取参数和值得对应列表
原文: C#分析URL参数获取参数和值得对应列表 /// <summary> /// 分析url链接,返回参数集合 /// </summary> /// <param n ...
- Spring MVC Controller与jquery ajax请求处理json
在用 spring mvc 写应用的时候发现jquery传递的[json数组对象]参数后台接收不到,多订单的处理,ajax请求: "}]}]} $.ajax({ url : url, typ ...
- 百度地图 javascript相关Bug搜集
一 在手机里用百度地图js版做webapp bug集合 1 之前用2.0版本的时候发现只要地图添加了覆盖物,无论数量多少,当地图放大到很小的范围时候,会卡死 1.1 当时处理办法:将版本降低至1. ...
- Swift - 基本数据类型,及常/变量声明
2015-01-08 14:59 发布:yuhang 浏览:434 下面是Swift中基本的数据类型介绍说明: 1,变量:使用var声明 1 var str:String = "hangge ...
- android binder机制之——(创建binder服务)
Binder机制编程 前面的几篇文章具体介绍了android中binder机制的方方面面,相信你对binder机制已经有了较深刻的理解.俗话说得好"学以致用",以下我们就通过在 ...
- 网络编程API-上 (基本API)
htons.ntohs.htonl和ntohl函数 Linux提供了4个函数来完毕主机字节序和网络字节序之间的转换 #include <netinet/in.h> uint16_t hto ...
- MySQL如何修改root密码
MySQL修改用户密码 因为长期不登录MySQL数据库,登录时经常忘记root权限密码.本文提供一个在数据库服务器上修改root密码的方法,本文撰写基础是在xp操作系统下进行. 第一步 ...
- go编程基础
可见性规则: go语言,根据函数名首字母大小写区分private,和pulic. 函数名首字母小写为private 函数名首字母大写为public