Edit:
When I create an instance of TwitterModel
wouldn't the main method be executed and then statuses
be initialized? So I should be getting the value of statuses
when I call getUsertimeline()
?
I'm trying to get statuses
in TwitterPanel
after setting it in the main method of TwitterModel
. However, I'm getting NullPointerException
when executing:
for (Status status : tm.getUserTimeline()) {
twitterFeed1.setText("@" + status.getUser().getScreenName() + " - "
+ status.getText());
}
MainFrame.java
import java.awt.CardLayout;
public class MainFrame extends JFrame {
static CardLayout cardLayout;
static JPanel cards;
static ResponseList<Status> statuses;
public MainFrame() {
TwitterPanel tp = new TwitterPanel();
cards = new JPanel();
cardLayout = new CardLayout();
cards.setLayout(cardLayout); // Setting JPanel, cards, as CardLayout
// JFrame settings
add(cards); // Adding JPanel, cards to JFrame
// setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
setLayout(new FlowLayout());
setTitle("Demo");
setSize(1000, 500);
setVisible(true);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cards.add(tp, "tp");
cardLayout.show(cards, "tp");
}
public static void main(String[] args) throws TwitterException {
MainFrame asd = new MainFrame();
}
TwitterPanel.java (NPE happens here)
import javax.swing.JPanel;
public class TwitterPanel extends JPanel {
// Global variables
static TwitterModel tm = new TwitterModel();
public TwitterPanel() {
// JPanel layout
setBorder(new LineBorder(new Color(0, 0, 0)));
setLayout(null);
// JLabel
JLabel twitterLabel = new JLabel("Twitter");
twitterLabel.setBounds(208, 0, 58, 30);
add(twitterLabel);
// JTextArea
TextArea twitterFeed1 = new TextArea();
twitterFeed1.setBounds(17, 28, 212, 248);
add(twitterFeed1);
for (Status status : tm.getUserTimeline()) {
twitterFeed1.setText("@" + status.getUser().getScreenName() + " - "
+ status.getText());
}
TextArea twitterFeed2 = new TextArea();
twitterFeed2.setBounds(246, 28, 212, 248);
add(twitterFeed2);
}
public static void main(String[] args) {}
TwitterModel.java
import twitter4j.Paging;
public class TwitterModel {
static ResponseList<Status> statuses;
static String user;
static Paging count = new Paging(1, 5);
static Twitter twitter;
public TwitterModel() {}
public ResponseList<Status> getUserTimeline() {
return TwitterModel.statuses;
}
public static void main(String[] args) {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(xxx")
.setOAuthConsumerSecret(
"xxx ")
.setOAuthAccessToken(
xxx")
.setOAuthAccessTokenSecret(
"xxx");
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
try {
if (args.length == 1) {
user = args[0];
statuses = twitter.getUserTimeline(user, count);
Controller con = new Controller();
con.settingStatus(statuses);
} else {
// user = twitter.verifyCredentials().getScreenName();
user = "Ashton";
System.out.println("Successful");
statuses = twitter.getUserTimeline(user, count);
}
System.out.println("Showing @" + user + "'s user timeline.");
for (Status status : statuses) {
System.out.println("@" + status.getUser().getScreenName()
+ " - " + status.getText());
}
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get timeline: " + te.getMessage());
System.exit(-1);
}
}