Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Class Casting

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Class Casting

    Hey i am trying to pass an object array from a client to server.....The problem is ,after passing it through the socket when i try to cast it back to client it gives me an exception


    "Exception in thread "main" java.lang.ClassCastException: [Ljavaapplication28.Client; cannot be cast to javaapplication28.Client
    "

    And i fail to understand what is wrng...Plz Help

    The code for Client is
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
     
     
     
    package javaapplication28;
    import java.net.*;
    import java.io.*;
     
    public class Client implements Serializable  {
        int value;
        String id;
        private Client[] Array;
     
       public Client[] getArray() {
     
            for (int i = 0 ; i < 5 ; i ++){
             Array[i].id = id;
             Array[i].value = value;
     
          }
       return Array;
        }
     
     
      public Client(int x , String putInId)
     
        {
             id = putInId ;
              value = x;
     
        }
     
     
     
        Client(){
            System.out.println("I am in constr");
         value = 1;
         id = "my program is working";
     
        }
    public static void main(String args[])
    {
        try{
          Socket accept_Socket = new Socket("127.0.0.1",1254);
          OutputStream through_data = accept_Socket.getOutputStream(); // getoutput returns the output stream where the data can be written
          ObjectOutputStream new_Object = new ObjectOutputStream(through_data);
     
     
          Client [] sendData = new Client [5];
          System.out.println("The Client Side" + sendData);
           String putInId = "1 + ";
             int x = 1;
     
           for (int i = 0 ; i < 5; i++)
                 {
     
                    sendData[i] = new Client (x,putInId);
                  }
     
     
     
          new_Object.writeObject(sendData);
          System.out.println("1");
     
          new_Object.close();
          System.out.println("2");
         through_data.close();
         System.out.println("3");
    //accept_Socket.close();
            }
            catch(Exception e)
                {
                        System.out.println(e);
                        System.out.println("4");
     
                }
        }
     
     
     
     
     
    }
    and the code for server is
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package javaapplication28;
     
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.Serializable;
    import java.net.ServerSocket;
    import java.net.Socket;
     
     
    /**
     *
     * @author Administrator
     */
    public class Main implements Serializable  {
     
    Main(){
    int value = 1;
    String id = "Sana";
     
    }
        public void driver() throws IOException, ClassNotFoundException
        {
     
            ServerSocket socket = new ServerSocket(1254);
            Socket accept_Socket = socket.accept();
            InputStream getData = accept_Socket.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(getData);
     
     
           Client y = null;
             y =  (Client) ois.readObject();
           Client [] check = new Client [5];
           check = y.getArray();
           //check [] = (Client) ois.readObject();
          // check[] = ois.readObject[];
                 for(int i = 0 ; i<5 ; i++)
                    {
                         System.out.println("The Server Side" + check[i].id);
                    }
         //  socket.close();
     
     
           accept_Socket.close();
           ois.close();
     
        }
     
     
     
        public static void main(String[] args) throws IOException, ClassNotFoundException {
     
            Main calling = new Main();
            calling.driver();
     
     
     
        }
     
     
     
    }
    Last edited by helloworld922; January 9th, 2012 at 10:36 AM.


  2. #2
    Member
    Join Date
    Dec 2011
    Posts
    50
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Class Casting

    [Ljavaapplication28.Client; cannot be cast to javaapplication28.Client
    You are trying to cast an array (Ljavaapplication28.Client) to a Client object (javaapplication28.Client) which definitely causes the error.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Class Casting

    @Sana1990: Please use the code tags to wrap your code in. If you don't know how, click Go Advanced button right below your reply panel. Then click the # sign and place your code inside.
    Thanks and don't forget to read the Forums Rules. Welcome to Java Programming Forums.

Similar Threads

  1. HELP: I have problem casting from Vector to Integer in Java
    By tintin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 17th, 2011, 12:39 PM
  2. [SOLVED] why casting int to String is not possible through brackets method
    By voltaire in forum Java Theory & Questions
    Replies: 2
    Last Post: May 2nd, 2010, 04:00 PM
  3. reading string input then casting it to an int?
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: March 27th, 2010, 11:49 PM
  4. Performance of Type Casting and Conversions
    By fritzoid in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2009, 07:56 PM
  5. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 04:11 PM