0

I'm trying to get one line of source from a website and then I'm returning that line back to main. I keep on getting an error at the line where I define InputStream in. Why am I getting an error at this line?

public class MP3LinkRetriever
{
    private static String line;

public static void main(String[] args)
{
    String link = "www.google.com";
    String line = "";

    while (link != "")
    {
        link = JOptionPane.showInputDialog("Please enter the link");

        try
        {
            line = Connect(link);
        }
        catch(Exception e)
        {
        }

        JOptionPane.showMessageDialog(null, "MP3 Link: " + parseLine(line));


        String text = line;

        Toolkit.getDefaultToolkit( ).getSystemClipboard()
        .setContents(new StringSelection(text), new ClipboardOwner()
        { 
            public void lostOwnership(Clipboard c, Transferable t) { } 
        });

        JOptionPane.showMessageDialog(null, "Link copied to your clipboard");

    }
}

public static String Connect(String link) throws Exception { 

    String strLine = null; 
    InputStream in = null; 
    try { 
        URL url = new URL(link); 
        HttpURLConnection uc = (HttpURLConnection) url.openConnection(); 

        in = new BufferedInputStream(uc.getInputStream()); 

        Reader re = new InputStreamReader(in); 

        BufferedReader r = new BufferedReader(re); 

        int index = -1; 

        while ((strLine = r.readLine()) != null && index == -1) { 
            index = strLine.indexOf("<source src"); 
        } 

    } finally { 
        try { 
            in.close(); 
        } catch (Exception e) { 
        } 
    } 

    return strLine; 
} 


public static String parseLine(String line)
{
    line = line.replace("<source", "");
    line = line.replace(" src=", "");
    line = line.replace("\"", "");
    line = line.replace("type=", "");
    line = line.replace("audio/mpeg", "");
    line = line.replace(">", "");

    return line;
}

}

durron597
  • 30,764
  • 16
  • 92
  • 150
dsta
  • 233
  • 1
  • 6
  • 15
  • Exception in thread "main" java.lang.NullPointerException at MP3LinkRetriever.parseLine(MP3LinkRetriever.java:54) at MP3LinkRetriever.main(MP3LinkRetriever.java:24) But it happens at the line where I declare InputStream in. – dsta Sep 24 '12 at 00:42
  • With a first glance I can see you use two Buffers, and InputStream combine with Reader,I think this is a mess. – giannis christofakis Sep 24 '12 at 00:42
  • you are not giving enough stack trace. those are useless since it contains nothing related to the code you posted above. – gigadot Sep 24 '12 at 00:43
  • @giannosfor using multiple buffers is not the problem. it is just inefficient. the problem is that the input stream is never get closed. – gigadot Sep 24 '12 at 00:44
  • It's a `NullPointerException` at `index = strLine.indexOf(" – MadProgrammer Sep 24 '12 at 00:45
  • I tried what MadProgrammer said, and I do not get an error anymore, but the code in the first try method never gets executed. It just jumps back to main after I define InputStream. – dsta Sep 24 '12 at 01:31
  • @dsta Don't consume you're exceptions. I got a `java.net.MalformedURLException: no protocol` (my bad) and a `Exception in thread "main" java.lang.NullPointerException` (your bad) – MadProgrammer Sep 24 '12 at 01:54
  • exceptions? Sorry, I'm a beginner. – dsta Sep 24 '12 at 01:56
  • if i were you, i would use Jsoup library and css selector to solve this problem. Then i can forget about trying to read in the input stream. – gigadot Sep 24 '12 at 01:58

2 Answers2

3

strLine = r.readLine(); will return null when it reaches the end of the content, but you don't make a check for the possibility for null AFTER you've read the line and BEFORE you use it in your code...

Try something like...

while ((strLine = r.readLine()) != null && index == -1) {
    index = strLine.indexOf("<source src");
}

Also, make sure you are closing your steams before you leave the method...

InputStream in = null;
try {
    // The rest of your code...
} finally {
    try {
        in.close();
    } catch (Exception exp) { // We don't really care about this one..
    }
}

UPDATED - Full Example

This is the test code I was using and works fine...

public class TestWebDownload {

    public static void main(String[] args) {
        try {
            Connect("http://www.google.com.au");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static String Connect(String link) throws Exception {

        String strLine = null;
        InputStream in = null;
        try {
            URL url = new URL(link);
            HttpURLConnection uc = (HttpURLConnection) url.openConnection();

            in = new BufferedInputStream(uc.getInputStream());

            Reader re = new InputStreamReader(in);

            BufferedReader r = new BufferedReader(re);

            int index = -1;

            while ((strLine = r.readLine()) != null && index == -1) {
                index = strLine.indexOf("<source src");
            }

        } finally {
            try {
                in.close();
            } catch (Exception e) {
            }
        }

        return strLine;
    }
}

UPDATE

Your new problem is similar to your old problem, you fail to anticipate that the information been passed to you may not be correct or valid...

public static String parseLine(String line)
{

    // What if line is null ??
    line = line.replace("<source", "");
    line = line.replace(" src=", "");
    line = line.replace("\"", "");
    line = line.replace("type=", "");
    line = line.replace("audio/mpeg", "");
    line = line.replace(">", "");

    return line;
}
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
  • This did not work for me. I don't get an error anymore, but it stops when I declare InputStream. I'll paste the new code I have – dsta Sep 24 '12 at 01:27
  • It still doesn't scroll through the code for me. It stops at "in = new BufferedInputStream(us.getInputStream())"; – dsta Sep 24 '12 at 01:38
  • Could be a network issue and/or the `BufferedInputStream` can't determine the end of the stream for some reason. What's the link you're trying do download? – MadProgrammer Sep 24 '12 at 01:40
  • I'm testing this link: http://digitaldripped.com/sep/13/2012/kanye-west-ft-big-sean-2-chainz--marsha – dsta Sep 24 '12 at 01:41
  • Works fine for me. I think the issue you "might" be having is the time it takes for the buffered reader to buffer it's contents. – MadProgrammer Sep 24 '12 at 01:43
  • I've updated the answer with an unbuffered example, try using that and see if it makes a difference... – MadProgrammer Sep 24 '12 at 01:49
  • Can you please just take a quick look at the code I just posted and see if you can find something that I'm doing wrong? I really appreciate it, thank you. – dsta Sep 24 '12 at 01:49
  • You're not checking the validity of the the parameter `line` in the `pareseLine` method. What if it is null? What if it doesn't contain `src`??? – MadProgrammer Sep 24 '12 at 01:56
  • oh ok so do I have it throw an exception? – dsta Sep 24 '12 at 01:58
  • In your code, where it reads `line = Connect(link);` you've wrapped the call in an exception handler, but you consumed the event (failed to display it or handle it in any meaningful manner), hence the reason you're blaming the code. The parse line method should check for a `null` value, as to what you do about it is up to you...I'd imagine passing it back as `null` and allowing the caller to determine the best course of action might be more prudent for you... – MadProgrammer Sep 24 '12 at 02:02
  • I've been printing the exceptions and I keep on getting: Invalid argument: connect. – dsta Sep 24 '12 at 02:20
  • Apart from some logic problems, I have no trouble running the code you supplied. You might want to have a look at http://stackoverflow.com/questions/8216713/java-net-socketexception-invalid-argument-connect and see that helps – MadProgrammer Sep 24 '12 at 02:34
  • Funny thing about that, I have an Alienware M13x. I'm gonna try running this code on my Mac. Thanks so much for your help! – dsta Sep 24 '12 at 22:46
0

UPDATE

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class Example {    
    public static void main(String[] args) {
        String strLine = "Line never found";
        try (BufferedReader in = new BufferedReader(new InputStreamReader(new URL("page").openStream()))){
            while ((strLine = in.readLine()) != null) {
                if (strLine.indexOf("<source src") > 0) {
                    break;
                }
            }
            in.close();
        } catch (MalformedURLException e) {
            System.out.println("MalformedURL catched");
        } catch (IOException e) {
            System.out.println("InputOutputException catched");
        } 
    }
}
giannis christofakis
  • 7,151
  • 4
  • 48
  • 63
  • 3
    the input stream should be closed in `finally` block. otherwise if there is an error while reading the stream, it will never be clsed. – gigadot Sep 24 '12 at 00:47
  • @gigadot The think is if an exception appears before the input stream is been initialized then you cannot close it,so inside the`finally` block you will have an error. – giannis christofakis Sep 24 '12 at 00:56
  • Wrap the close statement in an exception. You don't care if it's initalised or not, just that you've made a best effort to close it...`finally { try { is.close() } catch (Exception exp) {} }` or check to see if the input stream is null or not before you attempt to close it. It's up to you. gigadot is correct – MadProgrammer Sep 24 '12 at 01:35
  • you don't need to move the buffer reader all the way out of the method. it is best to keep it local to the method since it will be short-live object and get garbage collected quicker. However, it probably makes no difference for a small main method. – gigadot Sep 24 '12 at 01:42
  • @giannosfor Picky ain't we ;) – MadProgrammer Sep 24 '12 at 01:50