Hello
Im trying to code something like this

public class Decoding{


Decoding(InputStream in) //constructor

Object obj = new XMLDecoder(in)

}

}
public class Test{

public static main(String[] args0){

JButton button = new JButton();
decoder = new Decoding(new FileInputStream("somtheing.xml"));
button= decoder
}
}

So as you see I have a special decoding class, and i wan't to decode my xml file ito a java object and pass it to an other class. I've been trying on many ways to do this with no efect. Im counting on your help. Thanks.

You need to
1. Create a new XMLDecoder, passing an input stream as parameter
then (and this is the bit you are missing)
2. Use the XMLDecoder's readObject() method to get the object itself.

Can you show it on an exmaple because i just did it and it doesn't work

It's a single method call with no parameters; you don't need an example for that.
What exactly did you code, and in what way exactly didn't it "work"?

Well Ive got two clases

public class Testing {


	public static void main(String[] args) throws FileNotFoundException {
	
		
		Object de=new Decoding(new FileInputStream("TestXML2.xml"));
		JButton button1=(JButton) de;
		

	}

}
public class Decoding {

	Decoding(InputStream in){
		
		Object obj=new Object(); 
		XMLDecoder dec = new XMLDecoder(in);
		obj=dec.readObject();
		
		
		
	}
	
	
}

OUTCOME:

Exception in thread "main" java.lang.ClassCastException: Decoding cannot be cast to javax.swing.JButton
at Testing.main(Testing.java:15)

That's because you still didn't use the readObject method in your Testing class.

If you don't mind me saying so, your structure here is far too complex, and a bit misdirected. Your Decoding constructor has a local variable obj, and (probably) successfully creates it from the XML. Only problem is: it's a local variable and gets discarded as soon as the constructor finishes executing. You have a class Decoding where all you need is a simple method that takes the input stream as parameter and returns the Object read from the XML as its return value. In fact that method only needs one line of code and no local variables, so it's not really worth having at all.
Just try getting it to work as a simple bit of in-line code in your main method, then think about how (or whether) to package it in a separate method or class.

Thanks James for bringing it to work.

public class Testing {
	public static void main(String[] args) throws FileNotFoundException {					
		Object de=new Decoding().returnObj(new FileInputStream("TestXML2.xml"));
		JButton button1=(JButton) de;	
	}
}
public class Decoding {
public	static  Object obj;
InputStream in;	
	Decoding(){				
	}
	
public Object returnObj(InputStream in){	
	XMLDecoder dec=new XMLDecoder(in);
	obj=dec.readObject();
	return obj;	
}

But what a realy need is to bring it to structure like I said before.

I want to use class Decoding with input of stream and return it as an object, which will by passed to the JButton.

Object de=new Decoding(new FileInputStream("TestXML2.xml"));
JButton button1=(JButton) de;

Edited 5 Years Ago by hatebin: something more

Object de=new Decoding(new FileInputStream("TestXML2.xml"));
JButton button1=(JButton) de;

To do that you need the Decoding class extend the JButton class. Then I'm not sure how it would work.
Constructors "return" an instance of their class. Not another class
If you want another data type returned, then use a method.

As I said before, I don't understand what you are trying to achieve with your decoding class. I don't see how having a separate class and method is any better.
Nevermind. Glad you got it working.
J

I don't see how having a separate class and method is any better.

Because it doesn't only do one thing.

Thanks guys for help !

This article has been dead for over six months. Start a new discussion instead.