0

My program is to add word characters in a node, then pop out in reverse. It complies, but gives error at run-

Exception in thread "main" java.lang.NullPointerException at ReverseLinkedList$Node.access$002(ReverseLinkedList.java:20) at ReverseLinkedList.push(ReverseLinkedList.java:44) at reverseWordTester.main(reverseWordTester.java:55)

Please help.

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

public class ReverseLinkedList
{
   int counter = 0;
   private Node first = null;  
   private Node node = first;

   private class Node
   {
      private String item; // data in Node
      private Node next; // points to next Node in list.
   }

   public void push(String w1)
   {
      String word = w1;

      for (int i=0; i<word.length(); i++)
      {
         char c = word.charAt(i);
         String str = Character.toString(c);
         node.item = str;

         node.next = first;
         first = node;      

      } //end for

   } //end push method

   public String pop()
   {  
      String wordReverse = "";

      if (first ==null)
      {
         throw new EmptyCollectionException("Empty");
      }      

      while(first != null)
      {
         Node tempNode = first; //  first node is saved in tempNode

         wordReverse += tempNode;
         first = first.next;
         counter--;
      }

      return wordReverse;      


   } //end pop  


}    


My Main program :
import java.util.*;
import java.io.*;

public class reverseWordTester extends RuntimeException
{
   public static void main(String[] args)    
   {
      String inputSentence;
      String nextWord, getSentence;
      boolean more = true;


      // Creates scanner object
      Scanner in = new Scanner(System.in);

      while (more == true)
      {
         // Prompts the user to type in a sentence
         System.out.println("Please type a sentence" );
         inputSentence = in.nextLine();

         if (inputSentence.equals("")|| inputSentence.equals(" "))
         {
            System.out.println("No sentence typed!");

         }
         else
         {

            // Displays the sentence typed by the user
            Scanner in1 = new Scanner(inputSentence);
            System.out.println("You typed the sentence: "+inputSentence);
            System.out.println("\nReversed word Sentence : ");


            while (in1.hasNext())
            {
               // Scans each word of the sentence
               nextWord = in1.next();

               // 
               ReverseLinkedList r1 = new ReverseLinkedList();

               // Sends the word to the constructor to reverse it

               r1.push(nextWord);

               // Pops the word in reverse order
               getSentence = r1.pop();

               // Displays the words of the sentence in reverse order
               System.out.print(getSentence);
               System.out.print(" ");


              }//end while
         }//end else

         // Checks if user wants to rerun the program
         Scanner ans = new Scanner(System.in);
         System.out.println("\n\nDo you want to try reversing word in sentence again? (y/n): ");
         String reply = ans.next();

         if (reply.equalsIgnoreCase("n"))
         {
            System.out.println("Good Bye");
            more = false;
         }

      } //end while (more)
   }
}
Johnny Mopp
  • 10,608
  • 4
  • 33
  • 58
Molly
  • 19
  • 1
  • 2
  • Well it is not just a problem of null pointer there is more to this question than null pointer error. There is logic and coding errors. in the push and pop methods and the main. –  Oct 07 '15 at 18:43

1 Answers1

0

The first time you try to push and object first is null so you get a null pointer exception.

You have to add code to handle the first element being pushed similar to what you do for pop() when there are no elements left

dkatzel
  • 29,286
  • 2
  • 57
  • 64