• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in reading a file.

 
Ankit Tripathi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to read the frequency of each character through my java program.It has compiled successfully but when i am running it,it is showing me null pointer exception.what is the
problem with it.




import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class FileInput {
char ch;int count;

public static void main(String[] args) {
FileInput sts = new FileInput();



InputStream is = sts.getClass

().getResourceAsStream("F:/a.txt");


System.out.println

(sts.convertStreamToString(is));
}

public String convertStreamToString

(InputStream is) {

BufferedReader reader = new

BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null )

{
sb.append(line + "\n");
if(ch=='E')
{
count=count+1;
}
System.out.println(count);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return sb.toString();
}
}
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I did a bit of a formating (yours job actually) ... learn how it is done.


Anyhow, regarding your problem.
Method getResourceAsStream returns null in case the resource is not found.
You have to specify a vaild URL name in order to work ... at lesat for getResourceAsStream() or getResource().openStream().


Regards,
Rok
 
Somnath Mallick
Ranch Hand
Posts: 482
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shouldn't it be:
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Shouldn't it be:
InputStream is = sts.getClass().getResourceAsStream("F:\\a.txt");

This is not a valid URL file name.
Check the following link:
http://java.sun.com/docs/books/tutorial/networking/urls/index.html

Regarding the file reading, check the following link (various examples):
http://www.google.si/search?hl=sl&client=firefox-a&rls=org.mozilla%3Asl%3Aofficial&hs=ZER&q=java+how+read+a+file&btnG=Iskanje&meta=&aq=f&oq=


Regards,
Rok
 
Paul Clapham
Sheriff
Pie
Posts: 20725
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the "getResourceAsStream" method searches your classpath for the resource, and the URL you provide is relative to the class on which you call the method. Unless you put a slash at the beginning of the URL, in which case the URL is considered to be in the root of one of the entries of the classpath.

But if you want to specify a file name, the easiest change to make would be to not use that method, but to use an ordinary FileInputStream.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic