【问题标题】:Jar giving null pointer exception java programjar给出空指针异常java程序
【发布时间】:2014-11-01 21:52:34
【问题描述】:

我为一个朋友写了一个调整图像大小的程序,如果我用 eclipse 编译它似乎可以工作,它只是调整正在执行程序的所有图像的大小,并在那里创建一个新目录并将新的缩放图像保存到文件夹。

但是当我在java中导出它时,我得到一个空指针异常指向的行

 "  for (File file : allSubFiles) {"

请帮忙

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);
              }
          }
      }


}

 }

【问题讨论】:

  • 好吧,你的调试发现了什么?

标签: java eclipse jar scale executable-jar


【解决方案1】:

因为我不能在这里评论(没有足够的声誉)一个答案。

请尝试

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

如果这不起作用,请检查 tempfile 是否指向有效目录 (isDirectory())。程序(System.out)的输出是什么? listFiles() 方法在某些情况下可能会返回 null。

来自文件的 javadoc (javadoc):

返回: 一组抽象路径名,表示由该抽象路径名表示的目录中的文件和目录。这 如果目录为空,则数组将为空。 如果是这样,则返回 null 抽象路径名不表示目录,或者如果 I/O 错误 发生

【讨论】:

    【解决方案2】:

    我相信如果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);
                  }
              }
             }
            }
    
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-06
      • 2014-07-19
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多