Results 1 to 8 of 8
- 07-17-2012, 06:25 PM #1
NullPointerException when reading a file
Well, I was trying to figure out how to read codepoints, as part of my learning curve to understand Unicode characters, however, I am getting a NullPointerException when trying to read a file named input.txt which is placed in the same directory where I am typing code:
input.txtJava Code:import java.io.*; class CharacterPlay { public static void main (String[] args) throws FileNotFoundException { try { BufferedReader bufReader = new BufferedReader( new InputStreamReader(new FileInputStream("input.txt"), "UTF-16")); final int max_characters_per_file = 1000; int[] codepoint = new int[max_characters_per_file]; for (int i = 0; i < max_characters_per_file; i++) { System.out.println(bufReader.readLine()); codepoint[i] = bufReader.readLine().codePointAt(i); } } catch (NullPointerException e) {e. printStackTrace();} catch (Exception e) {e.printStackTrace();} } }
sometext and characters...
- 07-17-2012, 06:30 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
Re: NullPointerException when reading a file
Where in your code happened that NullPointerException? The exception stack trace tells you exactlly where it happened. You may want to post this message here.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-17-2012, 08:08 PM #3
- 07-17-2012, 08:12 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
Re: NullPointerException when reading a file
You do realize that line #16 reads another line from the BufferedReader each time it executes do you? (and you don't do anything with it except printing it).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-19-2012, 01:51 PM #5
Re: NullPointerException when reading a file
Yeah, it is just to see what code point is read, now I have updated it, however, it still reads garbage as far as I can see...
Java Code:import java.io.*; class CharacterPlay { public static void main (String[] args) throws FileNotFoundException { try { BufferedReader bufReader = new BufferedReader( new InputStreamReader(new FileInputStream("input.txt"), "UTF-16")); final int max_characters_per_file = 1000; int[] codepoint = new int[max_characters_per_file]; for (int i = 0; i < max_characters_per_file; i++) { String buf = bufReader.readLine(); codepoint[i] = buf.codePointAt(i); System.out.println(codepoint[i] + " " + buf); } } catch (NullPointerException e) {e. printStackTrace();} catch (Exception e) {e.printStackTrace();} } }
- 07-19-2012, 02:42 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: NullPointerException when reading a file
Each time you loop (0..max_characters_per_file) you read a line then pick a character (codepoint) from that line that matches the current iteration.
Um...why?Please do not ask for code as refusal often offends.
- 07-19-2012, 03:15 PM #7
Re: NullPointerException when reading a file
I want to write the codepoint and the character(s) being read. This was part of trying to understand how Java reads and interprets Unicode characters with various character sets.
For example, I want to translate English to Chinese, and, I want to read a text file in English, process grammatical and language rules and return the text in Chinese. I assumed that I would have to use the underlying character sets to have my engine learn the alphabet first, and, then, learn Chinese so I can code the grammar rules. I was thinking of using Google Translate API to do that, which I have looked into at a cursory level as yet, so any guidelines would help me create this project which I have to deliver in 2014 :~)
Another idea that I was thinking about was to interface the library of the private school I am attending to be able to provide information about books on sale from Amazon.co.uk (and perhaps other portals). The portal (Login to St Martin's Institute of IT Intranet Website), which seems to be written in ASP.NET would need to interface with AWS (https://aws.amazon.com/what-is-aws/), and, I am also wondering how to do this using either Java or .NET, since both APIs are possible.
I am slightly worried about being charged for using web services as well, so I need to find a sponsor :~)
- 07-19-2012, 04:40 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: NullPointerException when reading a file
OK, so to take that code snippet up there, if I have a file consisting of
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
as an example (until the whatever exception for trying to codepoint a character that doesn't exist anyway)...
The BufferedReader will read that first line and will pick out '1', since that is charAt(0).
The next time round that loop the BufferedReader reads line 2 and, since the index 'i' is now 1, will pick out '2'.
Then around again and line 3, picking out '3'...and so on.
It's not reading all the stuff on a single line...it's just picking out 1 character.Please do not ask for code as refusal often offends.
Similar Threads
-
Why do I get java.lang.NullPointerException only for a specific file ?
By aneuryzma in forum New To JavaReplies: 3Last Post: 03-28-2011, 01:16 PM -
NullPointerException in FileInputStream text file
By cheskers11 in forum EclipseReplies: 1Last Post: 11-19-2009, 01:38 PM -
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 10:52 AM -
[SOLVED] java.lang.NullPointerException when reading getClass().getResourceAsStream(.
By jon80 in forum New To JavaReplies: 2Last Post: 05-31-2009, 05:03 PM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks