Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently working on an Android application that allows you to watch streaming video in a VideoView. I have a method for allowing you to select one of four streams via a switch statement. That is working correctly and the code for that is as follows:

public void playStream(int position) {
    switch (position) {
        case 0:
            streamOn = true;
            streamPos = 0;
            logString = "M";
            posSelected = "0";
            break;
        case 1:
            streamOn = true;
            streamPos = 1;
            logString = "J";
            posSelected = "1";
            break;
        case 2:
            streamOn = true;
            streamPos = 2;
            logString = "B";
            posSelected = "2";
            break;
        case 3:
            streamOn = true;
            streamPos = 3;
            logString = "N";
            posSelected = "3";
            break;
        default:
            break;
    }
    checkStreamLink(position);

    Log.wtf(logString, posSelected);
    Log.wtf(logString, streamURL);
}

What is not working correctly is that in this method for selecting the stream, I have a call to another method ( checkStreamLink(); ) that runs a thread. Depending on which stream you have selected, the thread will call another method that opens up a webpage, reads a line of text, and then sets that text to a String streamURL. The code for those two methods is as follows:

public void checkStreamLink(final int position) {
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                switch (position) {
                    case 0:
                        streamURL = getStreamLink("LINK 0 GOES HERE");
                        break;
                    case 1:
                        streamURL = getStreamLink("LINK 1 GOES HERE");
                        break;
                    case 2:
                        streamURL = getStreamLink("LINK 2 GOES HERE");
                        break;
                    case 3:
                        streamURL = getStreamLink("LINK 3 GOES HERE");
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}


public String getStreamLink (String textSource) {
    URL streamURL;
    String errorParsingURL = "ERROR PARSING URL";
    try {
        streamURL = new URL(textSource);
        BufferedReader bufferReader = new BufferedReader(new InputStreamReader(streamURL.openStream()));
        String StringBuffer;
        String stringText = "";
        while ((StringBuffer = bufferReader.readLine()) != null) {
            stringText += StringBuffer;
        }
        bufferReader.close();
        return stringText;
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    return errorParsingURL;
}

The issue I'm having is that the String streamURL is returning null on its first use as evidenced by the Log statements I have included. Each time you select a stream after that, the String streamURL returns the text that you should have received the previous time you select a stream. I cannot seem to figure out why this is happening and I would appreciate any assistance.

share|improve this question
    
what is the value of position variable? does it also lag? –  RJadhav yesterday
1  
...do you somehow wait for this thread to finish? because if not there's a very good chance that your logging code runs before the thread. –  radai yesterday
    
@RJadhav, the position variable is just the position in the array for where all the stream names are. It does not lag at all. –  Patr3xion yesterday
    
@radai, I was under the assumption that the thread finished and the code was logged, but playing around with additional log statements is showing me otherwise now. –  Patr3xion yesterday

1 Answer 1

up vote 1 down vote accepted

You are getting a null because getStreamLink is returning its value after you have already printed the result. Print the result log messages at the end of the getStreamLink method to see the actual value which is being returned, and call any additional functionality at that point as well.

share|improve this answer
    
Thank you, Justin Reda. This put me in the right direction. You are a great help. –  Patr3xion yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.