-3

the program is to count the frequency of each word in a sentence. This is the source code:

import java.io.*;
import java.util.Scanner;
class Dictionary
{
  String key;
  int val;
  public Dictionary()
  {
    key="";
    val=0;
  }
 void insert(String k)
  {
    key=k;
    val=1; 
    System.out.println("\nInserted key with value: "+k);
  }
 void update()
  {
    val++;
  }
 String retkey()
 {
   return key;
 }
 int retval()
 {
   return val; 
 }
 public void display()
  {
    System.out.println("Word = "+key);
    System.out.println("Number = "+val);
  }
}
class stack
{
  Dictionary D[]=new Dictionary[50];
  int n;
  int top;
  public stack(int N)
  {
    n=N;
    top=-1;
  // D=new Dictionary[n];
  }
  public void push(String p)
  {
    top++;
    System.out.println("pushing "+p+"\n top=="+top);
    D[top].insert(p);
    System.out.println("\ntop="+top);
  }
  public void check(String p)
  {
    int i=0,flag=1;
    String q="";
    if(top==-1)
    {
      System.out.println("top=-1");
      push(p);
      flag=0;
    }
    else
    {
     for(i=0;i<=top;i++)
     {
       q=D[i].retkey();
       if(q.equals(p))
         { 
          D[i].update();
          flag=0;
         }
        else
        flag=1;
     }
    if(flag==1)
     push(p);
    }
   }
 public void print()
 {
   D[top].display();
 }
}
public class Wrdcnt
{
  public static void main(String[] args)throws Exception
  {
  //  Dictionary D[]=new Dictionary() ;
    String st="";
    String p="";
    System.out.println("Enter String: ");
    Scanner in= new Scanner(System.in);
    st=in.nextLine();
/*    no of words*/
    int n=count(st);
    int j=0;
    System.out.println("No of words: "+n);
    stack stck =new stack(n);
    for(int i=0;i<=st.length();i++)
     {
      if(i==st.length())
       {
        p = st.substring(j,i);
         System.out.println(""+p);
         stck.check(p);
         System.out.println("Checking done for word: "+p);                           
         j=0;
        break;
       }       

       if(st.charAt(i)==' ')
        {
         p = st.substring(j,i);
         System.out.println(""+p);
         stck.check(p);
         j=i+1;
         System.out.println("Checking done for word: "+p);          
        }
     }
  }
  public static int count(String s)
  {
    int w=0;
    for(int i=0;i<s.length();i++)
     {
           if(s.charAt(i)==' ')
               {
                   w++;
               }
     }
   return (w+1);
  }
}

output:

Enter String:
Roses are red
No of words: 3
Roses
top=-1
pushing Roses
top==0
Exception in thread "main" java.lang.NullPointerException
at stack.push(Wrdcnt.java:51)
at stack.check(Wrdcnt.java:61)
at Wrdcnt.main(Wrdcnt.java:117)

Phoebe
  • 1
  • Voting to reopen, because the linked dupe is explaining about NPE in the general case, and this is about a specific case. – amit May 28 '15 at 07:41
  • 3
    *Most* questions about NPEs are specific cases, but boil down to the same reason. The OP has asked what it means, the dupe question explains it. – Andy Turner May 28 '15 at 07:47

1 Answers1

0

In this line:

D[top].insert(p);

You access the variable D[top], which happens to be null.
When trying to invoke insert(p) on null, you get a NullPointerException.

This happens because you only initialize the array, but don't initialize the objects themselves to populate the array:

Dictionary D[]=new Dictionary[50];

Is allocating place for the Dictionary[] array, but NOT allocating the Dictionary objects themselves.

amit
  • 175,853
  • 27
  • 231
  • 333