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 9156143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:43:06+00:00 2026-06-17T12:43:06+00:00

I am developing one socket based application. but the problem is my application working

  • 0

I am developing one socket based application. but the problem is my application working properly in emulator but when i tested in real devices it crashed many times.

Here is my Logcat :

01-21 16:24:38.667: E/AndroidRuntime(3525): FATAL EXCEPTION: main
01-21 16:24:38.667: E/AndroidRuntime(3525): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.inextrix.automation/org.inextrix.automation.MainActivity}: java.lang.NullPointerException
01-21 16:24:38.667: E/AndroidRuntime(3525):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at android.os.Looper.loop(Looper.java:137)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at java.lang.reflect.Method.invokeNative(Native Method)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at java.lang.reflect.Method.invoke(Method.java:511)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at dalvik.system.NativeStart.main(Native Method)
01-21 16:24:38.667: E/AndroidRuntime(3525): Caused by: java.lang.NullPointerException
01-21 16:24:38.667: E/AndroidRuntime(3525):     at org.inextrix.automation.MainActivity$1.updateStatus(MainActivity.java:272)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at org.inextrix.automation.MainActivity$1.run(MainActivity.java:250)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at org.inextrix.automation.MainActivity.startRepeatingTask(MainActivity.java:485)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at org.inextrix.automation.MainActivity.onCreate(MainActivity.java:205)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at android.app.Activity.performCreate(Activity.java:4492)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-21 16:24:38.667: E/AndroidRuntime(3525):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-21 16:24:38.667: E/AndroidRuntime(3525):     ... 11 more

Here is my MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);            
    getSharedSettings();

    fanDimmer1=(ToggleButton)findViewById(R.id.button_fan1);
    fanDimmer2=(ToggleButton)findViewById(R.id.button_fan2);
    dimmerLight1=(ToggleButton)findViewById(R.id.button_light1);
    dimmerLight2=(ToggleButton)findViewById(R.id.button_light2);


    fanDimmer1.setOnClickListener(this);
    fanDimmer2.setOnClickListener(this);
    dimmerLight1.setOnClickListener(this);
    dimmerLight2.setOnClickListener(this);

    if(ip.equals("") || port.equals(""))
    {
        new AlertDialog.Builder(MainActivity.this)
        .setTitle("Warning !")
        .setMessage("Please set IP and PORT first")
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setNeutralButton("ok", null)
        .show();
    }
    else
    {
        new Thread(new Runnable() 
        {   
            @Override
            public void run()
            {
                Log.v(TAG, "openconnection");
                openConnection();
            }
        }).start();
        m_handler = new Handler();
        startRepeatingTask();

    }

}

public void openConnection()
{
    // TODO Auto-generated method stub
    try 
    {
        s = new Socket(ip, Integer.parseInt(port));
        i = s.getInputStream();
        iD = new DataInputStream(i);
        o = s.getOutputStream();
        oD = new DataOutputStream(o);
        Log.v(TAG, "openconnection 2");

    }
    catch (UnknownHostException e) {
        // TODO: handle exception
        Log.v("UnknowHostException :::::", "In Catch Block");
        e.printStackTrace();
    }
    catch (IOException e) {
        // TODO: handle exception
        Log.v("IOException :::::", "In Catch Block");
        e.printStackTrace();
    }
}

