2016-11-29 50 views
0

私のプロジェクト用に実行可能なjarファイルを作成しています。Eclipseでressourceを使用しようとするとNullPointerExceptionが発生する

コード

public class StandingsCreationHelper 
{ 
    private static final String TEMPLATE_FILENAME = "Standings_Template.xls"; 

public static void createStandingsFile() throws Exception 
{ 
    StandingsCreationHelper sch = new StandingsCreationHelper(); 

    // Get the file from the resources folder 
    File templateFile = new File("TemporaryPlaceHolderExcelFile.xls"); 
    OutputStream outputStream = new FileOutputStream(templateFile); 
    IOUtils.copy(sch.getFile(TEMPLATE_FILENAME), outputStream); 
    outputStream.close(); 
} 
} 

public InputStream getFile(String fileName) 
{ 
    return this.getClass().getClassLoader().getResourceAsStream(fileName); 
} 

public static void main(String[] args) throws Exception 
{ 
    createStandingsFile(); 
} 

プロジェクトの構造

enter image description here

質問

私は実行可能なjarファイルに私のコードをパッケージ、私のプログラム問題なく実行されます。しかし、私のIDE(Eclipse)からmainメソッドを呼び出すと、リソースが見つからないかのように、次のエラーメッセージが表示されます。

スレッド "main"の例外java.lang.NullPointerException at org.apache .poi.util.IOUtils.copy事前のためのstandings.StandingsCreationHelper.main(StandingsCreationHelper.java:222)でstandings.StandingsCreationHelper.createStandingsFileで(IOUtils.java:182) (StandingsCreationHelper.java:153)

感謝どんな助けでも!

+0

'' /resources/Standings_Template.xls '; '??? –

+0

迅速な回答ありがとうございます。上記の文字列に変更すると、まだNULLポインタ例外が返されます。 –

+0

null、入力または出力は何ですか? –

答えて

2

ファイルへの絶対パスが必要なgetClassLoader()を使用しています。

変更:

public InputStream getFile(String fileName) 
{ 
    return this.getClass().getClassLoader().getResourceAsStream(fileName); 
} 

public InputStream getFile(String fileName) 
{ 
    return this.getClass().getResourceAsStream(fileName); 
} 

には、今、あなたはあなたのクラスから見た相対パスを、使用することができます。コメントに記載されているように TEMPLATE_FILENAME"resources/Standings_Template.xls"に変更することを忘れないでください。

+0

あなたは行く! 1 + –

+0

ありがとうございました!残念ながら、この変更によりNullPointerExceptionが発生します。 –

+1

先頭にスラッシュを入れないでください。TEMPLATE_FILENAME = "resources/Standings_Template.xls" – Daniel

関連する問題