0

I was trying to solve This problem :

And my solution was this:

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc= new Scanner(System.in);
          int n=sc.nextInt();
        int size[]=new int[n];
        
        String str[]= new String[n];
          
          for(int i=0;i<n;i++)
          {
            //  System.out.println("INSIDE LOOP");
              size[i]=sc.nextInt();
             // System.out.println(size[i]);
            
                 str[i]=sc.next();  
          }

          for(int i=0;i<n;i++)
          {
              Map<Character,Integer> map = new HashMap<Character,Integer>();
              int count=0;
              int k=0;
              for(int j = 0;j<size[i];j++)
              {
                  if(map.containsKey(str[i].charAt(j)))
                  {
                      map.put(str[i].charAt(j),map.get(str[i].charAt(j)+1));
                  }
                  else
                  {
                      map.put(str[i].charAt(j),1);
                  }
              }
              for (Map.Entry<Character,Integer> var : map.entrySet())  
              {
                  if(var.getValue()==1) //**NULLPOINTER EXCEPTION IS OCCURING HERE**
                  {
                     k=1;
                     continue;
                  }
                  if(var.getValue()%2==0)
                  {
                      count=count+var.getValue();
                  }
                  
              }
              
                /*
                 * for (Map.Entry<Character, Integer> var : map.entrySet()) {
                 * if(var.getValue()==1) { count++; continue; } if(var.getValue()%2==0) {
                 * count++; } }
                 */
              System.out.println(count);
              
          }
          
        
          
          
          
         
        
         
          
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    }
}

And no matter which method I employ to traverse its giving me the same nullpointer exception.

THIS IS THE ERROR: java.lang.NullPointerException at Solution.main(Solution.java:41)

Now, I dont understand why it would give nullpointer exception when I'm just traversing. Is the map empty? How would it be if I added key, value pairs before? How to solve this nullpointer problem?

bha_1999
  • 39
  • 1
  • 6
  • The link will tell you the general steps on how to debug NullPointerExceptions (NPE), and in the future, best to search on the exception type before asking. Had you done this, you'd not have left out of your question the key information needed to debug it: the stacktrace and the line that's throwing the exception – Hovercraft Full Of Eels Sep 12 '20 at 15:24
  • @HovercraftFullOfEels I did that problem in eclipse (local environment actually) so I could use shortcuts and all and Im updating the Question with the exception that I got now. Here you say that the link will tell me steps to debug NPE. If you mean hackerrank link then no, I dont see anything explaining how to debug NPE's in that link. So I used try and catch blocks in my code and its showing the same error that eclipse showed. – bha_1999 Sep 12 '20 at 16:00
  • @HovercraftFullOfEels Also, I already told using comments (in code) where the exception is occurring so I thought that would be sufficient :) – bha_1999 Sep 12 '20 at 16:05
  • While the entry set itself is not null, your map has null values for some of the keys. Use your debugger to see which ones this occurs with and why. – Hovercraft Full Of Eels Sep 12 '20 at 16:14
  • This `map.get(str[i].charAt(j) + 1)` doesn't sit right with me – Hovercraft Full Of Eels Sep 12 '20 at 16:26
  • 1
    Feeling pretty dumb right now because I looked at that now and figured it out. You're right, thanks. That was what was causing NPE. – bha_1999 Sep 12 '20 at 18:20

0 Answers0