Results 1 to 8 of 8

Thread: ArrayList overwriting entries

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    13

    ArrayList overwriting entries

    Hi Guys,

    i'm creating an Java app to add 2 different String values to an ArrayList ( it has to do with my Hobby Badminton )
    I now have to following code below:
    Code:
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.Collections;
    import javax.swing.*;
    
    public class Badminton implements ActionListener {
    
    
    	 JButton knop1;
    	 JTextField tekstvak1;
    	 JTextField tekstvak2;
    	 JTextField tekstvak3;
    	 JTextArea uitvoer;
    	 JButton toernooiButton;
         JButton exitButton;
         JButton zoekButton;
         JButton wisButton;
         ArrayList<String> array1;
    	 ArrayList<String> array2;
    	
    	 
    	private JPanel getContent()
    	
    	{
    		JLabel title = new JLabel ("Badminton Toernooien");
    		JPanel bannerpanel = new JPanel();
    		bannerpanel.setSize(300,50);
    		bannerpanel.add(title);
    		
    		JLabel toernooiplek = new JLabel("Toernooi plek:     ");
    		tekstvak1 = new JTextField(30);
    	    JPanel plekPanel = new JPanel();
            plekPanel.setLayout(new BoxLayout(plekPanel, BoxLayout.X_AXIS));
            plekPanel.setSize(200, 20);
            plekPanel.add(toernooiplek);
            plekPanel.add(tekstvak1);
        
            JLabel toernooinaam = new JLabel ("Toernooi naam:    ");
            tekstvak2 = new JTextField(30);
            JPanel tekstPanel = new JPanel();
            tekstPanel.setLayout(new BoxLayout(tekstPanel, BoxLayout.X_AXIS));
            tekstPanel.setSize(200,20);
            tekstPanel.add(toernooinaam);
            tekstPanel.add(tekstvak2);
            
            JPanel blankPanel = new JPanel();
            blankPanel.setLayout(new BoxLayout(blankPanel, BoxLayout.X_AXIS));
            blankPanel.setSize(200,20);
            
            toernooiButton = new JButton("Voeg toernooi toe");
            exitButton = new JButton("Afsluiten");
            zoekButton = new JButton("Geef Toernooien weer");
            wisButton = new JButton("Wis invoer");
           
          
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
            buttonPanel.add(toernooiButton);
            buttonPanel.add(exitButton);
            buttonPanel.add(zoekButton);
            buttonPanel.add(wisButton);
            
            
            toernooiButton.addActionListener(this);
            exitButton.addActionListener(this);
            zoekButton.addActionListener(this);
            wisButton.addActionListener(this);
        
            
            JPanel mainPanel = new JPanel();
            System.out.println("JPanel default layout manager = " +
            mainPanel.getLayout().getClass().getName());
            mainPanel.add(title);
            mainPanel.add(tekstPanel);
            mainPanel.add(plekPanel);
            mainPanel.add(blankPanel);
            mainPanel.add(buttonPanel);
            return mainPanel;
            
    	}
    	
    	public void actionPerformed(ActionEvent ae){
    		if(ae.getSource() == toernooiButton){
    			
    			 String plek = tekstvak1.getText();
    	         String naam = tekstvak2.getText();
    	         
    	         tekstvak1.setText("");
    	         tekstvak2.setText("");
    			
    	        
    	         array1 = new ArrayList<String>();
    	         array1.add(plek);
    	         array2 = new ArrayList<String>();
    	         array2.add(naam);
    	         
    	        
    	         Collections.sort(array1);
    	         Collections.sort(array2);
    	         
    	         JOptionPane.showMessageDialog(null, "Toernooi Toegevoegd");
    
    	         
    		}
    	
    
    		if (ae.getSource()== wisButton){
    			tekstvak1.setText("");
    			tekstvak2.setText("");
    			
    			
    		}
    		
    		if (ae.getSource()== zoekButton){
    
    				
    			System.out.println(array1);
    			
    			
    			}
    			
    			
    		if(ae.getSource()== exitButton){
    			System.exit(0);
    			
    		}
    	}
    
    	 public static void main(String[] args) {
    	        
    	        Badminton app = new Badminton();
    	        JFrame f = new JFrame();
    	        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	        f.getContentPane().add(app.getContent());
    	        f.setSize(800,600);
    	        f.setLocation(200, 200);  
    	 }
    }
    The problem is that when i want to show the current ArrayList with the "zoekButton" it will give me my last entered input. How can i fix this so that it will list my whole ArrayList?

    Thanks in advance

    Tom

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: ArrayList overwriting entries

    Every time you click your button you're re-initializing your ArrayList.

    Code:
    array1 = new ArrayList<String>();
    array1.add(plek);
    array2 = new ArrayList<String>();
    array2.add(naam);
    Move the array1/2 = new ArrayList<String>(); to where you're declaring the list instead.

    change
    Code:
    ArrayList<String> array1;
    ArrayList<String> array2;
    to

    Code:
    ArrayList<String> array1 = new ArrayList<String>();
    ArrayList<String> array2 = new ArrayList<String>();
    and then change the first bit of code to

    Code:
    array1.add(plek);
    array2.add(naam);

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    13

    Re: ArrayList overwriting entries

    Hi kfcSmitty,

    I just opened up Eclipse again to adjust it. I tested the code again before adjusting it and i got this error:

    Unable to create editor ID org.eclipse.jdt.ui.ClassFileEditor: The Class File Viewer cannot handle the given input

    Code:
    package Final;
    
    
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.Collections;
    import javax.swing.*;
    
    public class Badminton implements ActionListener {
    
    
    	 JButton knop1;
    	 JTextField tekstvak1;
    	 JTextField tekstvak2;
    	 JTextField tekstvak3;
    	 JTextArea uitvoer;
    	 JButton toernooiButton;
             JButton exitButton;
             JButton zoekButton;
             JButton wisButton;
             ArrayList<String> array1 = new ArrayList<String>();
             ArrayList<String> array2 = new ArrayList<String>();
    	
    	 
    	private JPanel getContent()
    	
    	{
    		JLabel title = new JLabel ("Badminton Toernooien");
    		JPanel bannerpanel = new JPanel();
    		bannerpanel.setSize(300,50);
    		bannerpanel.add(title);
    		
    		JLabel toernooiplek = new JLabel("Toernooi plek:     ");
    		tekstvak1 = new JTextField(30);
    	        JPanel plekPanel = new JPanel();
            plekPanel.setLayout(new BoxLayout(plekPanel, BoxLayout.X_AXIS));
            plekPanel.setSize(200, 20);
            plekPanel.add(toernooiplek);
            plekPanel.add(tekstvak1);
        
            JLabel toernooinaam = new JLabel ("Toernooi naam:    ");
            tekstvak2 = new JTextField(30);
            JPanel tekstPanel = new JPanel();
            tekstPanel.setLayout(new BoxLayout(tekstPanel, BoxLayout.X_AXIS));
            tekstPanel.setSize(200,20);
            tekstPanel.add(toernooinaam);
            tekstPanel.add(tekstvak2);
            
            JPanel blankPanel = new JPanel();
            blankPanel.setLayout(new BoxLayout(blankPanel, BoxLayout.X_AXIS));
            blankPanel.setSize(200,20);
            
            toernooiButton = new JButton("Voeg toernooi toe");
            exitButton = new JButton("Afsluiten");
            zoekButton = new JButton("Geef Toernooien weer");
            wisButton = new JButton("Wis invoer");
           
          
            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
            buttonPanel.add(toernooiButton);
            buttonPanel.add(exitButton);
            buttonPanel.add(zoekButton);
            buttonPanel.add(wisButton);
            
            
            toernooiButton.addActionListener(this);
            exitButton.addActionListener(this);
            zoekButton.addActionListener(this);
            wisButton.addActionListener(this);
        
            
            JPanel mainPanel = new JPanel();
            System.out.println("JPanel default layout manager = " +
            mainPanel.getLayout().getClass().getName());
            mainPanel.add(title);
            mainPanel.add(tekstPanel);
            mainPanel.add(plekPanel);
            mainPanel.add(blankPanel);
            mainPanel.add(buttonPanel);
            return mainPanel;
            
    	}
    	
    	public void actionPerformed(ActionEvent ae){
    		if(ae.getSource() == toernooiButton){
    			
    	         String plek = tekstvak1.getText();
    	         String naam = tekstvak2.getText();
    	         
    	         tekstvak1.setText("");
    	         tekstvak2.setText("");
    			
    	        
    	         array1 = new ArrayList<String>();
    	         array1.add(plek);
    	         array2 = new ArrayList<String>();
    	         array2.add(naam);
    	         
    	        
    	         Collections.sort(array1);
    	         Collections.sort(array2);
    	         
    	         JOptionPane.showMessageDialog(null, "Toernooi Toegevoegd");
    
    	         
    		}
    	
    
    		if (ae.getSource()== wisButton){
    			tekstvak1.setText("");
    			tekstvak2.setText("");
    			
    			
    		}
    		
    		if (ae.getSource()== zoekButton){
    
    				
    			System.out.println(array1);
    			
    			
    			}
    			
    			
    		if(ae.getSource()== exitButton){
    			System.exit(0);
    			
    		}
    	}
    
    	 public static void main(String[] args) {
    	        
    	        Badminton app = new Badminton();
    	        JFrame f = new JFrame();
    	        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	        f.getContentPane().add(app.getContent());
    	        f.setSize(800,600);
    	        f.setLocation(200, 200);  
    	 }
    }
    I changed the code in my Badminton class to

    Code:
    ArrayList<String> array1 = new ArrayList<String>()
    ArrayList<String> array2 = new ArrayList<String>();
    but i don't know exactly where i need to put the
    Code:
    array1 = new ArrayList<String>();
    array1.add(plek);
    array2 = new ArrayList<String>();
    array2.add(naam);
    I understand now what the problem is , but not totally how i need to fix it.
    Thanks for your fast response

    Tom

  4. #4
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: ArrayList overwriting entries

    This code

    Code:
    array1 = new ArrayList<String>();
    array1.add(plek);
    array2 = new ArrayList<String>();
    array2.add(naam);
    Is already in your program, if you search for it, you'll find it. What you need to do, is remove these two lines from the above code

    Code:
    array1 = new ArrayList<String>();
    array2 = new ArrayList<String>();
    Which will leave you with

    Code:
    array1.add(plek);
    array2.add(naam);

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    13

    Re: ArrayList overwriting entries

    ah ok fixed it now, but the program won't start because of that error:

    The Class File Viewer cannot handle the given input ('org.eclipse.ui.ide.FileStoreEditorInput').

    Do you know how to fix that error, didn't had it before

    Thanks for the fast feedback

    Tom

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    13

    Re: ArrayList overwriting entries

    ok i fixed that error by putting in
    Code:
    f.setVisible(true);
    But no i get this when i click the toernooiButton:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Final.Badminton.actionPerformed(Badminton.java:94)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Last edited by tommie20; Jan 27th, 2010 at 02:00 PM. Reason: typo

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    13

    Re: ArrayList overwriting entries

    Ok,

    the problem is this piece of code

    Code:
    array1.add(plek);
    array2.add(naam);
    if i delete it it runs, but not properly.
    Do you know where i need to put it / change it.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2010
    Posts
    13

    Re: ArrayList overwriting entries

    ok guys,

    solved the problem, it was the start of the code.
    The lines

    Code:
        ArrayList<String> array1 = new ArrayList<String>();
    ArrayList<String> array2 = new ArrayList<String>();
    I forgot those.

    kfcSmitty, thanks for your help

    Tom

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width