首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java代码- ArrayIndexOutofBoundsException

Java代码- ArrayIndexOutofBoundsException
EN

Stack Overflow用户
提问于 2014-06-03 08:34:20
回答 2查看 37关注 0票数 0

我已经写了下面的代码,并且一直遇到这个错误:

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

代码语言:javascript
复制
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class Yournamep3test { 


    public static void main(String[] args) {

        // Check if target file exists
        File targetFile = new File(args[0]);


        try {

            PrintWriter out = new PrintWriter(targetFile);
            out.write("\r\nStringed musical Instrument program");

            for (int arrayIndex = 0; arrayIndex < 10; arrayIndex++) {

                out.write("\r\n\r\n");              
                out.write("\r\nCreating new Stringed Musical Instrument object now..............");
                Yournamep3 violinInstrument = new Yournamep3();

                violinInstrument.setNameOfInstrument("Violin # " + (arrayIndex+1));

                out.write("\r\nCreated instrument with name - "
                        + violinInstrument.getNameOfInstrument());

                int num = violinInstrument.getNumberOfStrings();
                out.write("\r\nNumber of strings in instrument is " + num);

                out.write("\r\nNames of String are ");
                String strings[] = violinInstrument.getStringNames();
                for (int counter = 0; counter < num; counter++) {
                    out.write("\r\n" + strings[counter]);
                }

                out.write("\r\nIs the Instrument playing - "
                        + violinInstrument.isPlaying());
                out.write("\r\nIs the Instrument tuned - "
                        + violinInstrument.isTuned());

                out.write("\r\nTuning now.........");

                violinInstrument.setTuned(true);
                out.write("\r\nIs the Instrument tuned - "
                        + violinInstrument.isTuned());

                out.write("\r\nCalling the Instrument play  method now..");
                violinInstrument.startPlayInstrument();
                out.write("\r\nIs the Instrument playing - "
                        + violinInstrument.isPlaying());



                out.write("\r\nStopping playing of instrument..............");
                violinInstrument.stopPlayInstrument();
                out.write("\r\nIs the Instrument playing - "
                        + violinInstrument.isPlaying());


            }
            out.close();
        } catch (IOException e) {
        }
    }
}

我认为问题出在第23行。任何建议都将不胜感激,谢谢。

这是代码yournamep3的另一部分

公共类Yournamep3 {

代码语言:javascript
复制
//fields to determine if the instrument is isTuned,
private boolean isTuned;

//如果仪器当前为isPlaying。私有布尔isPlaying;

私有字符串名称;

private int numberOfStrings = 4;//字符串数私有字符串nameofStringsInInstrument[] = {"E","C","D","A"};//字符串名的数组

代码语言:javascript
复制
//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;
}  

}

EN

回答 2

Stack Overflow用户

发布于 2014-06-03 08:41:16

我会查看violinInstrument的getStringNames()方法。在我看来,它没有正确地填充字符串数组,或者getNumberOfStrings()方法没有给出正确的字符串数。如果你把代码放上去,我可以帮你更多。

票数 0
EN

Stack Overflow用户

发布于 2014-06-03 08:45:32

第23行似乎是

代码语言:javascript
复制
Yournamep3 violinInstrument = new Yournamep3();

如果是这样的话,您应该检查Yournamemp3的构造函数

由于第23行是

代码语言:javascript
复制
File targetFile = new File(args [0]); 

它表明你的args对象是空的。抛出ArrayIndexOutOfBoundException以指示已使用非法索引访问了数组。0是非法索引。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24005407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档