Skip to content

Commit

Permalink
Fixing null pointer exception on the nextLine()
Browse files Browse the repository at this point in the history
NullPointerException caused by attempting to invoke the method hasMoreTokens() on a StringTokenizer object (st) that is null. This happens in the nextLine() method of your FastReader class.

# Fix
Ensuring that st is properly initialized before calling hasMoreTokens() in the nextLine() method by modifying the nextLine() method to initialize st if it is null:
  • Loading branch information
edd-ie authored Nov 7, 2024
1 parent 0d15cfa commit e2a5eeb
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/pages/sheet/competitive-programming-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,19 @@ public class Main {
return st.nextToken();
}

String nextLine() { // read a line in the input
String str = "";
try {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
String nextLine() { // read a line in the input
String str = "";
try {
if (st == null || !st.hasMoreTokens()) {
str = br.readLine();
st = new StringTokenizer(str);
}
str = st.nextToken("\n");
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}

public static void main(String[] args) throws Exception {
Expand Down

0 comments on commit e2a5eeb

Please sign in to comment.