• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

problem in reading a file.

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shouldn't it be:
 
Rok Štelcer
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 26775
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic