Results 1 to 8 of 8
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default 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:

    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++)
    		{
    			System.out.println(bufReader.readLine());
    			codepoint[i] = bufReader.readLine().codePointAt(i);
    		}
    
    		}
    		catch (NullPointerException e) {e. printStackTrace();}
    		catch (Exception e) {e.printStackTrace();}
    
    	}
    }
    input.txt
    sometext and characters...

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default Re: NullPointerException when reading a file

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    Line 16

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default 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();}
    
        }
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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.

    ** This space for rent **

  7. #7
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default Re: NullPointerException when reading a file

    Quote Originally Posted by Tolls View Post
    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?
    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 :~)

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 03-28-2011, 01:16 PM
  2. NullPointerException in FileInputStream text file
    By cheskers11 in forum Eclipse
    Replies: 1
    Last Post: 11-19-2009, 01:38 PM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 2
    Last Post: 05-31-2009, 05:03 PM
  5. Replies: 3
    Last Post: 05-09-2009, 11:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •