我的一个 java 类中出现空指针异常。我一直在看这个东西,我可以用另一双眼睛。我有 2 个类文件: FileParser:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package photouploader;
import java.nio.*;
import java.io.*;
import java.util.*;
/**
*
* @author 10339
*/
public class fileParser {
/*var Decleration*/
private File dir;
private File [] Files;
public String [] Filernames;
/*Accessor Methods*/
public File getDir(){
return dir;
}
public void SetDir(File directory){
dir=directory;
}
public File[] getFiles(){
return Files;
}
public void setFiles(File [] matches){
Files=matches.clone();
}
public String [] getFilenames(){
return Filernames;
}
public void setFilenames(){
for(int i=0;i<Files.length;i++){
System.out.println(Files[i].toString());
System.out.println(Files[i].toString().substring(Files[i].toString().lastIndexOf("\\")+1));
Filernames[i]=Files[i].toString().substring(Files[i].toString().lastIndexOf("\\")+1);
}
}
}
照片上传器类文件/主要:
package photouploader;
import javax.swing.*;
import java.io.*;
/**
*
* @author 10339
*/
public class PhotoUploader {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFileChooser j = new JFileChooser();
j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = j.showOpenDialog(null);
if(returnVal == javax.swing.JFileChooser.APPROVE_OPTION){
File dir = j.getSelectedFile();
File[] matches = dir.listFiles(new FilenameFilter()
{
public boolean accept(File dir,String name)
{
return name.endsWith(".png")||name.endsWith(".jpg");
}
});
if(matches.length==0){
JOptionPane.showMessageDialog(null,"you have not chosen "
+ "a valid directory");
}
else{
fileParser FP = new fileParser();
FP.setFiles(matches);
FP.setFilenames();
/* Form FJP = new Form();
FJP.setVisible(true);*/
NewJFrame JP = new NewJFrame();
JP.setVisible(true);
JP.update_Upload_list(FP.getFilenames());
}
}
else{
JOptionPane.showMessageDialog(null,"you have not chosen "
+ "a valid directory");
}
}
}
我试图将数据放置在 Filternames 的位置 0 处,但它仍然吓坏了。我错过了什么吗?我需要在使用前声明数组的大小吗?谢谢,斯科利斯