Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7196927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:52:20+00:00 2026-05-28T20:52:20+00:00

I’m trying to get text from a text field but every time I run

  • 0

I’m trying to get text from a text field but every time I run the program I get a NullPointerException. Similar problems I’ve found on the internet involve the text field being redeclared as a local variable, but I can’t see that I am doing that here.

The error is :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at the line specified. Any help would be much appreciated

EDIT:

This short program demonstrates the problem

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Test{

public static final Test application = new Test();
private JFrame jFrame = null;
private JPanel jContentPane = null, buttonsPanel = null, cardPanel = null, manualPanel = null, uploadPanel = null,
        defaultPanel = null;
private JPanel[] mainPanel = {getDefaultPanel(), getManualPanel(), getUploadPanel()};
private JButton manualButton = null, uploadButton = null, manualAssignButton = null;
private JTextField manualEntryField = null;
private JLabel manualLabel = null;

final static String DEFAULTPANEL = "Default";
final static String MANUALPANEL = "Manual";
final static String UPLOADPANEL = "Upload";

/**
 * Main method
 * @param args
 */
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            application.getJFrame().setVisible(true);
        }
    });
}

/**
 * Get the main application frame
 * @return JFrame
 */
private JFrame getJFrame() {
    if (jFrame == null) {
        jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        jFrame.setResizable(false);
        // Add the main content pane
        jFrame.setContentPane(getJContentPanel());
        jFrame.addWindowListener(new java.awt.event.WindowAdapter() {   
            public void windowClosing(java.awt.event.WindowEvent e) {    
                closeApplication();
            }
        });
    }
    jFrame.pack();
    jFrame.setLocationRelativeTo(null);
    return jFrame;
}

/**
 * Get main content
 * @return JPanel
 */
private JPanel getJContentPanel(){
    if (jContentPane == null) {
        jContentPane = new JPanel();
        jContentPane.setPreferredSize(new Dimension(500, 200));
        jContentPane.setLayout(new BorderLayout());
        jContentPane.add(getButtonsPanel(), BorderLayout.NORTH);
        jContentPane.add(getCardPanel(), BorderLayout.CENTER);
    }
    return jContentPane;
}

private JPanel getButtonsPanel() {
    if(buttonsPanel == null){
        buttonsPanel = new JPanel();
        buttonsPanel.setPreferredSize(new Dimension(500, 50));
        buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.gray));

        manualButton = new JButton("Enter manually");
        manualButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout)(cardPanel.getLayout());
                cl.show(cardPanel, MANUALPANEL);
            }
        });

        uploadButton = new JButton("Upload file");
        uploadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout)(cardPanel.getLayout());
                cl.show(cardPanel, UPLOADPANEL);
            }
        });

        buttonsPanel.add(manualButton);
        buttonsPanel.add(uploadButton);
    }
    return buttonsPanel;
}

private JPanel getCardPanel() {
    if(cardPanel == null){
        cardPanel = new JPanel(new CardLayout());
        cardPanel.setPreferredSize(new Dimension(500, 200));
        cardPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
        cardPanel.add(mainPanel[0], DEFAULTPANEL);
        cardPanel.add(mainPanel[1], MANUALPANEL);
        cardPanel.add(mainPanel[2], UPLOADPANEL);
    }
    return cardPanel;
}

private JPanel getDefaultPanel() {
    if(defaultPanel == null){
        defaultPanel = new JPanel();
    }
    return defaultPanel;
}

private JPanel getManualPanel() {
    if(manualPanel == null){
        manualPanel = new JPanel();
        manualPanel.setLayout(new FlowLayout());

        manualEntryField = new JTextField(10);
        manualEntryField.setText("Enter code");

        manualAssignButton = new JButton("Assign");
        manualAssignButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
             // Here is the exception
            System.out.println(manualEntryField.getText().trim());
            }
        });

        manualLabel = new JLabel("Please enter a code and press assign");

        manualPanel.add(manualEntryField);
        manualPanel.add(manualAssignButton);
        manualPanel.add(manualLabel);

    }
    return manualPanel;
}

private JPanel getUploadPanel() {
    if(uploadPanel == null){
        uploadPanel = new JPanel();
    }
    return uploadPanel;
}

