×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    Class Cast error

    Hello i want to make a simple rfcomm connection and i get an error that i don't understand and don't know how to get around it.Hope you guys can help.Here is the source code,and also interesting is that on the wtk emulator it works fine but on my mobile phone(nokia 5230) it doesn't .The error i get is java.lang.classcastexception javax.microedition.lcdui.StringItem incompatible with javax.microedition.lcdui.TextField if it helps
    Server code
    Code:
    public class EchoServer extends BluetoothMIDlet {
    
    	
    	
    	private void displayConnectionString(Form f,
    			StreamConnectionNotifier notifier) {
    			try {
    			// Retrieve the connection string to use to
    			// connect to this server
    			LocalDevice device = LocalDevice.getLocalDevice();
    			ServiceRecord record = device.getRecord(notifier);
    			String connString = record.getConnectionURL(
    			ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
    			int index = connString.indexOf(";");
    			
    			connString = connString.substring(0, index);
    			// Display the connection string on the Form
    			f.append(new StringItem("Connection String:",
    			connString));
    			} catch (BluetoothStateException e) {
    			f.append("BluetoothStateException: " +
    			e.getMessage());
    			}
    			}
    
    	public void run() {
    		// Create the output Form and set it to be the
    		// current Displayable
    		Form msgForm = new Form("Echo Server");
    		msgForm.addCommand(new Command("Exit",
    		Command.EXIT, 1));
    		msgForm.setCommandListener(this);
    		Display.getDisplay(this).setCurrent(msgForm);
    		try {
    			// Create the notifier object
    			StreamConnectionNotifier notifier =
    			(StreamConnectionNotifier)
    			Connector.open("btspp://localhost:"+
    			"123456789ABCDE;name=Echo Server");
    			//Display the connection string on the Form
    			displayConnectionString(msgForm, notifier);
    			for (;;) {
    				StreamConnection conn = notifier.acceptAndOpen();
    				OutputStream output = conn.openOutputStream();
    				InputStream input = conn.openInputStream();
    				// Continued reading the input stream until the
    				// stream is closed. Display the data on the
    				// screen and write it to the output stream.
    				byte [ ] data = new byte[10];
    				int length =0;
    				while ((length = input.read(data)) != -1) {
    				msgForm.append(new String(data, 0, length));
    				output.write(data, 0, length);
    				output.flush();
    				}
    				// Close the streams and the connection
    				output.close();
    				input.close();
    				conn.close();
    				}
    			} catch (IOException e) {
    			msgForm.append("IOException: "+ e.getMessage());
    			}
    			}
    			
    		
    	
    	
    }
    And the client side
    Code:
    public class EchoClient extends BluetoothMIDlet {
    	private Form connForm;
    	/**
    	* The Command used to Connect to the server.
    	*/
    	private Command connectCommand;
    	/**
    	* Called when the MIDlet is made active. This
    	* method displays a Form that retrieves the
    	* Bluetooth address and the channel ID of the
    	* Echo Server.
    	*/
    	// The InputStream to receive data from the server.
    	private InputStream input;
    	// The OutputStream to send data to the server.
    	private OutputStream output;
    	// The connection to the server
    	private StreamConnection conn;
    	
    	
    	public EchoClient() {
    		// TODO Auto-generated constructor stub
    	}
    
    	public void destroyApp(boolean unconditional) {
    		// TODO Auto-generated method stub
    
    	}
    
    	public void pauseApp() {
    		// TODO Auto-generated method stub
    
    	}
    
    	public void startApp() throws MIDletStateChangeException {
    
    		// Create the Form. Add the Connect and Exit
    		// Commands to the Form.
    		connForm = new Form("Echo Client");
    		connectCommand = new Command("Connect",
    		Command.OK, 1);
    		connForm.addCommand(connectCommand);
    		connForm.addCommand(new Command("Exit",
    		Command.EXIT, 1));
    		connForm.setCommandListener(this);
    		// Add the TextFields to retrieve the
    		// Bluetooth address and channel
    		// ID of the Echo Server
    		TextField address = new TextField("Address",
    		null, 12, TextField.ANY);
    		connForm.append(address);
    		TextField channel = new TextField("Channel",
    		null, 2, TextField.NUMERIC);
    		connForm.append(channel);
    		Display.getDisplay(this).setCurrent(connForm);
    		}
    	public void commandAction(Command c, Displayable d){
    		switch (c.getCommandType()) {
    		case Command.OK:
    		// The Connect Command was selected so start a
    		// thread to establish the connection to the server
    		new Thread(this).start();
    		break;
    		case Command.SCREEN:
    		// The Send Command was selected so send the
    		// message to the server
    	 		int index = connForm.size() - 1;
    			// If the last Item is a TextField, then no message
    			// is currently being sent so send a Message.
    			Item item = connForm.get(index);
    			if (item instanceof TextField) {
    			TextField field = (TextField)item;
    			connForm.delete(index);
    			// Start a thread to send the message to the
    			// server and process the reply
    			new Thread(new Message(field.getString() +
    			"\n", input, output)).start(); 
    			}
    	/*		try{
    						
    			output.write("Hello, World".getBytes());
    			}
    			catch(ClassCastException e){
    				connForm.append("Error message "+e.getMessage());
    			} 
    		
    			catch(IOException e){
    				connForm.append("Error message "+e.getMessage());
    				
    			} */
    			
    			break;
    			case Command.EXIT:// The Exit Command was selected so destroy the
    				// MIDlet
    				try {
    				input.close();
    				output.close();
    				conn.close();
    				} catch (Exception e) {
    				}
    				notifyDestroyed();
    				break;
    				}
    	}
    	
    	public void run() {
    		String connString =getConnectionString();
    		connForm.append("Connecting to Server\n");
    		if (connectToServer(connString)) {
    		connForm.append("Done");
    		// Remove the Connect Command and add the Send
    		// Command to this Form
    		connForm.removeCommand(connectCommand);
    		Command sendCommand = new Command("Send",
    		Command.SCREEN, 1);
    		connForm.addCommand(sendCommand);
    		// Add a TextField to the Form to retrieve the
    		// text to send to the server from the user
    		connForm.append(new TextField("Text to send",
    		null, 20, TextField.ANY));
    		}
    		}
    	private String getConnectionString() {
    		// Retrieve the TextFields from the Form
    		TextField address = (TextField)connForm.get(0);
    		TextField channel = (TextField)connForm.get(1);
    		// Create the connection string
    		StringBuffer temp = new StringBuffer("btspp://");
    		temp.append(address.getString());
    		temp.append(":");
    		temp.append(channel.getString());
    		// Remove the TextFields from the Form
    		connForm.delete(0);
    		connForm.delete(0);
    		return temp.toString();
    		}
    	private boolean connectToServer(String connString) {
    		try {
    		// Establish a connection to the server
    		conn = (StreamConnection)
    		Connector.open(connString);
    		// Retrieve the input and output streams to
    		//communicate with
    		input = conn.openInputStream();
    		output = conn.openOutputStream();
    		return true;
    		} catch (IOException e) {
    		connForm.append("Connect failed (IOException: ");
    		connForm.append(e.getMessage());
    		connForm.append(")\n");
    		return false;
    		}
    		}
    	
     class Message implements Runnable {
    	// The message to send to the server.
    	private String theMessage;
    	// The InputStream to read the reply from.
    	private InputStream input;
    	// The OutputStream to send the message to.
    	private OutputStream output;
    	/**
    	* Creates a new Message to send to the server.
    	*
    	* @param msg the message to send
    	* @param in the InputStream to read the reply from
    	* @param out the stream to write the message to
    	*/
    	public Message(String msg, InputStream in,
    	OutputStream out) {
    	theMessage = msg;
    	input = in;
    	output = out;
    	}
    	
    	/**
    	* Sends the message to the server and reads the echo
    	* in reply. The method adds the echo to the Form and
    	* then adds a new TextField to the end of the Form.
    	*/
    	public void run() {
    	try {
    	// Send the message to the server.
    	byte[] data = theMessage.getBytes();
    	output.write(data);
    	output.flush();
    	// Read the reply and keep it in a StringBuffer
    	// until the full reply is received.
    	int fullLength = data.length;
    	int length = input.read(data);
    	fullLength -= length;
    	StringBuffer buf = new StringBuffer(new
    	String(data, 0, length));
    	while (fullLength > 0) {
    	length = input.read(data);
    	fullLength -= length;
    	buf = buf.append(new String(data, 0, length));
    	}
    	// Display the reply on the Form and remove the
    	// final new line sent from the server
    	connForm.append("\n");
    	String displayString = buf.toString();
    	displayString = displayString.substring(0,
    	displayString.length() - 1);
    	connForm.append(displayString);
    	} catch (IOException e) {
    	connForm.append("\nFailed to send message: " +
    	e.getMessage());
    	}
    	
    	connForm.append(new TextField("Text to send",
    	null, 20, TextField.ANY));
    	}
    	}
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: Class Cast error

    he error i get is java.lang.classcastexception javax.microedition.lcdui.StringItem incompatible with javax.microedition.lcdui.TextField if it helps
    Are you trying to Case the StringItem on the form directly in to textField, if yes avoid it and use getText from string item and then setText() on textfield.
    Thanks,
    Ekta

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    Re: Class Cast error

    Quote Originally Posted by ektasrv View Post
    Are you trying to Case the StringItem on the form directly in to textField, if yes avoid it and use getText from string item and then setText() on textfield.
    Thanks for the reply but i fixed it....it was something like that i'm not sure exactly but when i put the server on a normal java project it worked ,and i need it like that anyway but thanks for your advice.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105
    Viocartman, stil, I recommend to do at least a service discovery. Furthermore, an UUID should be unique. There are several tools out there which create a (long) 128 bit UUID for you.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    Re: Class Cast error

    Quote Originally Posted by traud View Post
    Viocartman, stil, I recommend to do at least a service discovery. Furthermore, an UUID should be unique. There are several tools out there which create a (long) 128 bit UUID for you.
    Thank you very much for your interest but for my project to do service search will be time consuming since i know where my server will be,and i need clients to connect to it,not for the server to search for clients.I want to send tests from the server to clients so it's easier just to publish the connection string.I followed your advice and got an unique uuid.I wanted to ask something else now is it possible for clients to connect to the same port?Or i should change somehow the port everytime a new client connects?I haven't worked with threads before and my teacher said i will be using multithreading for this....Hope you understood my question...thanks in advance

Similar Threads

  1. Errors compiling speex1.2-rc1 in Symbian 3th edition SDK
    By mauriziomento in forum Symbian Media (Closed)
    Replies: 2
    Last Post: 2010-03-23, 00:15
  2. error LNK2019: unresolved external symbol ???
    By wgarnett in forum Symbian
    Replies: 3
    Last Post: 2009-09-28, 06:53
  3. Error loading connection when obfuscation high?
    By ejakkk in forum Mobile Java General
    Replies: 8
    Last Post: 2009-06-26, 12:54
  4. [newbie here] Link error
    By xitpu in forum Symbian
    Replies: 3
    Last Post: 2007-01-24, 16:53

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×