这段程序在for (int j = index; j < map.get(ch[i]) + 1; j++)会报空指针异常,为什么
import java.util.*;
class Solution {
public static int lengthOfLongestSubstring(String s) {
char[] ch = s.toCharArray();
Map<Character, Integer> map = new HashMap<>();
int index = 0;// 子串开始的角标
int res = 0;
for (int i = 0; i < ch.length; i++){
if (map.containsKey(ch[i])){
res = Math.max(res, i - index);
// System.out.println(i);
// System.out.println(index);
// System.out.println(res);
System.out.println(map.get(ch[i]));
for (int j = index; j < map.get(ch[i]) + 1; j++){
map.remove(ch[j]);
}
index = map.get(ch[i]) + 1;
} else {
map.put(ch[i], i);
}
}
return res;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println(lengthOfLongestSubstring("avvab"));
}
}