public class CountOccurenceOfWords {
public static void main(String[] args){
// String text = "Good morning.Have a good class." +
// "Have a good vist. Hava fun!";
CountWords a = new CountWords("Good morning.Have a good class." +
"Have a good vist. Hava fun!");
a.printResult();
}
}
class CountWords{
private String text;
public CountWords(String text){
this.text = text;
}
Map<String, Integer> map = new TreeMap<String, Integer>();
//我debug了一下是这里没有给words赋上值,但是不知道为什么
String[] words = text.split("[ \n\t\r.,;:!?()]");
public void printResult(){
for(int i=0; i<words.length; i++){
String key = words[i].toLowerCase();
if(key.length() > 0){
if(map.get(key) == null){
map.put(key, 1);
}else{
int value = map.get(key).intValue(); //这里要加上intValue();
value++;
map.put(key, value);
}
}
}
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for(Map.Entry<String, Integer> entry : entrySet){
System.out.println(entry.getKey() + entry.getValue());
}
}
}
Exception in thread "main" java.lang.NullPointerException
at Unit_22.CountWords.<init>(CountOccurenceOfWords.java:26)
at Unit_22.CountOccurenceOfWords.main(CountOccurenceOfWords.java:11)
public void printResult(){
for(int i=0; i<words.length; i++){
String key = words[i].toLowerCase();
if(key.length() > 0){
if(map.get(key) == null){
map.put(key, 1);
}else{
int value = map.get(key).intValue(); //这里要加上intValue();
value++;
map.put(key, value);
}
}
}
java 运行出现的 空指针异常 问题求大神帮忙看一下
答案:3 悬赏:50 手机版
解决时间 2021-01-22 21:35
- 提问者网友:玫瑰园
- 2021-01-22 07:59
最佳答案
- 五星知识达人网友:煞尾
- 2021-01-22 09:33
把错误代码贴全点,你可以自己设断点debug啊,一步步看是谁是空指针就可以了
全部回答
- 1楼网友:你哪知我潦倒为你
- 2021-01-22 10:36
你的split用了几种分割标准啊?
- 2楼网友:十年萤火照君眠
- 2021-01-22 10:13
class CountWords {
private String text;
private String[] words;
public CountWords(String text) {
this.text = text;
words = this.text.split("[ \n\t\r.,;:!?()]");
}
Map<String, Integer> map = new TreeMap<String, Integer>();
public void printResult() {
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