Runnable m_statusChecker = new Runnable() 
{
    @Override
    public void run() 
    {
        if (count == 0) {
            updateStatus();
            count = 1;
        } else {
            updateStatus1();
            count = 0;
        }
        m_handler.postDelayed(m_statusChecker,1000);
    }

    private void updateStatus() 
    {
        // TODO Auto-generated method stub
        Log.v("test", "1");
        try {
            byte[] data1 = new byte[1024], packet1 = 
                { 
                    (byte) 0x00,(byte) 0x00,(byte) 0x00, 
                    (byte) 0x00,(byte) 0x00,(byte) 0x06, 
                    (byte) 0x01,(byte) 0x01,(byte) 0x00,
                    (byte) 0x00,(byte) 0x00,(byte) 0x19
                };

            o.write(packet1); //line number 272
            i.read(data1, 0, 1024);

            byte_to_hex = ConversionMethods.bytesToHex(data1).substring(18, 26);

            /*Log.d(TAG,"Original String ::: "+ byte_to_hex);*/

            char[] arr = byte_to_hex.toCharArray();
            for (int i = 0; i < arr.length - 1; i += 2) 
            {
                char temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }

            swapped_result=new String(arr);
            result = ConversionMethods.hexStringToNBitBinary(swapped_result, 32);

            int counter = 0;
            for( int i=0; i<result.length(); i++ ) 
            {
                if( result.charAt(i) == '1' )
                {
                    counter++;        
                }  
            }

            status=Integer.toString(counter);
            txt_status.setText(status);
            Log.v(TAG, "status is ::"+status);


            char[] c=result.toCharArray();
            int count=0;
            for (int i=0;i<result.length();i++)
            {
                count++;
                char j=c[i];
                //Log.v(TAG, count+"::"+j);
                if(count==1)
                    toggleButton=dimmerLight1;
                else if(count==2)
                    toggleButton=fanDimmer2;
                else if(count==3)
                    toggleButton=fanDimmer1;
                else if(count==4)
                    Log.v(TAG, "Count 4 is 0");

                if(j=='1')
                    toggleButton.setChecked(true);
                else
                    toggleButton.setChecked(false); 
            }

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

    }

    private void updateStatus1() 
    {
        Log.v("test", "2");
        try {
            byte[] data1 = new byte[1024], packet1 = 
                { 
                    (byte) 0x00,(byte) 0x00,(byte) 0x00, 
                    (byte) 0x00,(byte) 0x00,(byte) 0x06, 
                    (byte) 0x01,(byte) 0x03,(byte) 0x00,
                    (byte) 0x01,(byte) 0x00,(byte) 0x04
                };

            o.write(packet1);
            i.read(data1, 0, 1024);

            /*Log.v("::::TX::::", ConversionMethods.bytesToHex(data1).substring(0, 34));
            Log.v("::::RX::::", ConversionMethods.bytesToHex(packet1));*/

            d1=ConversionMethods.bytesToHex(data1).substring(20, 22);
            d2=ConversionMethods.bytesToHex(data1).substring(24, 26);
            d3=ConversionMethods.bytesToHex(data1).substring(28, 30);
            d4=ConversionMethods.bytesToHex(data1).substring(32, 34);

            Log.v(TAG, "D1="+d1);
            Log.v(TAG, "D2="+d2);
            Log.v(TAG, "D3="+d3);
            Log.v(TAG, "D4="+d4);

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

    }
};

void startRepeatingTask() {
    m_statusChecker.run();
}

void stopRepeatingTask() {
    m_handler.removeCallbacks(m_statusChecker);
}

i am stuck with this issue and can’t able to find the reason why this happens. please give me some idea how do i fix this issue?

Any idea and help will be appreciated.

Thanks

  • 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-17T12:43:07+00:00Added an answer on June 17, 2026 at 12:43 pm

    I can’t understand your logic. Why do you call openConnection from dedicated thread and then working with sockets from UI thread?

    I think that on emulator you got lucky and openConnection finishes before you call updateStatus from startRepeatingTask. That is why it works. On the real device you weren’t so lucky. Check with debugger that o and i are not null.

    Also you share several data objects between threads without proper synchronization. This can give you very tricky bugs )

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

Sidebar

Related Questions

Iam developing one application.In that iam using the uilabels.And my problem is i need
iam developing one application.In that i need to get the how many music files
iam developing one application.In that i use the search bar for search the data.But
Im developing one application which used SeekBar to control volume in MediaPlayer. The problem
I am developing a client application that manages one socket. I am using IOCP
I'm developing a C application to communicate with one of the real control system
Iam developing one application.In that iam placing the radio buttons(uiimageview) on table view and
iam developing one application.In that i need to get the music files from the
Iam developing one application.In that iam using one uiimageview.And take another image view and
Iam developing one application.In that i take the string from array and replace .xml

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.