Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Nullpointerexception说,通过split()处理txt文件_Java - Fatal编程技术网

Java Nullpointerexception说,通过split()处理txt文件

Java Nullpointerexception说,通过split()处理txt文件,java,Java,我知道这里有很多类似的问题,但我仍然无法解决。我可以得到我想要的所有结果。但是,最后,它仍然显示nullpointerexception。我不知道为什么。有人能帮忙吗 public class PointGenterate { public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub try{ File file = n

我知道这里有很多类似的问题,但我仍然无法解决。我可以得到我想要的所有结果。但是,最后,它仍然显示
nullpointerexception
。我不知道为什么。有人能帮忙吗

public class PointGenterate {

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    try{
    File file = new File("123.txt");
    double[] pointsid = new double[10];
    String[] data = null;

    for(int i = 0; i <10; i++){
        double rn = (int)(Math.random()*120);
        System.out.println(rn);
        pointsid[i] = rn;
    }
    //read file
    InputStreamReader rs = new InputStreamReader(new FileInputStream(file));//create input stream reader object
    BufferedReader br = new BufferedReader(rs);
    String line = "";
    line = br.readLine();
    //
    File write = new File("output.KML");
    write.createNewFile();
    BufferedWriter out = new BufferedWriter(new FileWriter(write));
    while(line != null){
        line = br.readLine();
        if(line==" "){
            System.out.print("empty");
        }else{
        data = line.split(",|:|[|]");
        }
        for(int i = 0; i < data.length; i++){
            data[i] = data[i].trim();
            System.out.println(data[i] + "num" + i);
        }
        if(data.length > 15){
            double id = Double.parseDouble(data[4]);
            for(int i = 0; i <10; i++){
                if(id == pointsid[i]){
                    data[10] = data[10].substring(0, data[10].length()-2);
                    data[15] = data[15].substring(1,data[15].length());
                    data[16] = data[16].substring(0, data[16].length()-6);
                    out.write(data[8]+" "+ data[10]+ " " + data[13] + data[15] + data[16]+ "\r\n");
                    out.flush();
                }
            }
        }



        //System.out.println(line);
    }



    out.close();
    }
    catch(Exception e){
        e.printStackTrace();            
    }
}

}

这是一行。我有很多行,实际上这只是一个测试代码。如果有效的话。我想把它写成javaseverlet类中的一个方法。获取字符串坐标并将其返回到我的JS字体末尾

您的代码有一些问题。在本节中:

InputStreamReader rs = new InputStreamReader(new FileInputStream(file));//create input stream reader object
BufferedReader br = new BufferedReader(rs);
String line = "";
line = br.readLine(); // here you read the first line in the file
//
File write = new File("output.KML");
write.createNewFile();
BufferedWriter out = new BufferedWriter(new FileWriter(write));
while(line != null){ // here you check that it's not null (it's not, you read the first line OK)
    line = br.readLine(); // here you read the second line (there is no second line, now line is null)
    if(line==" "){ // now you check if the line is a space character (this is wrong for 2 reasons, that's not how you compare strings, and a space character is not an empty string)
        System.out.print("empty");
    }else{
    data = line.split(",|:|[|]"); // here you call split() on line but line is null
    }
当您检查字符串是否为空时,您执行了
line==“”
,这是错误的,原因有二。首先,您不能使用
==
来比较字符串-请阅读以了解有关为什么不进行比较的详细信息。其次,
是一个包含空格字符的字符串<代码>“是一个空字符串

当您想检查字符串是否为空时,可以这样做:

line.equals("")
line.isEmpty()
或者像这样:

line.equals("")
line.isEmpty()
下面是您的代码,其中有一些小的更改,以便它在运行时不会引发异常

public class PointGenterate {

    public static void main(String[] args) throws Exception {
        try {
            File file = new File("123.txt");
            double[] pointsid = new double[10];
            String[] data = null;

            for(int i = 0; i < 10; i++){
                double rn = (int)(Math.random()*120);
                System.out.println(rn);
                pointsid[i] = rn;
            }

            //read file
            InputStreamReader rs = new InputStreamReader(new FileInputStream(file));//create input stream reader object
            BufferedReader br = new BufferedReader(rs);
            String line = "";

            //
            File write = new File("output.KML");
            write.createNewFile();
            BufferedWriter out = new BufferedWriter(new FileWriter(write));
            while((line = br.readLine()) != null){ // read the line and check for null
                if(line.isEmpty()) { // is the line equal to the empty string?
                    System.out.print("empty");
                } else {
                    data = line.split(",|:|[|]");
                }

                for(int i = 0; i < data.length; i++){
                    data[i] = data[i].trim();
                    System.out.println(data[i] + "num" + i);
                }

                if(data.length > 15){
                    double id = Double.parseDouble(data[4]);
                    for(int i = 0; i <10; i++){
                        if(id == pointsid[i]){
                            data[10] = data[10].substring(0, data[10].length()-2);
                            data[15] = data[15].substring(1,data[15].length());
                            data[16] = data[16].substring(0, data[16].length()-6);
                            out.write(data[8]+" "+ data[10]+ " " + data[13] + data[15] + data[16]+ "\r\n");
                            out.flush();
                        }
                    }
                }
                //System.out.println(line);
            }
            out.close();
        }
        catch(Exception e){
            e.printStackTrace();            
        }
    }
}
公共类指针{
公共静态void main(字符串[]args)引发异常{
试一试{
File File=新文件(“123.txt”);
double[]pointsid=新的双精度[10];
字符串[]数据=null;
对于(int i=0;i<10;i++){
double rn=(int)(Math.random()*120);
系统输出打印项次(rn);
点SID[i]=rn;
}
//读取文件
InputStreamReader rs=新的InputStreamReader(新文件InputStream(文件));//创建输入流读取器对象
BufferedReader br=新的BufferedReader(rs);
字符串行=”;
//
文件写入=新文件(“output.KML”);
write.createNewFile();
BufferedWriter out=新的BufferedWriter(新的文件写入程序(写入));
while((line=br.readLine())!=null){//读取该行并检查null
如果(line.isEmpty()){//该行是否等于空字符串?
系统输出打印(“空”);
}否则{
data=line.split(“,|:|[|]”;
}
对于(int i=0;i15){
double id=double.parseDouble(数据[4]);

对于(int i=0;i),它看起来非常类似JSON。您是否尝试过使用JSON库来处理它?谢谢,您的链接非常有用。都德谢谢,都德,我明白了。再问一个问题。在while循环之后,我的原始代码有line=br.readLine();//这里您阅读了第二行(没有第二行,现在行为空)你说它现在是空的,但是我的代码仍然可以运行并得到正确的答案。所以,如果我有这一行,如果它是空的,为什么我仍然可以运行其余的代码。我在这里不明白。此外,当我运行它时。如果我有这一行,eclipse控制台直接显示结果。如果我删除这一行,我仍然可以得到结果,而结果只是e逐行显示在控制台上。这取决于您的输入文件。您的文件中有多余的空行吗?当我测试您的代码时,我的输入文件只有一行(您的JSON字符串)。如果使用调试器,您可以逐步检查代码并检查变量,请尝试一下。