我有這個測試程序,我看到一個NullPointer異常。我需要幫助找出如何解決它,並想知道根本原因。在給定的代碼異常(java)
public class test {
private static void practice(String[] words) {
int vowelPosition = 0;
int consonantPosition = 0;
char[] vowel = (char[]) null;
char[] consonant = (char[]) null;
for (int i = 0; i < words.length; i++) {
int currentWordLength = words[i].length();
for (int j = 0; j < currentWordLength; j++) {
if (words[i].charAt(j) == 'a' || words[i].charAt(j) == 'e'
|| words[i].charAt(j) == 'i'
|| words[i].charAt(j) == 'o'
|| words[i].charAt(j) == 'u') {
consonant[j] = 'n';
vowel[j] = words[i].charAt(j);
vowelPosition = j;
System.out.println(j + "At this position is "
+ vowel[vowelPosition]);
} else {
vowel[j] = 'n';
consonant[j] = words[i].charAt(j);
consonantPosition = j;
System.out.println(j + " At this position is "
+ consonant[consonantPosition]);
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] words = { "harpreet" };
practice(words);
}
}
我也嘗試調試它,發現內循環拋出異常。
你能告訴我們一個確切的堆棧跟蹤嗎? – GreySwordz
爲了將來的參考,請隨時提供**堆棧跟蹤**,以便您詢問有關錯誤/異常情況。 –
討厭這個downvoting .. !! – Gurjit