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

  • SEARCH
  • Home
  • 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 8932885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:24:29+00:00 2026-06-15T09:24:29+00:00

Possible Duplicate: Force close using sockets and AsyncTask Hi people of stack…. Please please

  • 0

Possible Duplicate:
Force close using sockets and AsyncTask

Hi people of stack….

Please please please help me, I am losing my mind.

Please see code and logcat…..

Basically what the code is doing is:

  1. Taking an IP Address from an intent.
  2. Connecting to the IP with port 32
  3. Then send a command, wait for response and the send another command.
  4. After the 2 commands our sent I should get a response of “SNX_COM>”
  5. Once the connection is established, I want the connection to stay open to send specific commands on button click.

^^That’s basically what my code should achieve ^^

Java Class

package com.smarte.smartipcontrol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;

public class IPControl extends Activity {

private Socket socket;
private String serverIpAddress = "com.smarte.smartipcontrol.ACTU_IP";
private static final int REDIRECTED_SERVERPORT = 32;
public PrintWriter out;
public BufferedReader in;
public String data;
public Object pd;



public void getModel(View view) {
    try {
        out.println("[m\r\n");
        //System.out.print("root\r\n");
        while(!in.ready());
        String textStatus = readBuffer();

    } catch(IOException e) {}
}


@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   setContentView(R.layout.act_ipcontrol);


   try{   

    new AsyncAction().execute();

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

   }



private class AsyncAction extends AsyncTask<String, Void, String> {
   protected String doInBackground(String... args) { 
   try {
 InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
 socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e1) {
 e1.printStackTrace();
} catch (IOException e1) {
 e1.printStackTrace();
}
try {
 out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
 while (! in .ready());
 readBuffer();
 out.println("root\r\n");
 //System.out.print("root\r\n");
 while (! in .ready());
 readBuffer();
 out.println("root\r\n");
 //System.out.print("root\r\n");
 while (! in .ready());
 String msg = "";

while ( in .ready()) {
 msg = msg + (char) in .read();
}
} catch (IOException e) {}

       return null;//returns what you want to pass to the onPostExecute()
   }

   protected void onPostExecute(String result) {

   //results the data returned from doInbackground

       IPControl.this.data = result;



       }
   }


private String readBuffer() throws IOException {
    String msg = "";

    while(in.ready()) {
        msg = msg + (char)in.read();
    }
    //System.out.print(msg);
    if(msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
    else return msg;
}

}

LogCat….

12-03 21:56:43.670: E/AndroidRuntime(1231): FATAL EXCEPTION: AsyncTask #1
12-03 21:56:43.670: E/AndroidRuntime(1231): java.lang.RuntimeException: An error occured while executing doInBackground()
12-03 21:56:43.670: E/AndroidRuntime(1231):     at  android.os.AsyncTask$3.done(AsyncTask.java:299)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.lang.Thread.run(Thread.java:856)
12-03 21:56:43.670: E/AndroidRuntime(1231): Caused by: java.lang.NullPointerException
12-03 21:56:43.670: E/AndroidRuntime(1231):     at com.smarte.smartipcontrol.IPControl$AsyncAction.doInBackground(IPControl.java:71)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at com.smarte.smartipcontrol.IPControl$AsyncAction.doInBackground(IPControl.java:1)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-03 21:56:43.670: E/AndroidRuntime(1231):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-03 21:56:43.670: E/AndroidRuntime(1231):     ... 4 more
  • 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-06-15T09:24:30+00:00Added an answer on June 15, 2026 at 9:24 am

    This happens when you swallow exceptions… You have a NPE at one of these line (it can’t be said which one because you pasted both on the same line 71):

    out = new PrintWriter(new BufferedWriter(
        new OutputStreamWriter(socket.getOutputStream())), true);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    

    socket is an instance variable of IPControl (just like in and out) and this means that if for whatever reason the connection can’t be estabilished, the program just prints the stack trace and continue:

    try {
        InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
        socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    // next of the code
    

    If you examine your console carefully (or maybe the Eclipse console) you’ll find exactly what happens, and thus what you can do to fix the actual problem.

    However you should really put all of your code inside the first try block, because it doesn’t make any sense to continue if the connection cannot be estabilished.

    Also, socket, in and out should not need to be instance variable – we can’t give further advices because you provided no context – however since these are not reused in the rest of the Activity’s code, you can freely make them local variables. Finally, it’s good practice to close() everything when you’re done (that’s another reason not to use instance variables).

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

Sidebar

Related Questions

Possible Duplicate: Force Download an Image Using Javascript Basically I want to have the
Possible Duplicate: How do I force a stack backtrace for all fatal errors in
Possible Duplicate: How to force using zxing lib with only my application? I use
Possible Duplicate: Javascript force open a link in a browser I am working on
Possible Duplicate: Mobile site - force landscape only / no auto-rotate I have seen
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Setting up a build dependency without using a reference? Is it possible
Possible Duplicate: Entity Framework with NOLOCK I'm using EF4 and .Net 4 to load
Possible Duplicate: How to force C# App to run as administrator on Windows 7
Possible Duplicate: How to force indentation of C# conditional directives? Can visual studio automatically

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.