Results 1 to 2 of 2
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default How do initialize these Strings?

    I solved the problem that i originally wrote this thread for, but i have another problem, Why do lines 81-86 throw a null pointer exception?!

    Code:
    StartsWithLetter.java
    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    
    
    public class StartsWithLetter extends JFrame{
    	private JTextField inputWordField;//
    	private JButton addButton;//
    	private JButton sortButton;
    	private JList inputWordList;//
    	private JList sortedWordList;//
    	private static String[] inputWords;
    	private static int inputWordsCount = 0;
    	private static String[] sortedWords;
    	private static int sortedWordsCount=0;
    
    	
    	public StartsWithLetter()
    	{
    		super("Find words that start with");
    		setLayout(new FlowLayout());
    		
    		inputWords = new String[5];
    		sortedWords = new String[5];
    		
    		//word entered here
    		inputWordField = new JTextField("Enter Word Here");
    		add(inputWordField);
    		
    		//word is added to array and list is refreshed
    		addButton = new JButton("Add to List");
    		add(addButton);
    		
    		sortButton = new JButton("Sort");
    		add(sortButton);
    		
    		inputWordList = new JList(inputWords);
    		inputWordList.setVisibleRowCount(5);
    		inputWordList.setFixedCellHeight(20);
    		inputWordList.setFixedCellWidth(100);
    		add(new JScrollPane(inputWordList));
    		
    		sortedWordList = new JList(sortedWords);
    		sortedWordList.setVisibleRowCount(5);
    		sortedWordList.setFixedCellHeight(20);
    		sortedWordList.setFixedCellWidth(100);
    		add(new JScrollPane(sortedWordList));
    		
    		addButton.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent arg0) {
    						String enteredWord;
    						enteredWord = inputWordField.getText();
    						
    						inputWords[inputWordsCount]=enteredWord;
    						inputWordsCount++;
    						inputWordList.setListData(inputWords);
    						
    						
    					}
    					
    				}
    		);
    		
    		sortButton.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent arg0) {
    					//try{
    						for(int index=0;index<inputWords.length;index++)
    						{
    							{
    Java Code:
    								if(inputWords[index].startsWith("s"))
    								{
    									sortedWords[sortedWordsCount]=inputWords[index];
    									sortedWordsCount++;
    									sortedWordList.setListData(sortedWords);
    								}
    Java Code:
    							
    						}
    					}
    					//catch(Exception e)
    					//{
    					//		JOptionPane.showMessageDialog(null, "Shit Just Happened","WTF?!",JOptionPane.ERROR_MESSAGE);
    					//}
    					//	
    					//}
    					
    				}
    		);
    		
    	}
    }
    Exception Error Message:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at StartsWithLetter$2.actionPerformed(StartsWithLette r.java:81)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.jav a:6175)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:594 0)
    at java.awt.Container.processEvent(Container.java:210 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4536)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2163)
    at java.awt.Component.dispatchEvent(Component.java:43 62)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4125)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2149)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:43 62)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 604)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)
    Last edited by eLancaster; 03-02-2011 at 06:31 PM.

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Array's have to be initialized before you can use them.
    Java Code:
    inputWords = new String[numberofelements];
    You can't assign anything to them unless they've been initialized.

Similar Threads

  1. Initialize a Variable
    By lala in forum New To Java
    Replies: 13
    Last Post: 11-16-2010, 07:51 PM
  2. Trying to initialize variables
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-14-2009, 11:30 PM
  3. Int does not initialize, will this work?
    By starchildren3317 in forum New To Java
    Replies: 2
    Last Post: 07-09-2008, 11:42 PM
  4. How to initialize an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:47 PM
  5. Initialize variables before use
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-22-2007, 12:22 PM

Posting Permissions

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