Swarna Sekhar Dhar
Swarna Sekhar Dhar

Reputation: 548

getElementById is getting null pointer exception

my code is getting null pointer exception at content.html(); as each time Element content is assign to null while i am sure that the page contains the element with that id i am using jsoup to parse Document one can please check the code and catch my mistake

public void get_content(String eliment_by,String identification)
{
    try {

        File currentDirectory = new File(new File(".").getAbsolutePath());
        System.out.println(currentDirectory.getCanonicalPath());
        PrintWriter writer = new PrintWriter(currentDirectory.getCanonicalPath()+"/tmp/input.html", "UTF-8");
        writer.println(" ");
        writer.close();
        File input = new File(currentDirectory.getCanonicalPath()+"/tmp/input.html");
        org.jsoup.nodes.Document doc = Jsoup.parse(input, "UTF-8", this.curr_url);
        Element content = doc.getElementById(identification);

        this.current_page_content=content.html();

    } catch (IOException ex) {
        Logger.getLogger(url_looping.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Upvotes: 0

Views: 853

Answers (1)

Syam S
Syam S

Reputation: 8499

You are opening the file and writing a space character to it and then read this file for parsing. When you use PrintWriter this clears your file. So what happens here is first you clear the file and then trying to parse it. That is why null pointer

if you want to get that from url you could do like

public void get_content(String eliment_by, String identification) {
    try {

        org.jsoup.nodes.Document doc = Jsoup.connect("http://www.opengurukul.com/vlc/mod/page/view.php?id=523").get();
        Element content = doc.getElementById(identification);

        this.current_page_content = content.html();

    } catch (IOException ex) {
        Logger.getLogger(url_looping.class.getName()).log(Level.SEVERE,
                null, ex);
    }
}

Upvotes: 1

Related Questions