-2

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);
    }
}
Ashton
  • 43
  • 1
  • 3
  • 14
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – xlecoustillier Jul 03 '15 at 08:53
  • @X.L.Ant I just added an explanation to my question. Thank you – Ashton Jul 03 '15 at 08:58
  • Why would the main be executed during tm instanciation ? main is an application entry point, not a constructor. Anyway, use a debugger, and see what code is executed and what the statuses value is when you need it. – xlecoustillier Jul 03 '15 at 09:07
  • BTW, I hope that the oAuth values you provided are not the right ones. Otherwise, the whole internet now knows your credentials :'( – xlecoustillier Jul 03 '15 at 09:11
  • Good that you deleted them, but they still are available in the post history. You should reset them ASAP. – xlecoustillier Jul 03 '15 at 09:15
  • Thanks for clearing my misconception. However, the codes in the main method require `String [] args` to function, and if I were to do a method call, args would be initiated to `null`, causing a NPE. I can't seem to find a work around to this – Ashton Jul 03 '15 at 09:19
  • The main method is not intended to be called from within your application, it's intended to launch the application itself, taking args from command line (i.e. `java TwitterModel arg1 arg2...`). As if I suspect you launch your application through the main in `MainFrame.java`, just rename your main method in TwitterModel (and make it non static), and pass all the parameters you need to the method when calling it. Or put its implementation into the TwitterModel constructor. – xlecoustillier Jul 03 '15 at 09:50

2 Answers2

0

The main method is static and is called when you start your programm. Every Programm only got one main method.

public TwitterModel() {}

This is the constructor which is being called when you create an instance. Try to put your code of the main method into here

red13
  • 427
  • 4
  • 12
0

Background of problem:

The Twitter authentication codes required the String[] args parameter to be present before execution. However, when creating an instance of the aforementioned class, I'd have to initialize args to null. Hence receiving a NullPointerException.

Answer:

I believe the solution was constructor overloading as shown below. Do correct me if I'm wrong

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();
    ...
}
Ashton
  • 43
  • 1
  • 3
  • 14