我正在尝试遍历字符串并将字符保存在字符数组中。它完美地迭代并打印字符但是当我尝试将字符保存在数组或列表中时,它会给我 NullPointerException。可以有谁指出我在这里做的错误?
try {
List<String> result = Files.readAllLines(Paths.get("C:\\Users\\Rehan\\Documents\\NetBeansProjects\\Marks\\build\\classes\\marks\\input.txt"));
List<Character> marksC = null;
for(String line : result ){
System.out.println(line);
//Reversing each line of string
StringBuilder sb = new StringBuilder(line);
sb.reverse();
String RS = sb.toString();
System.out.println(RS);
//To read first sequence of integers in reversed string
StringCharacterIterator si = new StringCharacterIterator(RS, 0);
for(int i = 0; i <= RS.length() ; i++){
while (si.current()!=' '){
char c = si.current();
marksC.add(c); //This line giving NullPointerException
System.out.println(c);
si.next();
}
break;
}
}
JOptionPane.showMessageDialog(null, result);
}