簡體   English   中英

如何修復空指針異常?

[英]How do I fix Null Pointer Exception?

我正在嘗試計算字符串中的元音數量。 問題是它正確編譯,但是當我運行它時,命令窗口在CountVowels.main( CountVowels.java:25 )中顯示“線程中的異常'主' java.lang.NullPointerException”

import javax.swing.JOptionPane;

public class CountVowels
{
    public static void main(String[] args)
    {
        String thingy = JOptionPane.showInputDialog("Please enter a sentence, and I  will count the vowels.");
        int count = 0;

        char[] chars = new char[thingy.length()];
        String[] letters = new String[thingy.length()];

        for(int i = 0; i < thingy.length(); ++i)
        {
            chars[i] = thingy.charAt(i);
        }

        for(int i = 0; i < letters.length; ++i)
        {
            letters[i].valueOf(chars[i]);
        }

        for(int i = 0; i < chars.length; ++i)
        {
            if(letters[i].equals("a") || letters[i].equals("e") || letters[i].equals("i") || letters[i].equals("o") || letters[i].equals("u"))
            {
                ++count;
            }
        }
        System.out.println("There are " + count + " vowels in the string + " + thingy);
    }
}

是什么導致這種情況,我該如何修復它,以及如何防止它在將來再次發生?

第25行是我的if語句:

if(letters[i].equals("a") || letters[i].equals("e") || letters[i].equals("i") || letters[i].equals("o") || letters[i].equals("u"))
                {
                    ++count;
                }

這條線

letters[i].valueOf(chars[i]);

什么也沒做。 如果你改成它

letters[i] = String.valueOf(chars[i]);

您將所有letters元素設置為相應的字符。 這應該修復你的NullPointerException

您還可以使用一些正則表達式技巧來保存大量代碼:

import javax.swing.JOptionPane;

public class CountVowels
{
    public static void main(String[] args)
    {
        String input = JOptionPane.showInputDialog("Please enter a sentence, and I will count the vowels.");
        String[] letters = input.split("(?<=.)(?=.)");

        int count = 0;

        for(int i = 0; i < letters.length; i++)
        {
            if(letters[i].matches("[aeiou]"))
            {
                count++;
            }
        }
        System.out.println("There are " + count + " vowels in the string + " + input);
    }
}

請嘗試使用此代碼:

letters[i] = "" + chars[i];

此外,您不需要經歷創建從String到chars到String的新數組的所有麻煩。 有關從String訪問每個字符的更多信息,請閱讀此問題(這樣您就可以檢查thingy對象中的字母而無需創建更多數組)

通過索引獲取字符串字符 - Java

試試這個 :

      import javax.swing.JOptionPane;
      class CountVowels
     {
       public static void main(String[] args)
     {
      String thingy = JOptionPane.showInputDialog("Please enter a sentence, and I  will count the vowels.");
     int count = 0;
     char[] c=thingy.toCharArray();
     for(int i = 0; i < c.length; ++i)
     {
        if(c[i]=='a' || c[i]=='e' || c[i]=='i' || c[i]=='o' || c[i]=='u')
        {
            ++count;
         }
      }
    System.out.println("There are " + count + " vowels in the string + " + thingy);
   }
 }

輸出: 在此輸入圖像描述

數組letters為空。 它包含null值。

您的代碼中存在錯誤。 當您使用String進行操作時,應該使用返回字符串進行進一步處理。請參閱文檔

letters [i] = String.valueOf(chars [i]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM