1

我正在尝试创建一种简单的 GUI,其中我试图保存几个字符串和双精度数和一个整数,我使用 OOP 的基本属性,即继承我创建了一个类 Autos,它本质上是超类。

问题似乎出现在我的 GUI 类中名为“cargarDatosAutos”的方法中,代码如下:

private void cargarDatosAutos()
{
    regInt = at.numRegistros(); // number of registry
    if (regInt != -1)
    {
        curInt = 0;
        ats = new AutosRentables[regInt];
        try
        {
            RandomAccessFile f = new RandomAccessFile("Autos.txt", "rw");
            at.cargarDatos(f, ats, regInt); // method in subclass
            f.close();
        }
        catch (IOException ex)
        {
            Logger.getLogger(Interfaz3.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.mostrarAutos(ats[0]); // shows data
    }
}

以下是错误:

4-Dec-2011 11:35:20 PM rent_autos.Interfaz3 cargarDatosAutos
SEVERE: null
java.io.EOFException
at java.io.RandomAccessFile.readChar(RandomAccessFile.java:695)
at rent_autos.Autos.leerModelo(Autos.java:139)
at rent_autos.AutosRentables.cargarDatos(AutosRentables.java:84)
at rent_autos.Interfaz3.cargarDatosAutos(Interfaz3.java:6076)
at rent_autos.Interfaz3.<init>(Interfaz3.java:38)
at rent_autos.Interfaz3$159.run(Interfaz3.java:6107)

leerModelo 是一种读取字符串的方法:

public String leerModelo(RandomAccessFile file) throws IOException
{
    char cadena[] = new char[25], temp;
    for (int c = 0; c < cadena.length; c++)
    {
        temp = file.readChar();
        cadena[c] = temp;
    }
    return new String(cadena).replace('\0', ' ');
}

而 cargarDatos 是加载我的数据:

public void cargarDatos(RandomAccessFile file, AutosRentables[] lista, int reg) throws IOException
{
    int cont = 0;
    do
    {

        modelo = this.leerModelo(file);
        color = this.leerColor(file);
        tipoAM = this.leerTipoAM(file);
        rendimientoGalon = file.readDouble();
        placa = this.leerPlaca(file);
        ACRISS = this.leerACRISS(file);
        codigo = file.readInt();
        costo = file.readDouble();
        marca = this.leerMarca(file);
        detalles = this.leerDetalles(file);

        lista[cont] = new AutosRentables(modelo, color, tipoAM, rendimientoGalon, placa, ACRISS, codigo, costo, marca, detalles);
        cont++;
        System.out.println("Entra");
    }
    while (cont < reg);
}

这是 ArrayoutOfbound 错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at rent_autos.Interfaz3.cargarDatosAutos(Interfaz3.java:6081)
at rent_autos.Interfaz3.<init>(Interfaz3.java:38)
at rent_autos.Interfaz3$159.run(Interfaz3.java:6107)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

因此,如果有人知道发生了什么,请在这里帮助我......它是文件的字节大小吗?,我真的不知道,帮助!

4

2 回答 2

1

EOFException means you tried to read past the end of the stream; i.e. the end of the file in this case. Probably you aren't positioning yourself correctly in the file. Reading chars from a random access file is tricky as you can't know how many bytes they are encoded as. I suspect you need to redesign the file actually. Or else you should be reading bytes not chars if it is coming from an external system?

于 2011-12-05T05:56:30.403 回答
1

java.io.EOFException:

对于 java.io.EOFException 检查此链接http://docs.oracle.com/javase/1.4.2/docs/api/java/io/EOFException.html

ArrayOutOfBounds:当您尝试访问索引超过其长度的数组时发生越界异常。java数组的最大索引是(长度-1)。这意味着您正在尝试将值插入到不存在的数组元素中。为了处理它,您应该确保您的程序不会访问索引大于长度的数组 - 1。

于 2011-12-05T11:55:50.977 回答