我必须得到与搜索查询相关的推文。所以我使用的是twitter4j 2.2.5。当我输入要搜索的单词时会出现错误。
我必须在twitter中访问推文,我将它存储在数据库中,我将在java中获得感情分析。
public class TweetOperator {
private static final String consumerKey = "XXXXXXXXXXXXXXXXxxxxxxxxxxx";
private static final String consumerSecret = "XXXXXXXXXXXXXXXXxxxxxxxxxxx";;
private static final String tokenKey = "XXXXXXXXXXXXXXXXxxxxxxxxxxx";;
private static final String tokenSecret = "XXXXXXXXXXXXXXXXxxxxxxxxxxx";;
Twitter twitter;
public TweetOperator(int mode) {
try {
db = Operator.getInstance();
} catch(Exception ex) {
ex.printStackTrace();
}
this.mode = mode;
AccessToken token = new AccessToken(tokenKey, tokenSecret);
twitter.setOAuthAccessToken(token);// Exception occurs here
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
}
@Override
protected void finalize() throws Throwable{
super.finalize();
}
}
堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sentimentanalysis.twitterapi.TweetOperator.<init>(TweetOperator.java:57)
at sentimentanalysis.search.Search.<init>(Search.java:34)
at sentimentanalysis.view.SentimentAnalysisView.searchSubmitActionPerformed(SentimentAnalysisView.java:249)
at sentimentanalysis.view.SentimentAnalysisView.access$200(SentimentAnalysisView.java:22)
at sentimentanalysis.view.SentimentAnalysisView$3.actionPerformed(SentimentAnalysisView.java:135)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
答案 0 :(得分:4)
您在致电twitter
后,将new TwitterFactory().getInstance()
分配给twitter.setOAuthAccessToken
。 twitter
实例变量初始化为null,您将获得Null指针异常。
更改换行符应该有效:
twitter = new TwitterFactory().getInstance();
twitter.setOAuthAccessToken(token);
答案 1 :(得分:2)
最近Twitter已将其API升级为仅接受SSL请求。但TwitterFactory默认禁用SSL。
添加 Nigel Tufnel 代码,更新代码如下
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setUseSSL(true);
TwitterFactory factory = new TwitterFactory(builder.build());
Twitter twitter = factory.getInstance();
Tweet是一个被诽谤的人。状态是我们需要使用的新界面。请查看下面的示例程序。
package twitter;
import java.util.List;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.conf.ConfigurationBuilder;
public class XYZ {
public static void main(String[] args) {
String oauth_consumer_key="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
String oauth_token="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
String oauth_consumer_secret="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
String oauth_token_secret ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setUseSSL(true);
TwitterFactory factory = new TwitterFactory(builder.build());
Twitter twitter = factory.getInstance();
twitter.setOAuthConsumer(oauth_consumer_key, oauth_consumer_secret);
AccessToken accessToken = new AccessToken(oauth_token, oauth_token_secret);
twitter.setOAuthAccessToken(accessToken);
Query arg0 = new Query("query");
try {
QueryResult queryResult = twitter.search(arg0 );
System.out.println(queryResult.getCount());
List<Status> statuses = queryResult.getTweets();
for(Status status:statuses){
System.out.println(status.getText());
}
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 2 :(得分:0)
在
中编写代码new Thread(new Runnable() {
@Override
public void run() {
}
}).start();