The Artima Developer Community
Sponsored Link

Java Answers Forum
Object File Input/Array problem

5 replies on 1 page. Most recent reply: Jun 3, 2006 7:37 PM by kat ta

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
kat ta

Posts: 6
Nickname: codekat
Registered: Apr, 2006

Object File Input/Array problem Posted: May 24, 2006 5:54 PM
Reply to this message Reply
Advertisement
Hi all,

I'm having major probems.. this is my code below:

==================================================

import java.io.*;

public class SalesTransactionReader{

private int[] saleType;
SalesTransaction[] allSales;

public static void main(String[] args) {


}


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

int j = 0;
while( j < saleType.length && allSales.getSaleType() != saleType[j]){
j++;
}

if (j == saleType.length){
allSales.setFlag(false);
}else{
allSales.setFlag(true);
}
}

//input done, so close the stream
myObjectInputStream.close();

}
}

===================================== ===============


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


Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: Object File Input/Array problem Posted: May 25, 2006 2:35 AM
Reply to this message Reply
allSales is declared as a SalesTransaction[], but you are assigning to it and trying to use it as a SalesTransaction.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Object File Input/Array problem Posted: May 25, 2006 2:53 AM
Reply to this message Reply
No, he didn't
The correct line would be
allSales[i] = (SalesTransaction)myObjectInputStream.readObject(); //THIS IS WHERE THE ERROR IS


but he forgot to use the Java-Tage, so [ i ] was interpretd as a italic tag and removed.

That's why people should use the Java tags.

Before investigating this any further the code should be reposted with Java tags.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Object File Input/Array problem Posted: May 25, 2006 2:56 AM
Reply to this message Reply
Ok, here's the error.

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.

nagasrikanth chalasani

Posts: 3
Nickname: cnsrikanth
Registered: May, 2006

Re: Object File Input/Array problem Posted: May 25, 2006 10:32 PM
Reply to this message Reply
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....

kat ta

Posts: 6
Nickname: codekat
Registered: Apr, 2006

Re: Object File Input/Array problem Posted: Jun 3, 2006 7:37 PM
Reply to this message Reply
Thanks everyone for your help! I was suppose to put int[] saleType instead of int[] allSales in the method declaration!!

THANKS all!

Flat View: This topic has 5 replies on 1 page
Topic: Converting .class files to .java files Previous Topic   Next Topic Topic: BIRT Report Problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use