Hello everybody,
I have a trouble with my assignment . In my assignment, the application have to read the lists of text file before performing the another taks. I want my application run independently on any computer with JVM, so it must be read the text file in the jar file.
I have rearched on internet and got some stuff, but when i applied it, my application has some error:
Here is the code:

public class ReadFile {
	private String inputFile;

	public ReadFile(String inputFile) {
		this.inputFile=inputFile;
	}

	public ArrayList<String> readFile(){
                 InputStream input =null;
                 BufferedReader bufferReader=null;
		ArrayList<String> list=new ArrayList<String>();
		try {

                    input = getClass().getResourceAsStream(inputFile);
                    bufferReader = new BufferedReader(new                InputStreamReader(input));
      
                    String line="";
                    while(null != (line=bufferReader.readLine())) {
				list.add(line);
			}
			bufferReader.close();
		}
		catch(IOException e) {
			System.out.println("IOException : " + e);
		}
		return list;
	}
}

and:

public class Gui{
...
private String[] output= {
			"sentence01.txt","sentence02.txt",
			"sentence03.txt","sentence04.txt",
			"sentence05.txt","sentence06.txt",
			"sentence07.txt","sentence08.txt",
			"sentence09.txt","sentence10.txt",
			"sentence11.txt","sentence12.txt",
			"sentence13.txt","sentence14.txt",
			"sentence15.txt","sentence16.txt",
			"sentence17.txt","sentence18.txt",
			"sentence19.txt","sentence20.txt"};
private ReadFile readFile;
...
private void readSentenceFile() {
		for(int i=0;i<20;i++) {
			readFile=new ReadFile("Sentence/"+output[i]);
			ArrayList<String> temp=new        ArrayList<String>();
			temp=readFile.readFile();
			sentenceList.addAll(temp);
		}
	}
}

And here is the error:

run:
Exception in thread "main" java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:61)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
        at vn.edu.uit.assignment3.collocationdetection.ReadFile.readFile(ReadFile.java:30)
        at vn.edu.uit.assignment3.collocationdetection.Gui.readSentenceFile(Gui.java:220)
        at vn.edu.uit.assignment3.collocationdetection.Gui.<init>(Gui.java:87)
        at vn.edu.uit.assignment3.collocationdetection.Main.main(Main.java:51)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Here is my project file:
http://www.mediafire.com/?jjgoywjlg1t

Any suggestion ?
Thanks in advance.

When reading files from a jar the file name is case-sensitive, "sentence" != "Sentence". You could double check that.

When reading files from a jar the file name is case-sensitive, "sentence" != "Sentence". You could double check that.

I don't understand your mind so much.
Can you talk more about it ?
Thks in advandce.

If your code sats "sentence19.txt", and the file in the jar is called "Sentence19.txt" it won't work (but, under Windows, it will work if it's an ordinary file not in a jar). So it's a good idea to check the file names to make sure the upper case / lower case letters are the same.

If your code sats "sentence19.txt", and the file in the jar is called "Sentence19.txt" it won't work (but, under Windows, it will work if it's an ordinary file not in a jar). So it's a good idea to check the file names to make sure the upper case / lower case letters are the same.

When reading files from a jar the file name is case-sensitive, "sentence" != "Sentence". You could double check that.

Thanks, I will try.

I modified the code as JamesCherrill 's idea, in the modified method i checked the file path to ensure it is always a lowercase string. But the error still occurs.

run:
/sentence/sentence01.txt
Exception in thread "main" java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:61)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:55)
        at vn.edu.uit.assignment3.collocationdetection.ReadFile.readFile(ReadFile.java:29)
        at vn.edu.uit.assignment3.collocationdetection.Gui.readSentenceFile(Gui.java:223)
        at vn.edu.uit.assignment3.collocationdetection.Gui.<init>(Gui.java:87)
        at vn.edu.uit.assignment3.collocationdetection.Main.main(Main.java:51)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

And the modified method:

private void readSentenceFile() {
        for(int i=0;i<20;i++) {
                    String s=output[i].toLowerCase();
                    s="/sentence/"+s;
                    System.out.println(s); //for checking
                    readFile=new ReadFile(s);
                    ArrayList<String> temp=new ArrayList<String>();
                    temp=readFile.readFile();
                    sentenceList.addAll(temp);
        }
    }

As the message error, the file path is a lowercase string:
/sentence/sentence01.txt

I am a new in java (and not a English native speaker)
Who can help me ??

help me plz, i need a help

Please show the exact line of your code where the null pointer is being thrown. Print out all the variables and expressions on those lines so you can see which one is null.
Show us a listing of the jar file contents so we can see if the files are exactly as the code assumes the to be.

But your jar file does not contain the "Sentence" resource!
if you look to the structure of your project you will configure out that the "folder" Sentence is external to the project and when you build your jar it will not contain that "folder" Sentence.
If you whant the "Sentence folder to be included in the jar file remove it to the source folder :"src"; in that case it will be includded in the jar file after building the project; and this is your case.
And this

readFile=new ReadFile([B]"/Sentence/"+output[i][/B]);

means that the Sentence "folder" is in the same folder as the "vn" folder

Hope it helps

