Java文件输入 - NullPointerException

时间:2014-01-17 23:23:14

标签: java file-io nullpointerexception string-split

我的代码如下:

package examen2;

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

public class Examen2 {

    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("dataIn.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

        TreeSet<Punct> set = new TreeSet();
        String line;

//problem in the while statement

        while (((line = br.readLine()).length() != 0)) {
            String[] splited = line.split("([^0-9\\n\\r\\-][^0-9\\n\\r\\-]*)");
            int[] number = new int[splited.length];

            for (int i=0, j=0; i<splited.length; i++) {
                number[j] = Integer.parseInt(splited[i]);
                j++;
            }

            set.add(new Punct(number[0], number[1]));

            Iterator it = set.iterator();

            while (it.hasNext()) {
                System.out.print(it.next());
            }
            System.out.println();
        }

        br.close();
        br = null;
        fis = null;
    }

    static class Punct implements Comparable {

        int x;
        int y;

        Punct() {
            x = 0;
            y = 0;
        }
        Punct(int x, int y) {
            this.x = x;
            this.y = y;
        }

         @Override
        public String toString() {
            return "(" + this.x + ":" + this.y + ")";
        }
        @Override
        public boolean equals(Object o) {
            try {
                Punct other = (Punct)o;
                return (this.x==other.x && this.y==other.y);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
            return false;
        }
        @Override
        public int compareTo(Object t) {
            Punct other = (Punct)t;
            if (this.x == other.x  &&  this.y == other.y) {
                return 0;
            } else if (Math.sqrt(Math.pow(this.x, 2)+Math.pow(this.y, 2))-Math.sqrt(Math.pow(other.x, 2)+Math.pow(other.y, 2))>0) {
                return 1;
            } else {
                return -1;
            }
        }

        @Override
        public int hashCode() {
            return super.hashCode(); //To change body of generated methods, choose Tools | Templates.
        }
        @Override
        protected void finalize() throws Throwable {
            super.finalize(); //To change body of generated methods, choose Tools | Templates.
        }
        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone(); //To change body of generated methods, choose Tools | Templates.
        }

   }

}

dataIn.txt内容:

  

1 2

     

3 assfas 4

     

5 asfl; a 8

     

1 1

     

3 4

并将其写入控制台:

  

(1:2)

     

(1:2)(3:4)

     

(1:2)(3:4)(5:8)

     

(1:1)(1:2)(3:4)(5:8)

     

(1:1)(1:2)(3:4)(5:8)

     

线程“main”中的异常java.lang.NullPointerException

    at examen2.Examen2.main(Examen2.java:15)
     

Java结果:1成功建立(总时间:0秒)

这是明天考试中会出现什么问题的一个例子。

我必须从输入文件的每一行读取一对数字。我认为问题不在于正则表达式,而在于对结果的解释,但我找不到解决方案。

2 个答案:

答案 0 :(得分:2)

你从哪里拿到这行代码?

while (((line = br.readLine()).length() != 0)) {

这并不好,因为你最终会在空对象上调用length()

而是检查该行是否为空,并在while条件中使用该行。

while((line=br.readLine())!=null) {
 //....
}

答案 1 :(得分:1)

NullPointerException来自这一行:

while (((line = br.readLine()).length() != 0)) {

BufferedReader's readLine() method到达流的末尾时,它返回null,而不是空字符串。

  

返回:

     

包含行内容的字符串,不包括任何行终止字符;如果已到达流的末尾,则为null

尝试

while ((line = br.readLine() != null) {