0

**the output I'm looking for is this, when given hacker I need "Hce akr", I get the output, but after that it throws NullPointerException on char arr initialization **

 public class Solution {
    public static void main(String[] args) {

            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            String arr[] = new String[10];
            int k = 0;
            while(k <= n){
                arr[k] = in.nextLine();
                k++;
            }
            for(int i = 0;i < arr.length;i++){
                String myStr = "";
                String myStr2 ="";

                char charArr[] = arr[i].toCharArray();

                for(int j = 0;j<charArr.length;j++){
                    if(j == 0 || j%2 ==0){
                        myStr += charArr[j];
                    }else if(j %2 != 0){
                        myStr2 += charArr[j];
                    }
                }
                System.out.println(myStr+" "+myStr2);
            }
        }```
  • Hello, welcome to the site. You need to learn how to use your IDE's debug mechanism. You're initializing only n positions of your array of ten positions. What happens when n is less than 10? arr.length is ten but arr[2] is null if you introduce 1 when you are executing int n = in.nextInt(). – RubioRic Jul 05 '21 at 07:43
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – RubioRic Jul 05 '21 at 07:43
  • yeah , I get it now , thanks for the help, I figured out – ks surya Jul 05 '21 at 11:12

0 Answers0