you have this structure

Assignment3_Group 16_Collocation Dectection
|
+--src --
|
+vn
|
+--Sentence
+
|--sentence01.txt
.--sentence01.txt
.
|
+--build
.
.

In this case you should put the "Sentence" in the CLASSPATH

you should have

Assignment3_Group 16_Collocation Dectection
|
+--src --
|
+vn
|
+--Sentence
|
+--build
.
.

In that case the expression

"/Sentence/"+output[i];

will be resolved successfully.

Hope it helps.

Sory the post bellow is not well formated. I repost with a document join.
Hope it helps

(deleted)

Sory the post bellow is not well formated. I repost with a document join.
Hope it helps

Hi moutanna,
Thanks for your help.
I solved the compiling error on NetBeans IDE, it means i compiled this project successfully on NetBeans, but i meet another problem: when i run the jar file which is generated in the dist folder of project, it can't run.

Here is the message occurs in the Task window of NetBeans:

init:
deps-jar:
Created dir: C:\Assignment3_Group 16_Collocation Dectection\build\classes
Created dir: C:\Assignment3_Group 16_Collocation Dectection\build\empty
Compiling 8 source files to C:\Assignment3_Group 16_Collocation Dectection\build\classes
Copying 20 files to C:\Assignment3_Group 16_Collocation Dectection\build\classes
compile:
Created dir: C:\Assignment3_Group 16_Collocation Dectection\dist
Building jar: C:\Assignment3_Group 16_Collocation Dectection\dist\Assignment3_Group_16_Collocation_Dectection.jar
Not copying the libraries.
To run this application from the command line without Ant, try:
java -jar "C:\Assignment3_Group 16_Collocation Dectection\dist\Assignment3_Group_16_Collocation_Dectection.jar"
jar:
BUILD SUCCESSFUL (total time: 0 seconds)

Here my jar file: http://www.mediafire.com/?ym5mmumqi1m
When i extract this jar, i see it has three folders: META-INF, vn and sentence.
What happen with my jar ? why it can not run ?

Thanks for reading.

Hi
I don't now the change you made; but I excute the jar file sccessfully!
Can you provide information about the message error you got.

Hi moutanna,
I am really sorry, a busy week so i can't not reply your post.
I moved the setence folder into the src folder of project, like as:

-src|
......|-sentence
......|-vn|
...........|-edu|

And I compiled successfully the project on Netbeans. But when i build project, the jar file which is generated in the dist folder of project,can't execute.
I don't understand why it can be executed in your computer, but i was not.
According the message in the Tasks Window of Netbeans, i tried to execute the jar file from command line, but the NullPointerException error was occurred.

Not copying the libraries.
To run this application from the command line without Ant, try:
java -jar "C:\Assignment3_Group 16_Collocation Dectection\dist\Assignment3_Group_16_Collocation_Dectection.jar"

And this is the error:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

c:\>java -jar "C:\Assignment3_Group 16_Collocation Dectection\dist\Assignment3_G
roup_16_Collocation_Dectection.jar"
Exception in thread "main" java.lang.NullPointerException
        at java.io.Reader.<init>(Unknown Source)
        at java.io.InputStreamReader.<init>(Unknown Source)
        at vn.edu.uit.assignment3.collocationdetection.ReadFile.readFile(ReadFil
e.java:30)
        at vn.edu.uit.assignment3.collocationdetection.Gui.readSentenceFile(Gui.
java:220)
        at vn.edu.uit.assignment3.collocationdetection.Gui.<init>(Gui.java:87)
        at vn.edu.uit.assignment3.collocationdetection.Main.main(Main.java:51)

c:\>

For more information, you can read my previous posts.
Thank you so much.
Hoping your help.

Hi again!
Can you please post the source code please?

Well it seem that the error in generated in thr readFile function my version is as follow:

public ArrayList<String> readFile() {
        BufferedReader bufferReader = null;
        InputStream input = null;
        ArrayList<String> list = new ArrayList<String>();
        try {
            input = getClass().getResourceAsStream(inputFile);
            bufferReader = new BufferedReader(new InputStreamReader(input));

            String line = "";
            while (null != (line = bufferReader.readLine())) {
                list.add(line);
            }
            bufferReader.close();
        } catch (IOException e) {
            System.out.println("IOException : " + e);
        }
        return list;
    }

It compile well and the jar excutes without error.

Hope it helps.

Hi,
The readFile method in your version is quite the same in my new version.
I want to talk again that the new version is compiled well in NetBeans IDE, but the jar file (in dist folder of project folder) can't execute.

Can you post the jar file which you compiled well ?

I also attach my new version of project. Please check it and help me solve this problem.
Thanks in advance.

Hi;
You should made this change:

readFile=new ReadFile("/[B]s[/B]entence/"+output[i]);

you have rename the package Sentence to sentence, according to JamesCherill's suggestion, without making the change in you source code.

It works for me because I did not made the renaming.
By making the modification mentioned above your version copiles and excutes succufuly.

Hope it helps.

Hi,
This is my mistake which i didn't see it. Thank you very much.
Now i knew how to access the resources from a jar file through the getResourceAsStream method yet. Maybe this is a way, have another one?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.