Results 1 to 2 of 2
Thread: How do initialize these Strings?
- 03-02-2011, 05:03 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 93
- Rep Power
- 0
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); }Exception Error Message:Java Code:} } //catch(Exception e) //{ // JOptionPane.showMessageDialog(null, "Shit Just Happened","WTF?!",JOptionPane.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 05:31 PM.
- 03-02-2011, 05:09 PM #2
Similar Threads
-
Initialize a Variable
By lala in forum New To JavaReplies: 13Last Post: 11-16-2010, 06:51 PM -
Trying to initialize variables
By random0munky in forum New To JavaReplies: 2Last Post: 10-14-2009, 10:30 PM -
Int does not initialize, will this work?
By starchildren3317 in forum New To JavaReplies: 2Last Post: 07-09-2008, 10:42 PM -
How to initialize an Array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:47 PM -
Initialize variables before use
By Java Tip in forum Java TipReplies: 0Last Post: 12-22-2007, 11:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks