-3

I am trying to make a reply Form using NetBeans gallery. I am using JList to view my messages and when I would like to reply to a specific message I got Null Pointer Exception. This my code: I made two constructors one to make a JFrame and the other to set getFrom() method in to.Text().

public class ReplyForm extends javax.swing.JFrame {

private JList theList;

ReplyForm(JList inputList) throws IOException, MessagingException {

    theList = inputList;

 //   System.out.println(theList);
    if (theList != null) {
        int i = theList.getSelectedIndex();
        Email email = (Email) theList.getModel().getElementAt(i);
        Message message = email.getMessage();
        to.setText(message.getFrom()[0].toString());
        subject.setText("RE:   " + message.getSubject());
        text.setText((String) message.getContent());

    }
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3274585
  • 21
  • 1
  • 4
  • 1
    1. Post an [MCVE](http://stackoverflow.com/help/mcve). 2. Post the stack trace. 3. Let us know what line its pointing to 4. Read [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). I'm going to vote to close this as a duplicate in it's current state. Please fix the question to include the above – Paul Samsotha Aug 23 '14 at 01:29
  • 1
    First learn about what a null pointer exception is. After you know what it is, it should be easy to debug. – nmore Aug 23 '14 at 01:41
  • NullPointerException is the *easiest* of all problems to debug. The exception message tells you what line, and you know one of the pointers on that line is null. Figure out which one, either using a debugger or with println statements. – Hot Licks Aug 23 '14 at 02:41

1 Answers1

0

You might be getting NullPointerException, as message might be null.

Try:

if (message != null) {
    to.setText(message.getFrom()[0].toString());
    subject.setText("RE:   " + message.getSubject());
    text.setText((String) message.getContent());
}
Vikram
  • 3,996
  • 8
  • 37
  • 58
  • 2
    there's a lot of variables that might be null... `theList.getModel()`,`email`, `to`, `subject` – Gus Aug 23 '14 at 01:36
  • What I did is using JList to view all my messages. What I want to do is replying to a specific message the same when we reply by Yahoo mail. My problem is when I parse the subject and From method to the JTextField in the Reply Form. I did debugging but it said JList is null. I do not know how to parse the From and the subject and put them in the JTextField??.In my code I added ListSelectionListener, but I know it is wrong: – user3274585 Aug 23 '14 at 21:45
  • My code is:theList.setModel(new javax.swing.AbstractListModel() { public int getSize() { return emails.length; } public Object getElementAt(int i) { return emails[i]; } }); email = (Email) theList.getModel().getElementAt(i); message = email.getMessage(); to.setText(message.getFrom()[0].toString()); subject.setText("RE: " + message.getSubject()); text.setText((String) message.getContent()); – user3274585 Aug 23 '14 at 21:50