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 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之1075大数阶乘
题目 解决代码及点评 /************************************************************************/ /* ...
- 在命令行获取标准输入序列的反互序列,pep序列和长度信息
近期对序列文件处理的比較多,时常要看一些核酸序列的反向互补序列,长度.可能的翻译序列. 曾经我常用seqBuider 来查看.假设能在命令行直接查看.想必是极好的. 这是一个perl脚本.只是我把它的 ...
- 关于WCF在IIS8注册的问题
原文 http://social.microsoft.com/Forums/id-ID/30cf8a24-2719-4c1c-b035-3d186fbfc09c/wcfiis8?forum=wcfzh ...
- JQuery 插件之Ajax Autocomplete(ajax自动完成)
平时用百度,谷歌搜索的时候 会有一个下 拉列表进行提示 这是一个非常好的功能 本文要介绍的这个JQuery 插件 名叫Ajax Autocomplete 顾名思义 ajax 也就是用ajax的方式获取 ...
- BestR #31
hdu 5178 求|a[i] - a[j]| <= k (i < j) <i,j>的对数,一开始认为数据不大就直接ans++了,后来结果出来才知道,啊啊啊,too young ...
- Python标准库:内置函数dict(iterable, **kwarg)
本函数是从可迭代对象来创建新字典.比方一个元组组成的列表,或者一个字典对象. 样例: #dict() #以键对方式构造字典 d1 = dict(one = 1, two = 2, a = 3) pri ...
- 纯后端尝试写一个前端slide插件
概述 由于项目组前端人员缺失,又赶上需要在手机端做一个slide效果的页面,所以只能自己硬着头皮上了,写的很简单,请大家不要笑话,只是拿出来分享下,大家先看下完成后的效果,如下: 过程 看了效果图是不 ...
- Android 系统搜索框(有浏览记录)
实现Android 系统搜索框(有浏览记录),先看下效果: 一.配置搜索描述文件 要在res中的xml文件加创建sreachable.xml,内容如下: <?xml version=" ...
- POJ 1862 & ZOJ 1543 Stripies(贪心 | 优先队列)
题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- 站在OC的基础上快速理解Swift的类与结构体
阅读此文章前,您已经有一定的Object-C语法基础了!) 2014年,Apple推出了Swift,最近开始应用到实际的项目中. 首先我发现在编写Swift代码的时候,经常会遇到Xcode不能提示,卡 ...