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
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++)
{
{
Code:
if(inputWords[index].startsWith("s"))
{
sortedWords[sortedWordsCount]=inputWords[index];
sortedWordsCount++;
sortedWordList.setListData(sortedWords);
}
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)