Results 1 to 5 of 5
  1. #1
    chocolade is offline Member
    Join Date
    Aug 2015
    Posts
    3
    Rep Power
    0

    Default Why when using a break my program crash ?

    What i want to do is to loop over ipaddresses which is array of ip's.
    The method Get send a command to my web server on my pc and return a string to my client.
    The client is my android smartphone device connected to my pc usb.

    In other place in my program it's working good i can send commands and get back in return strings.

    But in this place in my code when i click the button when it's getting to the break; my program crash on the android.
    Without the break the program will keep working but i wanted to use a break there since if i found the correct ip of my pc web server then i want to stop the loop and use this ip.
    The question is why the program crash on the break ?

    This is in my MainActivity.java in the onCreate:

    Java Code:
    @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ipaddresses[0] = "http://10.0.0.2:8098/?cmd=nothing";
            ipaddresses[1] = "http://10.0.0.5:8088/?cmd=nothing";
            addListenerOnButton();
        }
    This is for testing i know now that the ip 10.0.0.2 is the correct one the second ip i added it to the array just for the testing.

    This is the code of the addListenerOnButton it's a click button event:

    Java Code:
    public void addListenerOnButton()
        {
            btnClick = (Button) findViewById(R.id.checkipbutton);
            btnClick.setOnClickListener(new OnClickListener()
            {
                byte[] response = null;
                @Override
                public void onClick(View arg0)
                {
                    text = (TextView) findViewById(R.id.textView2);
                    Thread t = new Thread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            for (int i = 0; i < ipaddresses.length; i++)
                            {
                                    response = Get(ipaddresses[i]);
                                    if (response!=null)
                                    {
                                        String a = null;
                                        try
                                        {
                                            a = new String(response,"UTF-8");
                                            text.setText(a);
                                        } catch (UnsupportedEncodingException e)
                                        {
                                            e.printStackTrace();
                                        }
                                        Logger.getLogger("MainActivity(inside thread)").info(a);
                                        break;
                                    }
                                    else
                                    {
                                        text.setText("Connection Failed");
                                    }
    
                            }
    
                        }
                    });
                    t.start();
                }
            });
    
        }
    I'm not sure about the logic of this FOR loop in the button event.
    But i used a break point and i know that when it's getting to the correct ip the server return a string.
    And in the variable a i see: "Connection Success"

    But once it's getting to the break; the program crash. And i don't want the loop to continue i want that when it find the ip and get response so if response is not null then stop the loop.

    And last this is the Get method i'm using to send command and get response back from the web server on my pc.
    In another place in my program i'm using it and it's working fine. But here in the button code i'm using a loop and a break and thats not working.

    Java Code:
    private byte[] Get(String urlIn)
        {
            URL url = null;
            String urlStr = urlIn;
            if (urlIn!=null)
                urlStr=urlIn;
    
            try
            {
                url = new URL(urlStr);
            } catch (MalformedURLException e)
            {
                e.printStackTrace();
                return null;
            }
            HttpURLConnection urlConnection = null;
            try
            {
    
                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    
                byte[] buf=new byte[10*1024];
                int szRead = in.read(buf);
                byte[] bufOut;
    
                if (szRead==10*1024)
                {
                    throw new AndroidRuntimeException("the returned data is bigger than 10*1024.. we don't handle it..");
                }
                else
                {
                    bufOut = Arrays.copyOf(buf, szRead);
                }
                return bufOut;
            }
            catch (IOException e)
            {
                e.printStackTrace();
                return null;
            }
            finally
            {
                if (urlConnection!=null)
                    urlConnection.disconnect();
            }
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Why when using a break my program crash ?

    What error are you getting?
    There'll be a stack trace somewhere in the logs if it's crashed.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    chocolade is offline Member
    Join Date
    Aug 2015
    Posts
    3
    Rep Power
    0

    Default Re: Why when using a break my program crash ?

    Not sure how to get the stack trace in the logcat i see the text scrolling non stop i tried in the filter to type: exception but it didn't help.

    The error i get is on the smartphone a message: Unfortunately, MyApp has stopped. and when i click ok thats it the program close.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Why when using a break my program crash ?

    You'll have to debug it then.
    Without knowing where it is falling over it will be a needle-in-a-haystack task to spot the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Why when using a break my program crash ?

    Save the logcat to a file that you can then edit and find where the error happens.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Nested For loop causing my program to crash.
    By Deathslice in forum New To Java
    Replies: 13
    Last Post: 01-23-2015, 08:22 PM
  2. While loop makes program crash
    By atiyarose17 in forum New To Java
    Replies: 5
    Last Post: 10-10-2012, 05:53 AM
  3. Re-writing program with break/continue
    By rich123 in forum New To Java
    Replies: 10
    Last Post: 02-01-2011, 01:19 AM
  4. Replies: 10
    Last Post: 01-07-2011, 10:28 AM
  5. Help Making a program not crash
    By Lifeis2evil in forum New To Java
    Replies: 2
    Last Post: 12-10-2008, 03:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •