源程序如下:import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Text1
{
public static void main(String[] args)
{
TextFrame frame = new TextFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class TextFrame extends JFrame
{
public TextFrame()
{
setTitle("TextFrame");
setSize(WIDTH,HEIGHT);
panel = new JPanel();
panel.setLayout(new GridLayout(5,2));
panel.add(new JLabel("Input:"));
textfield1 = new JTextField(10); //增加一个Input文本域
panel.add(textfield1);
textfield1.addActionListener(listener1);
panel.add(new JLabel("output:"));
textfield2 = new JTextField(10); //增加一个Onput文本域
textfield2.setEditable(false);
panel.add(textfield2);
group = new ButtonGroup();
addRadioButton("A",false);
addRadioButton("B",false);
addRadioButton("C",false); //增加四个单选按钮
addRadioButton("D",false);
choice = new Choice(); //增加一个choice组件
panel.add(choice);
add(panel,BorderLayout.NORTH);
}
public void addRadioButton(String name,boolean selected)
{
JRadioButton button = new JRadioButton(name,selected);
group.add(button);
panel.add(button);
button.addActionListener(listener2);
}
ActionListener listener1 = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
String s = textfield1.getText();
pass(s);
String text = textfield2.getText() + " " + textfield1.getText();
textfield2.setText(text);
if(evt.getSource() == textfield1)
{
textfield1.setText("");
}
}
};
ActionListener listener2 = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
String s1 = evt.getActionCommand();
String s2 = s1.toLowerCase();
find(s2);
}
};
int i = 0;
public void pass(String s)
{
str[i] = s;
i++;
}
public void find(String s)
{
for(int i = 0; i < str.length; i++)
{
if(str[i].startsWith(s))
choice.add(str[i]);
}
}
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
private JTextField textfield1;
private JTextField textfield2;
private JPanel panel;
private ButtonGroup group;
private Choice choice;
private String[] str;
}编译没问题,但在Input文本域中输入单词按回车后就有异常了。想请教高手这是怎么回事?
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TextFrame.pass(Text1.java:101)
at TextFrame$1.actionPerformed(Text1.java:69)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(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)
import java.awt.*;
import java.awt.event.*;
public class Text1
{
public static void main(String[] args)
{
TextFrame frame = new TextFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class TextFrame extends JFrame
{
public TextFrame()
{
setTitle("TextFrame");
setSize(WIDTH,HEIGHT);
panel = new JPanel();
panel.setLayout(new GridLayout(5,2));
panel.add(new JLabel("Input:"));
textfield1 = new JTextField(10); //增加一个Input文本域
panel.add(textfield1);
textfield1.addActionListener(listener1);
panel.add(new JLabel("output:"));
textfield2 = new JTextField(10); //增加一个Onput文本域
textfield2.setEditable(false);
panel.add(textfield2);
group = new ButtonGroup();
addRadioButton("A",false);
addRadioButton("B",false);
addRadioButton("C",false); //增加四个单选按钮
addRadioButton("D",false);
choice = new Choice(); //增加一个choice组件
panel.add(choice);
add(panel,BorderLayout.NORTH);
}
public void addRadioButton(String name,boolean selected)
{
JRadioButton button = new JRadioButton(name,selected);
group.add(button);
panel.add(button);
button.addActionListener(listener2);
}
ActionListener listener1 = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
String s = textfield1.getText();
pass(s);
String text = textfield2.getText() + " " + textfield1.getText();
textfield2.setText(text);
if(evt.getSource() == textfield1)
{
textfield1.setText("");
}
}
};
ActionListener listener2 = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
String s1 = evt.getActionCommand();
String s2 = s1.toLowerCase();
find(s2);
}
};
int i = 0;
public void pass(String s)
{
str[i] = s;
i++;
}
public void find(String s)
{
for(int i = 0; i < str.length; i++)
{
if(str[i].startsWith(s))
choice.add(str[i]);
}
}
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
private JTextField textfield1;
private JTextField textfield2;
private JPanel panel;
private ButtonGroup group;
private Choice choice;
private String[] str;
}编译没问题,但在Input文本域中输入单词按回车后就有异常了。想请教高手这是怎么回事?
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at TextFrame.pass(Text1.java:101)
at TextFrame$1.actionPerformed(Text1.java:69)
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(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)
解决方案 »
- 为什么是这样呀
- 求计算器的思路以及简短有解释带吗
- 我是学VB的,谁帮我把这个简单的程序加个注释
- JAVA中有没有类似C++中eof文件结尾的标志?
- 急急急!jfreechart如何生成动态曲线图
- jsp生成验证码
- 关于javac的问题
- 请问tomcat的jsp文件和class文件分别放在哪两个目录下面?
- PART TIME合作方案
- Exception in thread "main" java.lang.NoClassDefFoundError:alysor
- [请大家帮帮我啊 ]编写一程序,读入一系列名字并将它们存储在LINKEDLIST,不能存储重复的名字,并允许用户查找一个名字
- synchronized(xxx){...};请教者关于这段程序中syncronized代码块及wait的理解
i++;
} public void find(String s) {
for (int i = 0; i < str.length; i++) {
if (str[i].startsWith(s))
choice.add(str[i]);
}
}
就会有Exception in thread "AWT-EventQueue-0 " java.lang.NullPointerException 回复4楼:我没想到那么多,不好意思。这个程序主要是在一个文本域中输入单词,在另一个文本域或文本区中添加该单词。
另外,有四个单选按钮,分别表示A、B、C、D大写字母,选择一个后在choice组件
显示所有以所选择大写字母开头的单词。上面的程序编译没问题,就是在2楼中那段代码出问题了。
我构造一个pass方法传递在文本域输入的单词,保存到数组里,但会说是空指针异常,我怎么改都不行。请高手帮一下,谢谢。
你在定义时没有定义数组长度
而再使用的时候:
str[i] = s;
肯定出错
所以你再使用的时候先为str分配空间
{
if(str != null && str.length !=0) {
for(int i = 0; i < str.length; i++)
{
if(str[i] != null) {
if(str[i].startsWith(s))
choice.add(str[i]);
}
}
}
}
再把private String[] str; 注释掉,
在choice组件中可以显示匹配的单词,但还是会跳出一个空指针异常。这是为什么?
最好不要数组,你可以用容器,例如用Vector或是List子类的