修改
当我创建TwitterModel
的实例时,不会执行主要方法,然后初始化statuses
?因此,当我致电statuses
时,我应该获得getUsertimeline()
的价值?
我在statuses
的主要方法中设置TwitterPanel
之后试图获取TwitterModel
。但是,我在执行时获得NullPointerException
:
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发生在这里)
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);
}
}
答案 0 :(得分:0)
主要方法是静态的,在启动程序时调用。每个程序只有一个主要方法。
public TwitterModel() {}
这是在创建实例时调用的构造函数。 尝试将主要方法的代码放到这里
答案 1 :(得分:0)
问题背景:
Twitter身份验证代码在执行前需要String[] args
参数。但是,在创建上述课程的实例时,我必须将args
初始化为null
。因此收到NullPointerException
。
<强>答案:强>
我认为解决方案是构造函数重载,如下所示。如果我错了,请纠正我
<强> MainFrame.java 强>
public class MainFrame extends JFrame{
public MainFrame(){
....
}
}
public static void Twitter(){
... // Twitter's authentication... etc are executed here
}
public static void main(String[] args){
Twitter();
...
}