2009-03-31 7 views
4

私はPDFにTXTを変換するために、このテストを使用します。ファイルを読み込もうとするとNullPointerExceptionが発生するのはなぜですか?

package convert.pdf; 

//getResourceAsStream(String name) : Returns an input stream for reading the specified resource. 
//toByteArray : Get the contents of an InputStream as a byte[]. 

import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.commons.io.IOUtils; 

import convert.pdf.txt.TextConversion; 

public class TestConversion { 

    private static byte[] readFilesInBytes(String file) throws IOException { 
     return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file)); 
    } 

    private static void writeFilesInBytes(byte[] file, String name) throws IOException { 
     IOUtils.write(file, new FileOutputStream(name)); 
    } 

    //just change the extensions and test conversions 
    public static void main(String args[]) throws IOException { 
     ConversionToPDF algorithm = new TextConversion(); 
     byte[] file = readFilesInBytes("/convert/pdf/text.txt"); 
     byte[] pdf = algorithm.convertDocument(file); 
     writeFilesInBytes(pdf, "text.pdf"); 
    } 

} 

問題:

 
Exception in thread "main" java.lang.NullPointerException 
    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1025) 
    at org.apache.commons.io.IOUtils.copy(IOUtils.java:999) 
    at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:218) 
    at convert.pdf.TestConversion.readFilesInBytes(TestConversion.java:17) 
    at convert.pdf.TestConversion.main(TestConversion.java:28) 

私はデバッガを使用し、問題はここに配置されているようだ:

private static byte[] readFilesInBytes(String file) throws IOException { 
     return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file)); 
    } 

何私の問題ですか?

答えて

1

readFilesInBytes()にファイルを渡す前にファイルが存在するかどうかを確認していますか?ファイルが見つからない場合、Class.getResourceAsStream()nullを返します。あなたはおそらく何をしたい:

private static byte[] readFilesInBytes(String file) throws IOException { 
    File testFile = new File(file); 
    if (!testFile.exists()) { 
     throw new FileNotFoundException("File " + file + " does not exist"); 
    } 
    return IOUtils.toByteArray(TestConversion.class.getResourceAsStream(file)); 
} 

またはより良いまだ:リソースのような

private static byte[] readFilesInBytes(String file) throws IOException { 
    InputStream stream = TestConversion.class.getResourceAsStream(file); 
    if (stream == null) { 
     throw new FileNotFoundException("readFilesInBytes: File " + file 
             + " does not exist"); 
    } 
    return IOUtils.toByteArray(stream); 
} 
17

サウンドは、おそらくその名前で存在していません。

Class.getResourceAsStream()は、そのクラスのパッケージに関連するリソースを見つけますが、ClassLoader.getResourceAsStream()はそうではありません。あなたはそう

Foo.class.getResourceAsStream("/bar.png") 

Foo.class.getClassLoader().getResourceAsStream("bar.png") 

とほぼ同等で、これを模倣するClass.getResourceAsStream()に先頭のスラッシュを使用することができ、このはファイル(すなわち、通常のファイルシステム上の特定のファイル)実際ですあなたがロードしようとしている?もしそうなら、FileInputStreamを使う方が良いでしょう。 jarファイルまたはクラスパスにバンドルされているリソースの場合は、Class.getResourceAsStream()を使用してください。それがファイルシステムのどこにでもある可能性のある任意のファイルであれば、FileInputStreamを使用してください。

EDIT:今まで私に問題を引き起こしていたことに気をつけなければならないこと - これはWindowsであるdevboxボックスでうまくいけば、今はUnixであるプロダクションサーバでは失敗するファイル名の大文字小文字。大文字と小文字を区別して扱うファイルシステムでは、異なるファイルシステムが痛みを伴う可能性があります。

0

このクラスはクラスパス内のTXTファイルを読み取り、TextConversionを使用してPDFに変換した後、pdfをファイルシステムに保存します。

ここTextConversionコード:

package convert.pdf.txt; 
//Conversion to PDF from text using iText. 
import java.io.BufferedReader; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 

import convert.pdf.ConversionToPDF; 
import convert.pdf.ConvertDocumentException; 

import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.Font; 
import com.lowagie.text.Paragraph; 
import com.lowagie.text.pdf.PdfWriter; 

public class TextConversion implements ConversionToPDF { 

    public byte[] convertDocument(byte[] documents) throws ConvertDocumentException { 
     try { 
      return this.convertInternal(documents); 
     } catch (DocumentException e) { 
      throw new ConvertDocumentException(e); 
     } catch (IOException e) { 
      throw new ConvertDocumentException(e); 
     } 
    } 

    private byte[] convertInternal(byte[] documents) throws DocumentException, IOException { 
     Document document = new Document(); 

     ByteArrayOutputStream pdfResultBytes = new ByteArrayOutputStream(); 
     PdfWriter.getInstance(document, pdfResultBytes); 

     document.open(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(documents))); 

     String line = ""; 
     while ((line = reader.readLine()) != null) { 
      if ("".equals(line.trim())) { 
       line = "\n"; //white line 
      } 
      Font fonteDefault = new Font(Font.COURIER, 10); 
      Paragraph paragraph = new Paragraph(line, fonteDefault); 
      document.add(paragraph); 
     } 

     reader.close(); 

     document.close(); 

     return pdfResultBytes.toByteArray(); 
    } 
} 

そしてここでConversionToPDFのコード:

package convert.pdf; 
// Interface implemented by the conversion algorithms. 
public interface ConversionToPDF { 

    public byte[] convertDocument(byte[] documentToConvert) throws ConvertDocumentException; 
    } 

私は問題が私のファイルシステムから来ると思います(Windowsおよびサーバー上のdevboxがUNIXです)。 クラスパスを変更しようとします。

0

この問題は、test.txtのメソッドを呼び出すと発生する可能性があります。これは、フォルダのショートカットにすることができます。つまり、存在しないファイルに対してメソッドを呼び出すと、結果はNullPointerExceptionとなります。

関連する問題