Math Java代码-ArrayIndexOutofBoundsException

Math Java代码-ArrayIndexOutofBoundsException,math,Math,我编写了以下代码,并不断遇到此错误: 线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常:0 在yournamep3.Yournamep3test.main(Yournamep3test.java:23) //如果仪器当前正在显示。 私有布尔显示 私有字符串名称 私有int numberOfStrings=4;//字符串数 StringsInstrument[]的私有字符串名称={“E”、“C”、“D”、“A”}//字符串名数组 //A c

我编写了以下代码,并不断遇到此错误:

线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常:0 在yournamep3.Yournamep3test.main(Yournamep3test.java:23)

//如果仪器当前正在显示。 私有布尔显示

私有字符串名称

私有int numberOfStrings=4;//字符串数 StringsInstrument[]的私有字符串名称={“E”、“C”、“D”、“A”}//字符串名数组

//A constructor method that set the isTuned and currently isPlaying fields to false.
public Yournamep3() {
    this.isTuned = false;
    this.isPlaying = false;
}


/**
 * @return the name
 */
public String getNameOfInstrument() {
    return name;
}

/**
 * @param name the name to set
 */
public void setNameOfInstrument(String nameOfInstrument) {
    this.name = nameOfInstrument;
}


// Other methods

public boolean isPlaying() {
    return isPlaying;
}

public void setPlaying(boolean playing) {
    this.isPlaying = playing;
}

public boolean isTuned() {
    return isTuned;
}

public void setTuned(boolean isTuned) {
    this.isTuned = isTuned;
}

public void startPlayInstrument() {
    System.out.println("The Instrument is now Playing.");
    isPlaying = true;
}

public void stopPlayInstrument() {
    System.out.println("The Instrument is not Playing anymore.");
    isPlaying = false;
}

public void startTuneInstrument() {
    System.out.println("The Instrument is Tuned.");
    isTuned = true;
}

public void stopTuneInstrument() {
    System.out.println("The Instrument is not Tuned.");
    isTuned = false;
}



public int getNumberOfStrings() {
   return this.numberOfStrings ;
}

public String[] getStringNames() {
    return nameofStringsInInstrument;
}  

}

我想看看小提琴乐器的getStringNames()方法。在我看来,它没有正确填充字符串数组,或者getNumberOfStrings()方法没有给出正确的字符串数。如果你把代码放上去,我可以帮你多一点。

第23行似乎是

Yournamep3 violinInstrument = new Yournamep3();
如果是这种情况,你应该检查构造器中你的名字

因为第23行是

File targetFile = new File(args [0]); 

它表示args对象为空。引发ArrayIndexOutOfBoundException以指示已使用非法索引访问数组。0是非法索引。

消息中说了什么?您试图访问元素零(它将是数组中的第一个元素),但数组中根本没有元素。(当然,我们不知道第23行在哪里,因为您显然删除了一些行。)我猜错误在这一行:
File targetFile=new File(args[0]),或在您删除的某一行上。您有许多out.write(…)语句。您应该能够根据最后一个输出行将其缩小到代码的某一行。@womanamongmen-无需“缩小”-异常会告诉您哪一行出错。
args
是一个数组,它包含的元素数量与
java Yournamep3test
之后的单独单词数量相同。如果在那之后没有单词,那么就没有元素,你会得到异常。如果错误在构造函数中,那么它将位于堆栈的顶部。嗯……可能是这样。如果OP提供了她/他正在使用的编辑器上的第23行,这会有所帮助。第23行:File targetFile=new File(args[0])@用户3697829-正如我所料。福尔摩斯的格言——当你消除了不可能,剩下的无论多么不可能,都一定是真相。谢谢你的想法。我已将代码的其余部分添加到原始帖子中。
Yournamep3 violinInstrument = new Yournamep3();
File targetFile = new File(args [0]);