2014-11-01 5 views
-1

나는 그것이 프로그램이 실행되는 모든 이미지의 크기를 조정하고 거기에 새로운 디렉토리를 생성하는 일식과 함께 컴파일하면 작동하는 것 같다 친구를위한 이미지 크기를 조정하는 프로그램을 썼다 새 크기 조정 된 이미지를 폴더에 저장합니다. 내가 자바를 내보낼 때Jar null 포인터 예외 자바 프로그램을 제공

는하지만 난 여기에 내가 (충분하지 않은 평판을) 언급 할 수 있기 때문에

" for (File file : allSubFiles) {" 

도움말 pelase

public class Sample { 

/** 
* @param args 
* @throws IOException 
*/ 
public static void main(String[] args) throws IOException { 
    // TODO Auto-generated method stub 

    String a = (Sample.class.getProtectionDomain().getCodeSource().getLocation().getPath()); 
    File dir = new File(a+"resized"); 
    dir.mkdir(); 

    System.out.println(a); 
    File tempfile = new File(a); 
     File[] allSubFiles=tempfile.listFiles(); 

     for (File file : allSubFiles) { 
      if(file.isDirectory()) 
      { 

      } 
      else 
      { 
       String temp = file.getAbsolutePath(); 
       String substr = temp.substring(temp.length() - 3); 
       //System.out.println(substr); 
       if(substr.equals("png") || substr.equals("gif") || substr.equals("jpg") || substr.equals("bmp")) 
       { 
       //System.out.println(file.getAbsolutePath()+" is file"); 
       BufferedImage bufimage = ImageIO.read(file); 
       BufferedImage newImage = new BufferedImage(90, 90, BufferedImage.TYPE_INT_RGB); 

       Graphics g = newImage.createGraphics(); 
       g.drawImage(bufimage, 0, 0, 90, 90, null); 
       g.dispose(); 

       File outputfile = new File(a+"resized/"+file.getName()); 
       ImageIO.write(newImage, "png", outputfile); 
       } 
      } 
     } 


} 

} 
+1

글쎄, 디버깅은 무엇을 찾나요? –

답변

2

을 가리키는 라인 대답 널 포인터 예외가 .

유효한 디렉토리에 임시 파일 포인트 있는지 확인

Sample.class.getProtectionDomain().getCodeSource().getLocation().toURI().g‌​etPath() 

를 문제가 해결되지 않으면 시도하십시오 (isDirectory()). 프로그램 (System.out)의 출력은 무엇입니까? listFiles() 메소드는 경우에 따라 null을 리턴 할 수 있습니다.

반환 값 : 파일에 대한 자바 독 (javadoc)에서

이 추상 패스 명이 가리키는 디렉토리에있는 파일 및 디렉토리를 나타내는 추상 패스의 배열입니다. 디렉터리가 비어 있으면 배열이 비어 있습니다. 추상 경로명이 디렉토리를 나타내지 않거나 I/O 오류 이 발생하는 경우이 반환됩니다.

0

tempfile이 디렉토리가 아닌 경우이 문제가 발생할 것으로 생각됩니다. File API에 따르면 listFiles()은 표시된 파일이 디렉터리가 아닌 경우 null을 반환합니다.

또한 jar 파일은 클래스 파일 및 기타 파일의 압축 패키지이므로 tmpFile의 부모를 사용해보십시오. Eclipse에서 클래스 파일은 실제로 디렉토리에 있지만 jar 파일에서는 그렇지 않습니다. Sample.class.getProtectionDomain().getCodeSource().getLocation().getPath()은 jar 파일이있는 디렉토리가 아닌 jar 파일의 경로를 리턴 할 수 있습니다. 상위 파일을 사용하는 것은 jar가있는 디렉토리 여야합니다. 새 디렉토리 dir을 작성할 때도 마찬가지입니다. 이런 식으로 해보십시오.

public class Sample { 

/** 
* @param args 
* @throws IOException 
*/ 
public static void main(String[] args) throws IOException { 
    // TODO Auto-generated method stub 

    String a = (Sample.class.getProtectionDomain().getCodeSource().getLocation().getPath()); 
    File tempfile = new File(a).getParentFile(); 
    File dir = new File(tempFile.getPath()+"resized"); 
    dir.mkdir(); 

    System.out.println(a); 
    File[] allSubFiles=tempfile.listFiles(); 

    if(allSubFiles!=null){ 

     for (File file : allSubFiles) { 
      if(file.isDirectory()) 
      { 

      } 
      else 
      { 
       String temp = file.getAbsolutePath(); 
       String substr = temp.substring(temp.length() - 3); 
       //System.out.println(substr); 
       if(substr.equals("png") || substr.equals("gif") || substr.equals("jpg") ||   substr.equals("bmp")) 
       { 
       //System.out.println(file.getAbsolutePath()+" is file"); 
       BufferedImage bufimage = ImageIO.read(file); 
       BufferedImage newImage = new BufferedImage(90, 90, BufferedImage.TYPE_INT_RGB); 

       Graphics g = newImage.createGraphics(); 
       g.drawImage(bufimage, 0, 0, 90, 90, null); 
       g.dispose(); 

       File outputfile = new File(a+"resized/"+file.getName()); 
       ImageIO.write(newImage, "png", outputfile); 
       } 
      } 
     } 
     } 


    } 

}