在我正在进行的项目中,我需要从文本框和JFileChooser中的一些选定文件中获取输入...我希望将文件的arrayList传递给另一个必须处理它的类..但是我在这里得到一个nullPointerException ...这是一个新的java所以真的不知道如何解决这个问题...: - / plz help !! 我也包括了索引类:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*; //notice javax
public class home extends JFrame
{
JLabel lab=new JLabel("MiniGoogle",JLabel.CENTER);
JLabel lab2=new JLabel("Select Files:",JLabel.CENTER);
JLabel lab3=new JLabel("Enter Keywords",JLabel.CENTER);
JPanel pane = new JPanel(new BorderLayout(0,50));
JButton search = new JButton("Search!");
JTextField kw=new JTextField(25);
JButton Browse=new JButton("Browse");
JFileChooser fc= new JFileChooser();
ArrayList files=new ArrayList();
String dest;
home() // the frame constructor method
{
setBounds(100,100,350,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane(); // inherit main frame
con.add(pane);
JPanel s=new JPanel(new FlowLayout());
JPanel r=new JPanel(new FlowLayout());
search.setActionCommand("search");
r.add(lab2);
r.add(Browse);
r.add(lab3);
r.add(kw);
pane.add ("North", lab);
pane.add ("Center", r);
s.add(search);
pane.add("South",s);
setVisible(true); // display this frame
Browse.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
fc.setCurrentDirectory(new java.io.File("C:/Ananya/Files/DSA/project 2/searchfiles"));
fc.setDialogTitle(dest);
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fc.setAcceptAllFileFilterUsed(false);
if (fc.showOpenDialog(fc) == JFileChooser.APPROVE_OPTION) {
files.addAll(Arrays.asList(fc.getSelectedFiles()));
}
else {
System.out.println("No Selection ");
}
}
});
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String text=kw.getText();
if(text.equals("")||files==null)
{
JOptionPane.showMessageDialog(null,"Empty fields!","Error!",JOptionPane.WARNING_MESSAGE);
}
else
{
try{
new Indexing(files,text);/*this is where the exception is thrown*/
}catch(Exception ex)
{
System.out.println(ex+"home");
}
}
}
});
}
public static void main(String args[])throws IOException {
new home();
}
}
这是我的索引类:
public class Indexing{
HashMap map=new HashMap();
int n;
ArrayList temp;
String s;
public Indexing(ArrayList files,String kw)throws IOException
{
for(int i=0;i<2;i++)
{
temp=new ArrayList(50);
BufferedReader br = new BufferedReader(new FileReader((File)files.get(i)));
String line = "", str = "";
while ((line = br.readLine()) != null) {
str += line + " ";
}
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens()) {
temp=new ArrayList(50);
s = st.nextToken();
s.trim();
s.toLowerCase();
char ch=s.charAt(s.length()-1);
if(!(Character.isLetterOrDigit(ch)))
s=s.substring(0,s.length()-1);
if(map.get(s)==null)
{
temp.add(files.get(i));
map.put(s,temp);
}
else if(!(((ArrayList)map.get(s)).contains(files.get(i))))
{
((ArrayList)map.get(s)).add(files.get(i));
}
}
}
ArrayList result=(ArrayList)map.get(kw);
new SearchResults(result,kw); //line 43
}
}
这是SearchResults类:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class SearchResults {
JFrame jtfMainFrame;
JLabel msg;
JPanel p;
JPanel s;
File fl;
JButton buttons[];
JLabel files[];
public SearchResults(ArrayList results,String kw) {
jtfMainFrame = new JFrame(kw+" - MiniGoogle Search");
jtfMainFrame.setSize(50, 50);
p=new JPanel(new BorderLayout(0,60));
msg=new JLabel(("Results for "+kw+":"),JLabel.CENTER);
p.add("North",msg);
s=new JPanel(new FlowLayout());
int l=results.size(); //line 22
for(int i=0;i<l;i++)
{
fl=(File)results.get(i);
files[i].setText((i+1)+"."+ fl);
buttons[i]=new JButton("Go!");
s.add(files[i]);
s.add(buttons[i]);
buttons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String[] commands = {"cmd", "/c",fl.getAbsolutePath()};
try{Runtime.getRuntime().exec(commands);}catch(Exception ex)
{
System.out.println(ex);
}
}
});
}
p.add("Center",s);
p.add("South",msg);
jtfMainFrame.setBounds(100,100,350,350);
jtfMainFrame.setVisible(true);
}
public static void main(String[] args) {
// Set the look and feel to Java Swing Look
try {
UIManager.setLookAndFeel(UIManager
.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
System.out.println(e);
}
}
}
这是异常追踪:
java.lang.NullPointerException
at SearchResults.<init>(SearchResults.java:22)
at Indexing.<init>(Indexing.java:46)
at home$2.actionPerformed(home.java:75)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
答案 0 :(得分:0)
我很确定NPE出现在这一行:
files.addAll(Arrays.asList(fc.getSelectedFiles()));
检查fc.getSelectedFiles()
是否不返回null。
答案 1 :(得分:0)
似乎异常发生在这一行:
fc.setDialogTitle(dest);
首先为dest
分配一个值:
dest = "The Title!";
答案 2 :(得分:0)
类Indexing
中的映射似乎有误。在第42行中,您根据关键字ArrayList
获取kw
。但是,Map
不包含此类关键字并返回null
。 SearchResult
尝试在该列表上调用size()
方法并正确地抛出NullPointerException
。
我们无法真正解决您的问题,但这就是原因。您需要在某处修复映射逻辑或添加其他null
检查。无论什么是合适的。