-1

I know there are many similar questions here, but I still can't solve it. I can get all the results that I want. However, in the end, it still shows nullpointerexception. I don't know why. can anyone help?

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

}

the txt file format is like

{ "type": "Feature", "properties": { "id": 126.000000, "osm_id": 4851918786.000000, "name": "Moray House Library", "type": "library" }, "geometry": { "type": "Point", "coordinates": [ -3.180841771200988, 55.950622362732418 ] } },

this is one line. I have many lines, and actually this is just a test code. if it works. i want to write it as a method in a javaseverlet class. get the string coordinates and return it to my JS font-end.

Zan Zheng
  • 23
  • 1
  • 4
  • That looks very JSON-esque. Have you tried just using a JSON library to handle it? https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – Chris Jun 22 '17 at 00:40
  • Thanks, your link is very helpful. Dude – Zan Zheng Jun 25 '17 at 14:52

1 Answers1

1

There's a few issues with your code. In this section:

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
    }

When you checked if the string was empty, you did line == " " which is wrong for 2 reasons. First you cannot use == to compare strings - read this question for details on why not. Second, " " is a string that contains a space character. "" is an empty string.

When you want to check if a string is empty you can do it like this:

line.equals("")

or like this:

line.isEmpty()

Here's your code with a few small changes so that it runs without throwing an exception.

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();            
        }
    }
}
Matt
  • 3,677
  • 1
  • 14
  • 24
  • Thanks, dude, I get it. Just one more question. After while loop, my original code has line = br.readLine(); // here you read the second line (there is no second line, now line is null). You said its null now, but my code still can run and get the correct answer. So, if I have this line, and if it's null, how come i still can run the rest code. I dont understand here. besides, when i run it. if i have this line, the eclipse console directly show the result. if i delete this line, i still can get result, while the result just line by line keep showing on console. – Zan Zheng Jun 22 '17 at 23:10
  • It depends on your input file. Do you have any extra blank lines in your file? When I tested your code my input file had a single line (your JSON string). If you use the debugger you can step through the code and inspect the variables, give that a go. – Matt Jun 22 '17 at 23:38