Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: need corection on java code

  1. #1
    Junior Member
    Join Date
    May 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need corection on java code

    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 :



    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
    here the whole code :

     
    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 .

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: need corection on java code

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at listtest.testlist$3.actionPerformed(testlist.java: 84)
    There is a null value in a variable when line 84 is executed. Look at line 84, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value when line 84 is executed.

  3. #3
    Junior Member
    Join Date
    May 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need corection on java code

    hi:

    may be the problem in this lines :


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

  4. #4
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: need corection on java code

    What variable has the null value? Add a print statement just before line 84 that prints the contents of all the variables used on line 84 so you can see where the null value is.

  5. #5
    Junior Member
    Join Date
    May 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need corection on java code

    I have added bellow code but it always point to line 84 .

    [QUOTE]

    String data = "";

    data = (String) list.getSelectedValue();

    System.out.println(data);


    [QUOTE]

    in console mode only message error is printed
    Attached Images Attached Images

  6. #6
    Junior Member
    Join Date
    May 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need corection on java code

    Hi :

    if I remove this line it works but I need textarea

    I can't find a solution

    jlisttest2.PNG

    thanks

  7. #7
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: need corection on java code

    Did you try printing the values of the variables used on line 84 to find which one had the null value?

    You need to find that variable so the code can be changed so the variable has a valid value.

  8. #8
    Junior Member
    Join Date
    May 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need corection on java code

    hi:

    I'm working on eclipse MARS.1

    can you an example from your side .

    the error still pending

    thanks

  9. #9
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: need corection on java code

    Here's an example of what you need to do:
      System.out.println("var1="+var1 +", var2="+var2); //  print the variables used on the next line
      var1.xyz =  var2.y();  // two variables could cause NPE

    Find line 84, get the names of the variables used there and print their values as shown above.

Similar Threads

  1. Replies: 0
    Last Post: May 23rd, 2013, 04:35 PM
  2. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  3. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  4. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  5. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM