I have a program that reads in lines from a file (with two lines) using a while loop (condition is bufferedReader.readLine()!=null
), assigns myJSONObject
a JSON read from the file, then I have an if statment (if(bufferedReader.readLine()!=null&&!bufferedReader.readline.matches(DELETE_REGEX))
and if that's true (i.e. if the line we read is not null, and we don't match a regex) then perform some function on the JSON which should append that new JSON to a file.
I have this in some try-catch blocks. It looks a little like so:
try{
openFiles;
while(buff.readLine()!=null){
try {
instatiateAndUseJSONParser;
if(bufferedReader.readLine()!=null
&&!bufferedReader.readline.matches(DELETE_REGEX))
{doSomeStuff;}
else
{continue;}
} catch (AllTheExceptions e){e.printStackTrace}
}
closeFiles;
}catch(SomeMoreExceptions e){e.printStackTrace}
When I run this is gets to the iff statement, and then terminates with exit value:0 (program closed as normal)
Why is this? It doesn't get anywhere near the 'continue' or a catch block.
If I remove the second line I get a NullPointerException due to line 50 of String Reader, but I'm not using StringReader
(I've tried importing it, but eclipse yellow-underlines it and this changes nothing). When debugging, it pops up a tab for StringReader.<init>(String) line: 50
and just says 'Source not found'.
I'm pretty new to Java, so I don't really have a clue what's happening. Any help would be appreciated in clearing this up.
Thanks!
Every time readLine()
is called, it reads a new line. You can thus read 3 lines per iteration in your current code. You should assign the result of the first call to a variable, and use this variable:
String line = null;
while ((line = buff.readLine()) !=null) {
try {
instatiateAndUseJSONParser;
if (line.matches(DELETE_REGEX)) {
doSomeStuff;
}
}
catch (AllTheExceptions e){
throw new RuntimeException(e);
}
}
You should also avoid swallowing exceptions.