获取空指针执行错误,但遵守

时间:2015-10-07 18:02:43

标签: java stack reverse

我的程序是在节点中添加单词字符,然后反向弹出。它符合,但在run-

时出错
  

线程中的异常" main" java.lang.NullPointerException at   ReverseLinkedList $ Node.access $ 002(ReverseLinkedList.java:20)at   ReverseLinkedList.push(ReverseLinkedList.java:44)at   reverseWordTester.main(reverseWordTester.java:55)

请帮忙。

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)
   }
}

1 个答案:

答案 0 :(得分:0)

第一次尝试推送和对象firstnull时,您会得到一个空指针异常。

您必须添加代码来处理被推送的第一个元素,类似于您在没有元素时为pop()执行的操作