1

I am trying to create a sort of simple GUI where im trying to save a couple of Strings and doubles and one int, im using the basic property of OOP which is inheritance I created a class Autos which is essentially the super Class.

The problem seem to arise in a method called "cargarDatosAutos" this is in my GUI class, and here is the code:

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
    }
}

Here are the errors:

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)

the leerModelo is a method that reads strings:

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', ' ');
}

And the cargarDatos is to load my data:

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);
}

And heres the ArrayoutOfbound error:

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)

So if anyone knows whats going on please help me out here... is it the byte size of the file?, I really dont know, HELP!

wattostudios
  • 8,666
  • 13
  • 43
  • 57
user1079801
  • 31
  • 1
  • 6
  • Irelevant to your post, but why do you use a non English language in your code? I worked on such a project one and it was the worst thing ever. – cherouvim Dec 05 '11 at 05:57
  • @cherouvim Probably because the OP isn't a native English speaker. – Dave Newton Dec 05 '11 at 06:04
  • @DaveNewton I am not either but I always code in English because I wouldn't want you to inherit my application code in Greek ;) – cherouvim Dec 05 '11 at 06:36
  • I assume `leerModelo(file)` in your code is to read a model data every 25 characters from a file in random-access mode. The problem is that it is possible that file is still empty at that moment or is not having enough characters (less than 25 characters in length) and you didn't make a check to avoid such traps. Reading in random-access mode is quite risky if you don't validate the read data – ee. Dec 05 '11 at 06:38

2 Answers2

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?

user207421
  • 305,947
  • 44
  • 307
  • 483
1

java.io.EOFException:

For java.io.EOFException check this link http://docs.oracle.com/javase/1.4.2/docs/api/java/io/EOFException.html.

ArrayOutOfBounds: Out of bounds exception is occurred when you try to access an array with index that exceeded its length. maximum index of a java array is (length -1). It means that you are trying to insert a value into an array element taht doesnt exist. For handling it you should make sure that your program doesn't access an array with index bigger than length - 1.