public void readAllSalesTransactions(String filename, int allSales[])throws ClassNotFoundException, IOException { //setup file and stream File mySalesFile = new File("objects.txt"); FileInputStream myInputFileStream = new FileInputStream(mySalesFile); DataInputStream myDataInputStream = new DataInputStream(myInputFileStream);
//get the integer at the start of the file //using the data input stream int numberOfObjects = myDataInputStream.readInt();
//Close Data Input Stream myDataInputStream.close();
// setup object stream ObjectInputStream myObjectInputStream = new ObjectInputStream(myInputFileStream );
for(int i = 0; i < numberOfObjects; i++){ allSales = (SalesTransaction) myObjectInputStream.readObject(); //THIS IS WHERE THE ERROR IS
I am using 'Eclipse' program and it is giving me an error saying: "Type mismatch: cannot convert from SalesTransaction to int" and also: "Cannot invoke getSaleType() on the primitive type int"
PLEASE HELP... it looks fine to me but I dont know?!?! -Kath
You use this class Variable: SalesTransaction[] allSales;
But then you pass this variable to the readAllSalesTransactions method: int[] allSales;
It has the same name as the class variable, so inside this method the class variable is not visible. In the line with the error you try to assign a SalesTransaction object to a int variable.
There are 2 errors #####1:#### AllSales is an array of objects. You cannot invoke getSaleType on an array. You can invoke this on an object inside this array, if that object has that method.
i.e allSales.getSaleType // error allSales[x].getSaleType // correct Note: X is from 0 to allSales.length -1
#####2:#### myObjectInputStream.readObject() returns an Object which is of type SalesTransaction not Array.
You are typecasting and assigning to a different type i.e Array/List or collection what ever....