문제

I just want to print the selected option in the combo box, to a textfield. Please explain what's wrong because i have to complete it & explain it in class. Any help would be greatly appreciated. Thanks in advance.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class App3 extends JFrame implements ActionListener
{
    private JPanel boxPanel,textPanel;
    private JLabel selectName,selectedName;
    private JComboBox nameCombo;
    private JTextField valueOfSelectedName;
    private Container c;

    public App3()
    {
        super("Combo example");
        setup();
        setSize(200,200);
        setLocation(50,50);
        show();
    }

    public void setup()
    {
        c = getContentPane();

        boxPanel = new JPanel();
        c.add(boxPanel,BorderLayout.NORTH);

        selectName = new JLabel("Select Name : ");
        selectedName = new JLabel("The selected Name : ");

        String[] names = {"Ramila","Hashan","Shaad","Gus","Mahasen","Hasaru","Shabba"};
        nameCombo = new JComboBox(names);
        nameCombo.addActionListener(this);

        valueOfSelectedName = new JTextField(10);

        boxPanel.add(selectName);
        boxPanel.add(nameCombo);

        c.add(textPanel,BorderLayout.CENTER);

        textPanel.add(selectedName);
        textPanel.add(valueOfSelectedName);

    }

    public void actionPerformed(ActionEvent e) 
    {
        JComboBox nameCombo = (JComboBox)e.getSource();
        String newSelection = (String)nameCombo.getSelectedItem();
        valueOfSelectedName.setText(newSelection);
    }

    public static void main(String args[])
    {
        App3 a = new App3();
    }
}

i don't get any compile time errors, i get these errors when i run it.

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1041)
    at java.awt.Container.add(Container.java:927)
    at App3.setup(App3.java:42)
    at App3.(App3.java:16)
    at App3.main(App3.java:58)

Process completed.
도움이 되었습니까?

해결책

private JPanel boxPanel,textPanel;
...
textPanel = new JPanel();

You have not created the JPanel object hence, textPanel is pointing to null which is why the Exception is being thrown. Create the object and everything should work fine

다른 팁

textPanel is null when you try to add it to the content pane:

c.add(textPanel, BorderLayout.CENTER);
import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class App3 extends JFrame implements ActionListener
{
    private JPanel boxPanel,textPanel;

    private JLabel selectName,selectedName;

    private JComboBox nameCombo;

    private JTextField valueOfSelectedName;

    private Container c;


    public App3()
    {
        super("Combo example");
        setup();
        setSize(200,200);
        setLocation(50,50);
        show();
    }

    public void setup()
    {
        c = getContentPane();

        boxPanel = new JPanel();
        c.add(boxPanel,BorderLayout.NORTH);

        selectName = new JLabel("Select Name : ");
        selectedName = new JLabel("The selected Name : ");

        String[] names = {"Ramila","Hashan","Shaad","Gus","Mahasen","Hasaru","Shabba"};
        nameCombo = new JComboBox(names);
        nameCombo.addActionListener(this);

        valueOfSelectedName = new JTextField(10);

        boxPanel.add(selectName);
        boxPanel.add(nameCombo);

        textPanel = new JPanel(); // panel is not init that why its give error

        c.add(textPanel,BorderLayout.CENTER);

        textPanel.add(selectedName);
        textPanel.add(valueOfSelectedName);

    }

    public void actionPerformed(ActionEvent e) 
    {
        JComboBox nameCombo = (JComboBox)e.getSource();
        String newSelection = (String)nameCombo.getSelectedItem();
        valueOfSelectedName.setText(newSelection);
    }

    public static void main(String args[])
    {
        App3 a = new App3();
    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top