Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4335214
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:38:20+00:00 2026-05-21T10:38:20+00:00

I have an app in android which is something like a client server application

  • 0

I have an app in android which is something like a client server application and inside my program at a certain point I start a new thread meant to connect the client to the server.

My problem is that when I start this new thread I don’t know what happens but when I try to interact with my GUI(press a button that takes me to another activity) I get force close.

It’s a little bit confusing cause as I read on different web pages the new thread I start has nothing to di with my GUI so even if the client tries to connect or fails I should be able to
“play” with my interface.

Here is my code:

public class screen1 extends Activity {

private TextView clientState;
private String serverIpAddress="10.0.2.2";
public static final int ClientPort = 8080;
String message="Hello Server!";
int longitude;
int latitude;
private PrintWriter out=null;
DBAdapter db;
private Handler handler=new Handler();
Socket socket;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen1);
    clientState = (TextView) findViewById(R.id.client_Status);

//this is the button meant to get me to another activity and when pressed it gives me FC

    Button b = (Button)findViewById(R.id.mainMenu);
    b.setOnClickListener(new View.OnClickListener() {
       public void onClick(View arg0) {
       Intent i = new Intent(screen1.this, screen2.class);
       startActivity(i);
       } 
    });

//here I start a new thread defined below

  Thread cThread=new Thread(new ClientThread()); 

  cThread.start();  
db=new DBAdapter(this);

}

//here it is my client that tries to connect

public class ClientThread implements Runnable{


    public void run()
    {
        try
        {
            InetAddress serverAddr=InetAddress.getByName(serverIpAddress);
            handler.post(new Runnable(){
                public void run(){
                    clientState.setText(" try to connect!");
                }
            });
            socket=new Socket(serverAddr,ClientPort);

            //connected=true;
        handler.post(new Runnable(){
                public void run(){
                    clientState.setText("Connected!");
                }
            });

        PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
        db.createDatabase();
         db.openDataBase();
           Cursor c=db.getAllData();

           if(c.moveToFirst()) 
             {
              do{

                  longitude=Integer.parseInt(c.getString(1));
                  out.println(longitude);
                  latitude=Integer.parseInt(c.getString(2));
                  out.println(latitude);

              }while(c.moveToNext());

             }




        }
        catch(Exception e){
            handler.post(new Runnable(){
                public void run(){
                    clientState.setText("Error");
                    }
            });

            e.printStackTrace();
        }

    }

}

And onStop() method:

protected void onStop() {

 super.onStop();

  try {

        out.close();
        socket.close();

     } catch (IOException e) {
         e.printStackTrace();
     }
 }
}

Well,this is it,for further details I’m here to explain.Thank u in advance:)

0

4-16 03:51:25.045: ERROR/AndroidRuntime(242): Uncaught handler: thread main exiting due to uncaught exception

04-16 03:51:25.074: ERROR/AndroidRuntime(242): java.lang.RuntimeException: Unable to stop activity {test.android/test.android.screen1}: java.lang.NullPointerException

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3227)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3272)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at android.app.ActivityThread.access$2500(ActivityThread.java:119)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1880)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at android.os.Handler.dispatchMessage(Handler.java:99)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at android.os.Looper.loop(Looper.java:123)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at android.app.ActivityThread.main(ActivityThread.java:4363)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at java.lang.reflect.Method.invokeNative(Native Method)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at java.lang.reflect.Method.invoke(Method.java:521)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at dalvik.system.NativeStart.main(Native Method)

04-16 03:51:25.074: ERROR/AndroidRuntime(242): Caused by: java.lang.NullPointerException

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at 
test.android.screen1.onStop(screen1.java:106)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at 
android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1169)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at android.app.Activity.performStop(Activity.java:3797)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3224)

04-16 03:51:25.074: ERROR/AndroidRuntime(242):     ... 11 more

04-16 03:51:25.144: INFO/Process(51): Sending signal. PID: 242 SIG: 3

04-16 03:51:25.154: INFO/dalvikvm(242): threadid=7: reacting to signal 3

04-16 03:51:25.204: INFO/dalvikvm(242): Wrote stack trace to '/data/anr/traces.txt'

04-16 03:51:41.604: INFO/Process(242): Sending signal. PID: 242 SIG: 9

04-16 03:51:41.714: INFO/ActivityManager(51): Process test.android (pid 242) has died.

04-16 03:51:41.753: INFO/WindowManager(51): WIN DEATH: Window{43e0eb10 test.android/test.android.screen2 paused=false}

04-16 03:51:41.765: INFO/WindowManager(51): WIN DEATH: Window{43e068b0 test.android/test.android.screen1 paused=false}

04-16 03:51:41.974: INFO/ActivityManager(51): Start proc test.android for activity test.android/.screen1: pid=249 uid=10030 gids={3003}

04-16 03:51:42.433: DEBUG/ddm-heap(249): Got feature list request

04-16 03:51:42.704: INFO/UsageStats(51): Unexpected resume of test.android while already resumed in test.android

04-16 03:51:43.294: WARN/InputManagerService(51): Got RemoteException sending setActive(false) notification to pid 242 uid 10030

04-16 03:51:43.513: INFO/ActivityManager(51): Displayed activity test.android/.screen1: 1736 ms (total 1736 ms)

Well,dear friends I’ve removed from my code the onStop() function and everything seems to work!

protected void onStop() {

    super.onStop();

    try {

        out.close();


        socket.close();

     } catch (IOException e) {

         e.printStackTrace();

     }
}

What is wrong with it?:-S

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T10:38:21+00:00Added an answer on May 21, 2026 at 10:38 am

    I assume that the variable out in your onStop method of the thread is null. You declare the variable, but you initialize it with null and never initialize another value to it.

    You do

    PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
    

    in the run method of your thread, but this variable has a different scope than the variable used in your onStop method. That’s why you get a NullPointerException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.