Hi :
I m working on java eclipse I want to move one selected item from list to texTarea but when I execute this code I have got following error :
here the whole code :PHP Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at listtest.testlist$3.actionPerformed(testlist.java:84)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
package listtest; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; import javax.swing.AbstractListModel; import javax.swing.JButton; import javax.swing.JTextArea; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class testlist { private JFrame frame; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { testlist window = new testlist(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public testlist() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 490, 422); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JPanel panel = new JPanel(); panel.setBounds(0, 0, 464, 384); frame.getContentPane().add(panel); panel.setLayout(null); JScrollPane scrollPane = new JScrollPane(); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setBounds(121, 69, 77, 120); panel.add(scrollPane); final JList list = new JList(); list.setModel(new AbstractListModel() { String[] values = new String[] {"1", "2", "3"}; public int getSize() { return values.length; } public Object getElementAt(int index) { return values[index]; } }); scrollPane.setViewportView(list); JButton btnNewButton = new JButton("New button"); btnNewButton.addActionListener(new ActionListener() { private JTextArea textArea; public void actionPerformed(ActionEvent arg0) { textArea.append((String) list.getSelectedValue()); } }); btnNewButton.setBounds(115, 216, 89, 23); panel.add(btnNewButton); JScrollPane scrollPane_1 = new JScrollPane(); scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane_1.setBounds(128, 265, 73, 108); panel.add(scrollPane_1); JTextArea textArea = new JTextArea(); scrollPane_1.setViewportView(textArea); } }
thanks to correct me .