NullPointerException с getText () - PullRequest
       15

NullPointerException с getText ()

2 голосов
/ 31 января 2012

Я пытаюсь получить текст из текстового поля, но каждый раз, когда я запускаю программу, я получаю исключение NullPointerException. Подобные проблемы, которые я обнаружил в Интернете, связаны с тем, что текстовое поле переименовано в локальную переменную, но я не вижу, что я делаю это здесь.

Ошибка:

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

в указанной строке. Любая помощь будет высоко ценится

EDIT:

Эта короткая программа демонстрирует проблему

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);
}
}

Ответы [ 4 ]

3 голосов
/ 31 января 2012

Посмотрев на ваш SSCCE, мне удалось решить NPE, закомментировав избыточный Test, который был создан, и создав массив панелей в конструкторе.EG

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);
}
}

Примечание: я не могу полностью объяснить, почему изменения решили NPE, я просто рефакторинг "подозрительного" кода.

2 голосов
/ 31 января 2012

Я решил ваш запрос, связанный с NullPointerException:

Проблема в том, что вы объявили переменную класса:

JTextField manualEntryField = null;

Просто напишите:

private JTextField manualEntryField;

Простоудалите эту нулевую вещь, против нее и остальной код в порядке.Не могу сказать, почему это произошло, но может быть кто-то другой может.

Вот ваш код только с этим изменением, попробуйте это сейчас:

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;
    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);
    }
}

С уважением

0 голосов
/ 31 января 2012

Вы нажимаете кнопку, когда ваше текстовое поле пусто?Если это так, то вы можете получить это исключение при вызове функции trim (), поэтому посмотрите, что возвращает getText (), перед вызовом trim ()

0 голосов
/ 31 января 2012

Если textBox пуст, это нормально для этого исключения. Попробуй это if (getText (). size () <1) условие или использовать try-catch, а когда вы перехватываете исключение, задайте значение безразмерной строки "". </p>

...