This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Exception in code when implementing Externalizable interface......

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting an EOF exception when running code for implementing Externalizable interface ... I am not able to understand why ??? Please help me ... I am pasting the code for reference .....

package com.example.programs;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;


class EmployeeData implements Externalizable{
private String fname;
private String lname;
private String address;
transient private double salary;
static int x = 25000;

public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}

public void readExternal(ObjectInput arg0) throws IOException, ClassNotFoundException {
System.out.println(arg0.getClass());
fname = arg0.readUTF();
lname = arg0.readUTF();
salary = arg0.readDouble();
}

public void writeExternal(ObjectOutput arg0) throws IOException {
arg0.writeUTF("Siddharth");
arg0.writeUTF("Bhargava");
arg0.writeDouble(25000);
}
}

public class ExternalizationProgram {

public static void main(String[] args) {
ExternalizeObject();
DeExternalizeObject();
}

public static void ExternalizeObject(){
try {

EmployeeData e = new EmployeeData();
/*e.setFname("Siddharth");
e.setLname("Bhargava");
e.setSalary(25000.00);
e.setAddress("Rajouri Garden");*/

FileOutputStream fout = new FileOutputStream("SidExternal.txt");
ObjectOutputStream o = new ObjectOutputStream(fout);
e.writeExternal(o);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void DeExternalizeObject(){
try {
FileInputStream fin = new FileInputStream("SidExternal.txt");
ObjectInputStream o = new ObjectInputStream(fin);

System.out.println(o.readUTF());

EmployeeData e = new EmployeeData();
e.readExternal(o);

System.out.println("First Name Is : "+e.getFname());
System.out.println("Last Name Is : "+e.getLname());
System.out.println("Salary Is : "+e.getSalary());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}


The Exception which i am getting is :
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source)
at java.io.ObjectInputStream.readUTF(Unknown Source)
at com.example.programs.ExternalizationProgram.DeExternalizeObject(ExternalizationProgram.java:91)
at com.example.programs.ExternalizationProgram.main(ExternalizationProgram.java:64)



Thanks
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your ExternalizeObject method calls writeExternal() which writes 2 UTF values and 1 double value.

Your DeExternalizeObject reads 1 UTF value then it calls readExternal() which attempts to read 2 UTF values and 1 double value, but you've already read one of the UTF values so there's only 1 left to read.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if i comment out the following line

// System.out.println(o.readUTF());

from the method DeExternalizeObject() then also i am getting an exception :

java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source)
at java.io.ObjectInputStream.readUTF(Unknown Source)
at com.example.programs.EmployeeData.readExternal(ExternalizationProgram.java:48)
at com.example.programs.ExternalizationProgram.DeExternalizeObject(ExternalizationProgram.java:94)
at com.example.programs.ExternalizationProgram.main(ExternalizationProgram.java:64)
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes. That was the first mistake I'd spotted and assumed it was the only thing wrong but now that you've mentioned that it still doesn't work I've had another look and noticed that you are not closing (or even flushing) the file streams so you probably aren't even wrinting the whole file. Have you checked to see if there is a data file and if so what is in it?
 
Not so fast naughty spawn! I want you to know about
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!