If I use the filepath directly in the class CtoJ it works. If i pass it through the constructor it gives me a Nullpointer. Well I do not know what I could add to make my problem any more clear. Well if I use String path =""; it throws no such file or directory. It seems like the constructor is not able to write the filepath into the path variable?
public class CSVReader {
public static void main(String[] args) {
CtoJ test = new CtoJ("/Users/peterg/Desktop/test.csv");
}
}
import org.json.CDL;
import org.json.JSONArray;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CtoJ {
private String path;
private StringBuilder content = new StringBuilder();
private String line;
private String stringtoJSON;
public CtoJ(String path) {
this.path = path;
}
{
try {
BufferedReader reader = new BufferedReader(new FileReader(path));
while ((line = reader.readLine()) != null) {
content.append(removeUnnecessaryQuotes(line));
content.append(System.lineSeparator());
}
stringtoJSON = content.toString();
JSONArray jsonArray = CDL.toJSONArray(stringtoJSON);
System.out.println(jsonArray);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String removeUnnecessaryQuotes(String s) {
String withoutQuotes;
withoutQuotes = s.substring(0).replaceAll("\"", "");
withoutQuotes.substring(0).replaceAll("\"\"", "\"");
return withoutQuotes;
}
}
Here is some of the data I use, if u want to test it:
FID,OBJECTID,SHAPE,LAGE,GRILLPLATZ_ID,RESERVIERUNG,WEBLINK1,SE_ANNO_CAD_DAT
"GRILLPLATZOGD.6748,6748,POINT (16.465255884594104 48.19018769574157),""22., Donauinsel, ca. 350 Meter stromab der Steinspornbrücke (Inselmitte, Erdwall)"",15,ja,http://www.wien.gv.at/amtshelfer/umwelt/wasserbau/donauinsel/grillplatzreservierung.html,"
"GRILLPLATZOGD.6749,6749,POINT (16.48177464603615 48.183356069714286),""22., Neue Donau, linkes Ufer, zwischen Steinspornbrücke und Waluliso Brücke (bei km 5,1) (Dammbereich) "",16,ja,http://www.wien.gv.at/amtshelfer/umwelt/wasserbau/donauinsel/grillplatzreservierung.html,"
"GRILLPLATZOGD.6750,6750,POINT (16.460158556964053 48.177745677669925),""11., Donaukanal, Alberner Hafenzufahrtsstraße, Nähe Margetinstraße"",0,nein,http://www.wien.gv.at/umwelt/wald/freizeit/grillen/,"
CodePudding user response:
you get the error because you accidentally used an instance initializer block in your Code. That is
class Foo {
//constructor
public Foo() {
System.out.println("Constructor");
}
//instance initializer Block
{
System.out.println("Instance Initializer");
}
}
Generally, this is okay, however, the initializer block is executed before the constructor is.
In your particular example
BufferedReader reader = new BufferedReader(new FileReader(path));
in the initializer block is dependent on the instantiation of path
in the constructor. So as a quick fix just move the code from the initializer to the constructor and you shouldn't get any NullPointerException
.