Twitter api& GUI

时间:2017-10-16 17:28:30

标签: java twitter

我正在尝试创建一个GUI,它使用twitters API来搜索使用关键字的推文,然后显示推文中使用的顶部主题标签。我得到了一个" AWT-EventQueue-0"每次运行程序时都会出现java.lang.NullPointerException 。我使用我创建的主题标签类型来保存主题标签中的文本以及它使用了多少次。我真的很感激一些帮助。出于隐私原因,我删除了Twitter消费者密钥和令牌。

package twittersearch;

import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.ActionListener;
import static java.lang.Boolean.FALSE; 
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.HashtagEntity;

public class TwitterSearch extends JFrame implements ActionListener{

static Panel panel;
static Label label, label1, label2;
static Button button, button1, button2;
static TextField text, text1, text2, text3, text4, text5, text6, text7, text8, 
text9, text10, text11;
protected ArrayList <Status> returnedTweets = new ArrayList<>();

private static final String TWITTER_CONSUMER_KEY = 
"*************";
private static final String TWITTER_SECRET_KEY = 
"*********************************************";
private static final String TWITTER_ACCESS_TOKEN = "******************";
private static final String TWITTER_ACCESS_TOKEN_SECRET = 
"********************************";

public ArrayList <hashtag> hashtags = new ArrayList<>();

public TwitterSearch() {
    setTitle("Twitter Search");
    setSize(1250, 400);
    setLocation(10, 200);

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setLayout(new FlowLayout());

    label = new Label("Enter Keyword:");
    add(label);

    text = new TextField(15);
    add(text);

    button = new Button("SUBMIT");
    add(button);
    button.addActionListener(this);

    label1 = new Label("Results:");
    add(label1);

    text1 = new TextField(95);
    text1.setEditable(FALSE);
    add(text1);

    button1 = new Button("SAVE");
    add(button1);
    button1.addActionListener(this);

    label2 = new Label("Top Hashtags:");
    add(label2);

    text2 = new TextField(70) ;
    text2.setText("1: ");
    text2.setEditable(FALSE);
    add(text2);

    text3 = new TextField(70) ;
    text3.setText("2: ");
    text3.setEditable(FALSE);
    add(text3);

    text4 = new TextField(70) ;
    text4.setText("3: ");
    text4.setEditable(FALSE);
    add(text4);

    text5 = new TextField(70) ;
    text5.setText("4: ");
    text5.setEditable(FALSE);
    add(text5);

    text6 = new TextField(70) ;
    text6.setText("5: ");
    text6.setEditable(FALSE);
    add(text6);

    text7 = new TextField(70) ;
    text7.setText("6: ");
    text7.setEditable(FALSE);
    add(text7);

    text8 = new TextField(70) ;
    text8.setText("7: ");
    text8.setEditable(FALSE);
    add(text8);

    text9 = new TextField(70) ;
    text9.setText("8: ");
    text9.setEditable(FALSE);
    add(text9);

    text10 = new TextField(70) ;
    text10.setText("9: ");
    text10.setEditable(FALSE);
    add(text10);

    text11 = new TextField(70) ;
    text11.setText("10: ");
    text11.setEditable(FALSE);
    add(text11);

    button2 = new Button("New Search");
    add(button2);
    button2.addActionListener(this);

    setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    String display = null;
    HashtagEntity[] hashtagInTweet = new HashtagEntity[15];

    if(source == button){   //SUBMIT button action

        try {

            returnedTweets = search(text.getText());
            for(Status theTweet: returnedTweets){
                display += theTweet.getText() + "\t";
            }

            text1.setText(display);

        } catch (TwitterException ex) {
java.util.logging.Logger.getLogger(TwitterSearch.class.getName()).log(Level.SEVERE, null, ex);
        }

        text.setText("SUBMITTED");
    }
    else if(source == button1){  //SAVE button action
        text1.setText("SAVED");

        for(Status theStatus: returnedTweets){
            if(theStatus.getText().contains("#")){
                hashtagInTweet = theStatus.getHashtagEntities();

            }

            for (HashtagEntity hashtagInTweet1 : hashtagInTweet) {
                hashtag temp = new hashtag(hashtagInTweet1.getText());

                for (hashtag hashtag : hashtags) {
                    if(hashtag.equals(temp)){
                        hashtag.count++;
                    }else{
                        hashtags.add(temp);
                    }
                } 
            }
        }            
        sort();            
        text2.setText(hashtags.get(1).toString());

    }else if(source == button2){    //New Search action
        text.setText("");
        text1.setText("");
        returnedTweets.clear();
    }

}

public static void main(String[] args) {
    new TwitterSearch();

}


public ArrayList search(String keyword) throws TwitterException{

    System.setProperty("java.net.useSystemProxies", "true");

    ArrayList<Status> returnList = new ArrayList<>();

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
    .setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
    .setOAuthConsumerSecret(TWITTER_SECRET_KEY)
    .setOAuthAccessToken(TWITTER_ACCESS_TOKEN)
    .setOAuthAccessTokenSecret(TWITTER_ACCESS_TOKEN_SECRET);

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

    List<Status> statuses;
    statuses = twitter.getHomeTimeline();

    for(Status status : statuses){
        if(status.getText().contains(keyword)){

            returnList.add(status);


        }
    }

    return returnList;

}


public void sort(){
    int n = hashtags.size();
    hashtag temp;

    for(int i = 1; i <n; i++){
        if(hashtags.get(i - 1).count > hashtags.get(i).count){
            temp = hashtags.get(i-1);
            hashtags.set(i-1, hashtags.get(i));
            hashtags.set(i, temp);
        }

    }

    for(hashtag hashtag: hashtags){
        System.out.println(hashtag.toString());
    }
}

}

这是我得到的错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at twittersearch.TwitterSearch.actionPerformed(TwitterSearch.java:179)
at java.awt.Button.processActionEvent(Button.java:409)
at java.awt.Button.processEvent(Button.java:377)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at 


java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

0 个答案:

没有答案