private void closeApplication() {
    System.exit(0);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:52:21+00:00Added an answer on May 28, 2026 at 8:52 pm

    After looking at your SSCCE, I managed to solve the NPE by commenting out the redundant Test that was instantiated, and creating the panel array in the constructor. E.G.

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class Test{
    
    //public static final Test application = new Test();
    private JFrame jFrame = null;
    private JPanel jContentPane = null, buttonsPanel = null, cardPanel = null, manualPanel = null, uploadPanel = null,
            defaultPanel = null;
    private JPanel[] mainPanel = new JPanel[3];
    private JButton manualButton = null, uploadButton = null, manualAssignButton = null;
    private JTextField manualEntryField = null;
    private JLabel manualLabel = null;
    
    final static String DEFAULTPANEL = "Default";
    final static String MANUALPANEL = "Manual";
    final static String UPLOADPANEL = "Upload";
    
    Test() {
        mainPanel[0] = getDefaultPanel();
        mainPanel[1] = getManualPanel();
        mainPanel[2] = getUploadPanel();
    }
    
    /**
     * Main method
     * @param args
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test().getJFrame().setVisible(true);
            }
        });
    }
    
    /**
     * Get the main application frame
     * @return JFrame
     */
    private JFrame getJFrame() {
        if (jFrame == null) {
            jFrame = new JFrame();
            jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            jFrame.setResizable(false);
            // Add the main content pane
            jFrame.setContentPane(getJContentPanel());
            jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent e) {
                    closeApplication();
                }
            });
        }
        jFrame.pack();
        jFrame.setLocationRelativeTo(null);
        return jFrame;
    }
    
    /**
     * Get main content
     * @return JPanel
     */
    private JPanel getJContentPanel(){
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setPreferredSize(new Dimension(500, 200));
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getButtonsPanel(), BorderLayout.NORTH);
            jContentPane.add(getCardPanel(), BorderLayout.CENTER);
        }
        return jContentPane;
    }
    
    private JPanel getButtonsPanel() {
        if(buttonsPanel == null){
            buttonsPanel = new JPanel();
            buttonsPanel.setPreferredSize(new Dimension(500, 50));
            buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
    
            manualButton = new JButton("Enter manually");
            manualButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    CardLayout cl = (CardLayout)(cardPanel.getLayout());
                    cl.show(cardPanel, MANUALPANEL);
                }
            });
    
            uploadButton = new JButton("Upload file");
            uploadButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    CardLayout cl = (CardLayout)(cardPanel.getLayout());
                    cl.show(cardPanel, UPLOADPANEL);
                }
            });
    
            buttonsPanel.add(manualButton);
            buttonsPanel.add(uploadButton);
        }
        return buttonsPanel;
    }
    
    private JPanel getCardPanel() {
        if(cardPanel == null){
            cardPanel = new JPanel(new CardLayout());
            cardPanel.setPreferredSize(new Dimension(500, 200));
            cardPanel.setBorder(BorderFactory.createLineBorder(Color.gray));
            cardPanel.add(mainPanel[0], DEFAULTPANEL);
            cardPanel.add(mainPanel[1], MANUALPANEL);
            cardPanel.add(mainPanel[2], UPLOADPANEL);
        }
        return cardPanel;
    }
    
    private JPanel getDefaultPanel() {
        if(defaultPanel == null){
            defaultPanel = new JPanel();
        }
        return defaultPanel;
    }
    
    private JPanel getManualPanel() {
        if(manualPanel == null){
            manualPanel = new JPanel();
            manualPanel.setLayout(new FlowLayout());
    
            manualEntryField = new JTextField(10);
            System.out.println("creating text field");
            manualEntryField.setText("Enter code");
    
            manualAssignButton = new JButton("Assign");
            manualAssignButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                 // Here is the exception
                System.out.println(manualEntryField);
                System.out.println(manualEntryField.getText());
                System.out.println(manualEntryField.getText().trim());
                }
            });
    
            manualLabel = new JLabel("Please enter a code and press assign");
    
            manualPanel.add(manualEntryField);
            manualPanel.add(manualAssignButton);
            manualPanel.add(manualLabel);
    
        }
        return manualPanel;
    }
    
    private JPanel getUploadPanel() {
        if(uploadPanel == null){
            uploadPanel = new JPanel();
        }
        return uploadPanel;
    }
    
    private void closeApplication() {
        System.exit(0);
    }
    }
    

    Note: I cannot quite explain why the changes solved the NPE, I was just refactoring ‘suspicious’ code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a bunch of posts stored in text files formatted in yaml/textile (from
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.