我想知道是否有人能找出我收到错误的原因
Exception in thread "main" java.lang.NullPointerException
at WordSorter.verifier(WordSorter.java:76)
at WordSorter.main(WordSorter.java:12)
我做了这个程序:
import java.util.Scanner;
public class WordSorter {
static String words[];
static String sameEnd[][];
static String sameBeg[][];
static String alph[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
public static void main (String args[]){
Scanner getWordInput = new Scanner(System.in);
System.out.println("Enter word");
verifier(getWordInput.next());
System.out.println("Thank you.\nWhat would you like to do next?\n1:View list of entered words so far\n2: View list of words that start with the same letter\n3: View list of words ending with the same letter");
Scanner getFunctionInput = new Scanner(System.in);
int inputNum = getFunctionInput.nextInt();
switch (inputNum){
case 1:
viewListTot();
break;
case 2:
viewListSameBeg();
break;
case 3:
viewListSameEnd();
default:
System.out.println("Sorry, we're not sure what command you attempted to run");
break;
}
}
public static void inputSameEnd(String x){
int i=0;
while(i<26){
if(x.endsWith(alph[i])){
sameEnd[i][sameEnd.length+1]=x;
}
i++;
}
}
public static void inputSameBeg(String x){
int i=0;
while(i<26){
if(x.startsWith(alph[i])){
sameBeg[i][sameBeg.length+1]=x;
}
i++;
}
}
public static void viewListTot(){
for(int x=0;x<=words.length;x++){
System.out.println(words[x]);
}
}
public static void viewListSameBeg(){
for(int row = 0; row<=sameBeg.length; row++){
for(int column = 0; column<=sameBeg[row].length; column++){
System.out.println(sameBeg[row][column]);
}
}
}
public static void viewListSameEnd(){
for(int row = 0; row<=sameEnd.length; row++){
for(int column = 0; column<=sameEnd[row].length; column++){
System.out.println(sameEnd[row][column]);
}
}
}
public static void verifier(String w){
int i=0;
boolean WordThere = false;
while(i<=words.length){
if(words[i].equals(w)){
System.out.println(w+" is already in the dictionary");
WordThere=true;
}
if(i==words.length&&WordThere==false){
words[i].equals(w);
inputSameEnd(w);
inputSameBeg(w);
}
i++;
}
}
}
我试图将变量动态更改为所有正常而不是静态,因为我尝试多次访问单词数组而没有出现错误,但我还没有提出足够好的解决方案。我认为这可能是一个多线程问题,但后来我认为我一次不会处理多个任务。 我觉得晚安的休息对我有好处,因为我几乎从早上6点到晚上9点40分在电脑里一整天都在用C#和python编程其他东西。 我希望有一个人可以帮助我! 谢谢, - 最大