Theguy
Theguy

Reputation: 195

Somebody please explain the error

I am new to android programming.I have been trying to establish connection between two emulators.While my server emulator is up and running,client has a problem.Here is the code and logcat error description.Please tell me the error in this.

public class SocketClient extends Activity 
{

   private Button bt;
   private TextView tv;
   private Socket socket;
   private String serverIpAddress = "192.168.0.5";
   private static final int REDIRECTED_SERVERPORT = 5000;
   public void connect()
   {
   try 
   {
         InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
         tv.setText((CharSequence) serverAddr);
         socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
    }
    catch (UnknownHostException e1) 
    {
         e1.printStackTrace();
         System.out.println("Here");
    }
    catch (IOException e1)
    {
         e1.printStackTrace();
         System.out.println("Here too");
    }
    }


   @Override
    public void onCreate(Bundle savedInstanceState)
    {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  bt = (Button) findViewById(R.id.myButton);
  tv = (TextView) findViewById(R.id.myTextView);
  connect();
  bt.setOnClickListener(new OnClickListener()
  {
     public void onClick(View v)
     {
        try 
        {
           EditText et = (EditText) findViewById(R.id.EditText01);
           String str = et.getText().toString();
           PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
           out.println(str);
           Log.d("Client", "Client sent message");
        }
        catch (UnknownHostException e) 
        {
           tv.setText("Error1");
           e.printStackTrace();
        }
        catch (IOException e) 
        {
           tv.setText("Error2");
           e.printStackTrace();
        }
        catch (Exception e) 
        {
           tv.setText("Error3");
           e.printStackTrace();
        }
     }
  }
  );
 }
}

The logcat error is

01-31 04:42:51.170: W/dalvikvm(529): threadid=1: thread exiting with uncaught exception           (group=0x409c01f8)
01-31 04:42:51.187: E/AndroidRuntime(529): FATAL EXCEPTION: main
01-31 04:42:51.187: E/AndroidRuntime(529): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.app.ServerClient/com.app.ServerClient.SocketClient}:   java.lang.ClassCastException: java.net.Inet4Address cannot be cast to java.lang.CharSequence
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.os.Looper.loop(Looper.java:137)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-31 04:42:51.187: E/AndroidRuntime(529):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 04:42:51.187: E/AndroidRuntime(529):  at java.lang.reflect.Method.invoke(Method.java:511)
01-31 04:42:51.187: E/AndroidRuntime(529):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-31 04:42:51.187: E/AndroidRuntime(529):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-31 04:42:51.187: E/AndroidRuntime(529):  at dalvik.system.NativeStart.main(Native Method)
01-31 04:42:51.187: E/AndroidRuntime(529): Caused by: java.lang.ClassCastException: java.net.Inet4Address cannot be cast to java.lang.CharSequence
01-31 04:42:51.187: E/AndroidRuntime(529):  at com.app.ServerClient.SocketClient.connect(SocketClient.java:25)
01-31 04:42:51.187: E/AndroidRuntime(529):  at com.app.ServerClient.SocketClient.onCreate(SocketClient.java:48)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.Activity.performCreate(Activity.java:4465)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-31 04:42:51.187: E/AndroidRuntime(529):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-31 04:42:51.187: E/AndroidRuntime(529):  ... 11 more

Thanks in advance.


Thank You David and Jitendra.

I corrected the error and I get a nullPointerException in one part of the code.What did I do wrong?

public void onClick(View v)
     {
        try                                                      // The error is in                this block
        {
           EditText et = (EditText) findViewById(R.id.EditText01);
           String str = et.getText().toString();
           PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
           out.println(str);
           Log.d("Client", "Client sent message");
        }
        catch (UnknownHostException e) 
        {
           tv.setText("Error1");
           e.printStackTrace();
        }
        catch (IOException e) 
        {
           tv.setText("Error2");
           e.printStackTrace();
        }
        catch (Exception f)                 // I get the error here. java.lang.NullPointerException
        {
           tv.setText("Error3");
           tv.setText(f.toString());
           f.printStackTrace();
        }

     }

Upvotes: 2

Views: 934

Answers (2)

jeet
jeet

Reputation: 29199

Exception is in following line:

tv.setText((CharSequence) serverAddr);

and it is because you are trying to cast serverAddr into CharSequence.

If you really want to print serverAddr use

tv.setText((CharSequence) serverAddr.toString());

Upvotes: 2

David Hedlund
David Hedlund

Reputation: 129782

Your error is on this line:

tv.setText((CharSequence) serverAddr);

serverAddr is of type InetAddress, and you're trying to cast it to a CharSequence which cannot be done. Perhaps you meant:

tv.setText(serverIpAddress);

Upvotes: 2

Related Questions