| Author |
Path Problem..Please help
|
yogi ta dev
Greenhorn
Joined: Nov 25, 2004
Posts: 9
|
|
Hi, I have some problem in the path.I am supposed to pick .java files from the src directory..The src directory could be under C:/hw_4/src ,the user should be able to select at runtime..But I am able to pick only the .java files from src directly under C: which is C:/src..The following is the code..I have tried too much but can't come up with a solution..Really appreciate any help..The error that I get is nullpointerexceptio.. package MainPk; import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.filechooser.*; import java.io.File; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileView; import java.util.*; import Model.*; public class TestFrame extends JFrame { private String description = " "; private ArrayList extensions = new ArrayList(); public File[] f1; public File[] cf1; ArrayList list; public File[] f; public String namepath,newpath,parentdir; public File name; private JLabel label; public JFileChooser fc = new JFileChooser(); public ExtensionFileFilter filter = new ExtensionFileFilter(); String str[]; int count=0; public TestFrame() { setTitle("FileChooser"); setSize(400,400); JMenuBar menuBar=new JMenuBar(); setJMenuBar(menuBar); JMenu menu=new JMenu("File"); JMenuItem openItem=new JMenuItem("Open"); menu.add(openItem); openItem.addActionListener(new FileOpenListener()); JMenuItem exitItem=new JMenuItem("Exit"); menu.add(exitItem); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); label = new JLabel(); Container contentPane = getContentPane(); contentPane.add(label); fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); menuBar.add(menu); } private class FileOpenListener implements ActionListener { public void actionPerformed(ActionEvent event) { //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); ExtensionFileFilter filter = new ExtensionFileFilter(); filter.addExtension("java"); filter.setDescription("Java files"); fc.setFileFilter(filter); int result = fc.showOpenDialog(TestFrame.this); if(result == JFileChooser.APPROVE_OPTION) { name =fc.getSelectedFile(); String name1=name.getName(); File file=new File(name1); namepath= name.getAbsolutePath(); cf1=visitAllDirsAndFiles(file); } } } public File[] visitAllDirsAndFiles(File dir) { if (dir.isDirectory()) { File[] children = dir.listFiles(); list =new ArrayList(100); for (int i=0;i<children.length; i++) { f1 = children[i].listFiles(); for(int j=0;j<f1.length;j++) { list.add(f1[j]); } } } else dir=dir.getParentFile(); File[] cf=new File[list.size()]; list.toArray(cf); return cf; } class ExtensionFileFilter extends FileFilter { public void addExtension(String extension) { if(!extension.startsWith(".")) extension = "." + extension; extensions.add(extension.toLowerCase()); } public void setDescription(String aDescription) { description = aDescription; } public String getDescription() { return description; } public boolean accept(File f) { if(f.isDirectory()) return true; String name=f.getName().toLowerCase(); for(int i=0;i<extensions.size();i++) if(name.endsWith((String)extensions.get(i))) return true; return false; } } } Thanks, yogita
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
Can you copy and paste the exact error message you get? Also, it would help if you can indicate which line in your code causes the error. Thanks, Layne
|
Java API Documentation
The Java Tutorial
|
 |
yogi ta dev
Greenhorn
Joined: Nov 25, 2004
Posts: 9
|
|
Hi, This is the error message java.lang.NullPointerException and the error is at the following line:115 which is File[] cf=new File[list.size()]; Thanks, yogita
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
You are getting a NullPointerException because list is null, so you cannot call the size method. It looks like you initialize list in an if statement and this line that causes the problem is in the corresponding else. You should probably look closely at the logic you are using in visitAllDirsAndFiles() to find out when list is being initialized. Please let us know what you find and if you need any more help. Keep Coding! (TM) Layne p.s. When you post more code in the future, please use the UBB CODE tags. There are buttons to help you just below the message edit box when you make a reply or post a new message.
|
 |
 |
|
|
subject: Path Problem..Please help
|
|
|