Results 1 to 7 of 7
  1. #1
    kuckphoto is offline Member
    Join Date
    Mar 2015
    Posts
    3
    Rep Power
    0

    Default NullPointerException while using getText() method for JTextField

    I'm really stuck. I'm sure I'm missing something really simple but I've been stuck for a while. I'm getting the Exception when I click the addAuthor button with text in the authorField and I call authorField.getText(). Anyone have any ideas?


    package library.presentation;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import library.domain.Book;
    import java.util.*;
    import static javax.swing.JList.VERTICAL;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import library.business.BookMGR;

    /**
    *
    * @author skuck
    */
    public class LibraryBookGUI extends JFrame {

    // fields
    public ArrayList<String> authorToAdd;
    public String listAdd = "";
    private DefaultListModel listModel = new DefaultListModel();
    DocumentListener documentListener;

    private JLabel isbnLabel = new JLabel("Enter isbn");
    private JLabel authorLabel = new JLabel("Enter Author(s)");
    private JLabel noLabel = new JLabel("");
    private JLabel noLabel1 = new JLabel("");
    private JLabel noLabel2 = new JLabel("");
    private JTextField isbnField = new JTextField(50);
    private JTextField authorField = new JTextField(50);
    private JButton acceptButton = new JButton("Accept");
    private JButton exitButton = new JButton("Exit");
    private JButton addAuthorButton = new JButton("Add Author");
    private JButton resetButton = new JButton("Reset Fields");
    private JList authorList = new JList(listModel);
    private JTextArea authorDisplay = new JTextArea(listAdd, 5, 10);


    // LibraryBookGUI constructor
    public LibraryBookGUI() {
    // super(name);

    // components
    Container cp = this.getContentPane();

    cp.setBackground(Color.lightGray);
    GridBagLayout layout = new GridBagLayout();
    cp.setLayout(layout);

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 0;
    cp.add(isbnLabel, gbc);
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    isbnField.setText(" ");
    cp.add(isbnField, gbc);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    cp.add(authorLabel, gbc);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.gridwidth = 2;
    authorField.setText(" ");
    cp.add(authorField, gbc);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    cp.add(addAuthorButton, gbc);
    gbc.gridx = 1;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    cp.add(resetButton, gbc);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.gridwidth = 2;
    cp.add(acceptButton, gbc);
    gbc.gridx = 2;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    cp.add(exitButton, gbc);

    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 4;
    gbc.gridwidth = 3;
    gbc.gridheight = 4;
    authorList.setVisibleRowCount(4);
    authorList.setLayoutOrientation(VERTICAL);
    authorDisplay.setPreferredSize(new Dimension(100, 100));
    authorDisplay.setEditable(false);
    cp.add(authorDisplay, gbc);

    addAuthorButton.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    try {
    String authorTextFieldVar;
    authorTextFieldVar = authorField.getText();
    authorToAdd.add(authorTextFieldVar);
    listModel.addElement(authorTextFieldVar);
    authorDisplay.setText(authorTextFieldVar);
    System.out.println(authorTextFieldVar);
    } // end try
    catch(NullPointerException ex) {
    authorField.setText("No author was entered");
    } // end catch
    } // end actionPerformed
    } // end ActionListener
    );

    resetButton.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    isbnField.setText("");
    authorField.setText("");
    } // end actionPerformed
    } // end ActionListener
    );

    exitButton.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    System.exit(0);
    }
    }
    );

    acceptButton.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    try{
    long isbnToAdd = Long.parseLong(isbnField.getText());
    BookMGR bookMGR = new BookMGR();
    Book book = new Book(authorToAdd, isbnToAdd);
    bookMGR.storeBook(book);
    } // end try
    catch(Exception ex){
    System.out.println("isbn entered is not valid.");
    } // end catch
    } // end actionPerformed
    } // end ActionListener
    );


    // size, title, and visibility
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Library Book Data");
    setSize(350, 200);
    setVisible(true);

    } // end LibraryBookGUI

    @Override
    public String toString() {
    return "LibraryBookGUI{" + "authorToAdd=" + authorToAdd + ", listAdd="
    + listAdd + ", listModel=" + listModel + ", isbnLabel="
    + isbnLabel + ", authorLabel=" + authorLabel
    + ", noLabel=" + noLabel + ", noLabel1=" + noLabel1
    + ", noLabel2=" + noLabel2 + ", isbnField=" + isbnField
    + ", authorField=" + authorField + ", authorList="
    + authorList + '}';
    } // end toString


    private static final long serialVersionUID = 1L; // version 1

    public static void main(String[] args) {

    // run GUI codes in Event Dispatching Thread
    SwingUtilities.invokeLater(new Runnable() {
    @Override

    public void run() {
    LibraryBookGUI newGUI = new LibraryBookGUI();
    Book bookToAdd = new Book();
    } // end run
    }); // end invokeLater
    }

    } // end LibraryBookGUI

    StackTrace for NullPointerException when try-catch is removed:

    run:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at library.presentation.LibraryBookGUI$1.actionPerfor med(LibraryBookGUI.java:117)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6516)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3320)
    at java.awt.Component.processEvent(Component.java:628 1)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4872)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 98)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 98)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 705)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)
    BUILD SUCCESSFUL (total time: 1 minute 32 seconds)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: NullPointerException while using getText() method for JTextField

    Please go through BB Code List - Java Programming Forum - Learn Java Programming and format your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: NullPointerException while using getText() method for JTextField

    Quote Originally Posted by kuckphoto View Post
    StackTrace for NullPointerException when try-catch is removed:

    run:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at library.presentation.LibraryBookGUI$1.actionPerfor med(LibraryBookGUI.java:117)
    So, which is line 117? Which variable on that line wasn't assigned a value?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    kuckphoto is offline Member
    Join Date
    Mar 2015
    Posts
    3
    Rep Power
    0

    Default Re: NullPointerException while using getText() method for JTextField

    authorTextFieldVar is not getting assigned the value from authorField.getText()

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: NullPointerException while using getText() method for JTextField

    What variable had the null value when line 117 was executed?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    kuckphoto is offline Member
    Join Date
    Mar 2015
    Posts
    3
    Rep Power
    0

    Default Re: NullPointerException while using getText() method for JTextField

    Quote Originally Posted by Norm View Post
    What variable had the null value when line 117 was executed?
    This is line 117:

    authorToAdd.add(authorTextFieldVar);

    authorTextFieldVar is null.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: NullPointerException while using getText() method for JTextField

    Quote Originally Posted by kuckphoto View Post
    This is line 117:

    authorToAdd.add(authorTextFieldVar);

    authorTextFieldVar is null.
    Nope.
    For that line to throw the NPE then authorToAdd has to be null.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 02-15-2015, 04:50 AM
  2. Replies: 6
    Last Post: 02-10-2014, 12:44 PM
  3. Replies: 3
    Last Post: 03-30-2013, 02:33 AM
  4. Replies: 15
    Last Post: 10-30-2011, 06:02 AM
  5. JTextField method numberic
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 4
    Last Post: 03-14-2010, 11:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •