-3
String line = "";
BufferedReader f = new BufferedReader(new FileReader("test.txt"));
StringTokenizer x = new StringTokenizer(f.readLine());
int n = Integer.parseInt(x.nextToken());
for(int i = 0; i < n; i++)
{
    if(line == null)
        break;
    else
        line = f.readLine();

    StringTokenizer st = new StringTokenizer(line);

    xCoord.add(Integer.parseInt(st.nextToken()));
    yCoord.add(Integer.parseInt(st.nextToken()));
}

Here's the code. Don't know why I'm getting the exception. I have the while loop set to check if line is null.

File:

5
0 1
5 4
9 8
0 1

Getting the exception at StringTokenizer st = new StringTokenizer(line);

Exception in thread "main" java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:199)
at java.util.StringTokenizer.<init>(StringTokenizer.java:236)
at Problems.ProblemOne.main(ProblemOne.java:45)

Java Result: 1

Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
Vonais
  • 17
  • 1
  • 4

1 Answers1

3

In your code, you have the statement:

if(line == null)
            break;
        else
            line = f.readLine();

After that last piece of code, in the 'else' clause, line could equal null as you've just tried to read another line from the file. It would probably be better to have something like:

line = f.readLine();
if(line == null) break;

Based on the file contents as:

4 
0 5 
7 3 
11 -6 
0 2

The following example code works without any errors:

import java.io.*;
import java.util.*;

public class AlanTest
{
    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        String line = "";
        BufferedReader f = new BufferedReader(new FileReader("test.txt"));
        StringTokenizer x = new StringTokenizer(f.readLine());
        int n = Integer.parseInt(x.nextToken());
        for(int i = 0; i < n; i++)
        {
            line = f.readLine();
            if(line == null) break;

            StringTokenizer st = new StringTokenizer(line);
            int xCoord = Integer.parseInt(st.nextToken());
            int yCoord = Integer.parseInt(st.nextToken());
            System.out.format("%d %d%n", xCoord, yCoord);
        }
    }
}

Definitely need to know where you're getting the exception.

Alan
  • 2,962
  • 2
  • 15
  • 18
  • nice catch! And I think you should enclose this in a try-catch block – swdev Dec 14 '14 at 21:47
  • Just changed the code. What do you mean by reading another line? The text file is 4 0 5 7 3 11 -6 0 2 – Vonais Dec 14 '14 at 21:48
  • Every time you call f.readLine(), you're reading the next line from the file. If your file only has one line, you should only ever call readLine() once. – Alan Dec 14 '14 at 21:50
  • Answer edited, added some code that I think more closely matches the problem you're trying to solve. – Alan Dec 14 '14 at 21:55
  • My file has multiple lines. Sorry for the formatting. 4 is on the first line. 0 and 5 are on the second. Next line is 7 3. Next line is 11 -6. Next line is 0 2.How would I solve my problem with a multiple line file? – Vonais Dec 14 '14 at 21:55
  • In that case, your original code should work. Which line is the exception happening on? – Alan Dec 14 '14 at 22:02
  • @Vonais You should mention the file contents in the question. The fact that 4 is by itself on the first line is what's causing your error. But because you've buried it in a comment here, the people who can help you just might not see it. – Dawood ibn Kareem Dec 14 '14 at 22:10
  • @DavidWallace if I'm reading the code correctly, it should actually handle the 4 OK - it's reading a line, parsing out the 4, and then reading 4 lines from the file. – Alan Dec 14 '14 at 22:13
  • Oh, my mistake, sorry. I was looking at your code, @Alan (which would fail) instead of the code in the question. It doesn't change the fact that Vonais would be well-advised to update the question to show the file contents. – Dawood ibn Kareem Dec 14 '14 at 22:14
  • Yep, would definitely have been useful to see the file contents. I've removed my incorrect example, to save confusion. – Alan Dec 14 '14 at 22:16
  • I've added the file. Thanks for the help so far – Vonais Dec 14 '14 at 22:30
  • Added the exception. – Vonais Dec 14 '14 at 22:35
  • 1
    Did you notice my comment under your question? Your data states 5 lines, and then has 4 lines of data, which will cause this. – Alan Dec 14 '14 at 22:36