空指针异常 求各位大哥们看一下

qwertry1234 2011-11-04 02:58:36
package 搜索器;
import org.apache.lucene.document.*;
import org.apache.lucene.search.*;
import org.apache.lucene.index.*;
import java.util.Iterator;
import java.util.Date;
import java.io.*;
import javax.swing.*;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.*;
public class LuceneSearcher
{
private JTextField jtfa;
private JButton jba;
private JTextField jtfb;
private JButton jbb;
private JButton jbc;
private static JTextArea jta;
private JTextField jtfc;
private JButton jbd;
private JButton jbe;
private void createAndShowGUI()
{
//java感觉
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("创新大赛-李雨晴");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Container con = frame.getContentPane();
con.setLayout(new BorderLayout());

JPanel jpup = new JPanel();
jpup.setLayout(new GridLayout(2,2));
jtfa = new JTextField(30);
jba = new JButton("选择索引存放路径");
jba.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int r = fc.showOpenDialog(null);
if(r==JFileChooser.APPROVE_OPTION)
{
jtfa.setText(fc.getSelectedFile().getPath());
}
}
}
);
jtfa = new JTextField(30);
jbb = new JButton("搜索");
jbb.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String indexPath = jtfa.getText();
String phrase = jtfb.getText();
new LuceneSearcherTool().searches(phrase,indexPath);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"搜索失败!","提示",JOptionPane.ERROR_MESSAGE);
}
}
}
);
jpup.add(jtfa);
jpup.add(jba);
jpup.add(jtfb);
jpup.add(jbb);
jta = new JTextArea(10,30);
JScrollPane jsp = new JScrollPane(jta);
JPanel jpdown = new JPanel();
jpdown.setLayout(new FlowLayout());
jtfc = new JTextField(35);
jbd = new JButton("设定导出路径");
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jbd.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int r = fc.showOpenDialog(null);
if(r==JFileChooser.APPROVE_OPTION)
{
jtfc.setText(fc.getSelectedFile().getPath());
}
}
}
);
jbe = new JButton("导出搜索结果");
jbe.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
File f = new File(jtfc.getText());
FileWriter fw = new FileWriter(f);
PrintWriter pw = new PrintWriter(fw);
pw.write(jta.getText());
pw.flush();
pw.close();
JOptionPane.showMessageDialog(null,"写入文件成功!","提示",JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(null, "写入文件失败!","提示",JOptionPane.ERROR_MESSAGE);
}
}
}
);
jpdown.add(jtfc);
jpdown.add(jbd);
jpdown.add(jbe);
con.add(jpup, BorderLayout.NORTH);
con.add(jsp, BorderLayout.CENTER);
con.add(jpdown, BorderLayout.SOUTH);
frame.setSize(200,100);
frame.pack();
frame.setVisible(true);
}


public static void main(String[] args)
{
SwingUtilities.invokeLater
(
new Runnable()
{
public void run()
{
new LuceneSearcher().createAndShowGUI();
}
}
);
}

//使用内部类LuceneSearcherTool来实现索引工作。
//这样就可以把索引建立的情况反映在文本框里面
static class LuceneSearcherTool
{
public static void searches(String phrase,String indexPath)throws IOException
{
//建立搜索器
IndexSearcher searcher = new IndexSearcher(indexPath);
//搜索TEXT字段
Term t = new Term("Text",phrase);
//生成Query对象
Query q = new TermQuery(t);
//获得搜索结果对象
Hits hs = searcher.search(q);
//搜索到的结果数量
int num = hs.length();
jta.setText("检索到的记录数量:" + num + "\n");
jta.setText(jta.getText() + "***************************" + "\n\n");
//输出搜索结果
for(int i = 0;i<num;i++)
{
//获得Docunment
Document doc = hs.doc(i);
if(doc==null)
{
continue;
}
Field field = doc.getField("filename");
String filename = field.stringValue();
//uri
field = doc.getField("uri");
String uri = field.stringValue();
//cdate
field=doc.getField("cdate");
String cdate = field.stringValue();
//digest
field=doc.getField("digest");
String digest = field.stringValue();
StringBuffer sb = new StringBuffer();
sb.append("uri" + uri +"\n");
sb.append("filename" + filename +"\n");
sb.append("cdate" + cdate +"\n");
sb.append("digest" + digest +"\n");
sb.append("**********************" +"\n");
jta.setText(jta.getText()+sb.toString());
}
//关闭搜索器
searcher.close();
}
}

}
...全文
159 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qwertry1234 2011-11-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lucky_bw 的回复:]
空指针异常,jtfb没赋值,在createAndShowGUI()里加上jtfb = new JTextField(30);
[/Quote]
谢谢这位仁兄
Lucky_BW 2011-11-04
  • 打赏
  • 举报
回复
空指针异常,jtfb没赋值,在createAndShowGUI()里加上jtfb = new JTextField(30);
qwertry1234 2011-11-04
  • 打赏
  • 举报
回复
异常是Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1014)
at java.awt.Container.add(Container.java:348)
at 搜索器.LuceneSearcher.createAndShowGUI(LuceneSearcher.java:77)
at 搜索器.LuceneSearcher.access$4(LuceneSearcher.java:25)
at 搜索器.LuceneSearcher$5.run(LuceneSearcher.java:144)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