-1

I am trying to get all the text files from a location then checking with every file name (ACMEMM_ADDITIONAL_ATTRIBUTE_02172015_075611) then trimming it to 02172015_075611.After doing this I am inserting every file name into string array , but when I am doing this I am getting Null pointer exception at mr[i]=s;

import java.io.File;

public class dog{
public void listFiles(){
File directory = new File("C://Users//422405//Desktop//Az_Support_Process//New folder (3)");

//get all the files from a directory

File[] fList = directory.listFiles();
System.out.println(fList.length);
int count=0;
int i=0;
String s;
String [] mr=null;
for (File file : fList){

if (file .isFile()){
count++;

s=file.getName().replaceAll("[^0-9]", "");;
System.out.println(s);

mr[i]=s;

i++;
//System.out.println(i);
}}

//System.out.println(mr.length);
if(count==20){

}
System.out.println(count);
}

public static void main(String[] args)
{
System.out.println("Hello");
dog d=new dog();
d.listFiles();

}

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mohit Darmwal
  • 275
  • 1
  • 5
  • 21
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jens Feb 18 '15 at 09:56
  • `String [] mr= new String[fList.length]` will do the rest – Bhargav Modi Feb 18 '15 at 09:59

1 Answers1

0

You have to initialize the mr array:

String [] mr = new String[fList.length];

Read More about this exception here and how to fix.

Community
  • 1
  • 1
Salah
  • 8,567
  • 3
  • 26
  • 43