Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
尝试访问stone [0] .length时获取NullPointerException。
请帮忙。我已经初始化了Stones Object。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Solution
    {
public static void main(String args[]) throws IOException
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    Scanner sc = new Scanner(System.in);
    int times = sc.nextInt();

    String stones[] = new String[times];
    int score = 0;
        int counter;

    for(int tcase = 0; tcase < times; tcase++)
        stones[tcase] = br.readLine();

      int s = stones[0].length();
        for (int i = 0; i < s ; i++) {
          char j = stones[0].charAt(i);
            counter = 0;
            for (int k = 1; k < times; k++) {
                char aa[] = stones[k].toCharArray();
                for (int l = 0; l <aa.length ; l++) {
                    if(aa[l]==j)
                    {
                        counter++;
                        break;

                    }
                }
                if (counter==times-1) {
                    score++;
                }
            }
        }

    System.out.println(score);
    }
    }


当我尝试访问stone [0] .length()时得到NullPointerException。请帮忙

最佳答案

当您通过某种自动化服务提交代码时,它正在运行您的代码,但由于没有System.in流提供任何有效数据而失败了。如果您尝试在执行任何操作之前先检查有效数据,它会遇到这种情况,应让您提交,同时仍可在笔记本电脑上正常工作。

尝试这个:

Scanner sc = new Scanner(System.in);
int times = 0;

if ( sc.hasNext() ) {   // check to make sure there's valid input first
    times = sc.nextInt();


    String stones[] = new String[times];
    int score = 0;
        int counter;

    for(int tcase = 0; tcase < times; tcase++)
        stones[tcase] = br.readLine();

    if ( stones[0] != null ) {  // check to make sure your array object exists
        int s = stones[0].length();
        for (int i = 0; i < s ; i++) {
            char j = stones[0].charAt(i);
            counter = 0;
            for (int k = 1; k < times; k++) {
                char aa[] = stones[k].toCharArray();
                for (int l = 0; l <aa.length ; l++) {
                    if(aa[l]==j)
                    {
                        counter++;
                        break;
                    }
                }
                if (counter==times-1) {
                    score++;
                }
            }
        }
    }
}

关于java - Java中的null指针异常。 ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24845071/

10-12 15:21