Solved

null pointer? Where/why?

Posted on 2004-04-28
9
484 Views
Last Modified: 2012-05-04
I am trying out an html document reader, but am getting a null pointer exception for every page I try.  I don't know why, where or how I am getting it.  the following is my code.

public void readPage(String page) {
        JEditorPane jep = new JEditorPane();
        System.out.println("Created a pane");
        jep.setEditable(false);
        System.out.println("set editable to false");
        EditorKit htmlKit = jep.getEditorKitForContentType("text/html");
        System.out.println("created editor kit with " +htmlKit.toString());
        HTMLDocument doc = (HTMLDocument) htmlKit.createDefaultDocument();
        System.out.println("created document with "+ doc.toString());
        jep.setEditorKit(htmlKit);
        System.out.println("set the editor kit");
        try {
            URL u = new URL(page);
            System.out.println("created url with "+u.toString());
            InputStream in = u.openStream();
            System.out.println("opened the stream with "+in.toString());
            jep.read(in, doc);
            System.out.println("read the steam");
        }
        catch (IOException e) {
            System.err.println("something bad happened " +e);
        }
        JScrollPane scrollPane = new JScrollPane(jep);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        f.getContentPane().add(scrollPane);
        f.setSize(512, 342);
        f.show();
       
    }
0
Comment
Question by:tigress298
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10940175
What are you passing in as 'page'? It should be a url, e.g. "file:something.html"
0
 

Author Comment

by:tigress298
ID: 10940307
String initialPage = "http://metalab.unc.edu/javafaq/";
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10940381
Can you change this

>>

        catch (IOException e) {
            System.err.println("something bad happened " +e);
        }

>>

to


        catch (Exception e) {
            e.printStackTrace();
        }


?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:tigress298
ID: 10940446
Created a pane
set editable to false
created editor kit with javax.swing.text.html.HTMLEditorKit@1ebd75b
created document with javax.swing.text.html.HTMLDocument@198cb3d
set the editor kit
created url with http://metalab.unc.edu/javafaq/
opened the stream with sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1576e70
java.lang.NullPointerException
        at javax.swing.JEditorPane.read(JEditorPane.java:516)
        at javax.swing.JEditorPane.read(JEditorPane.java:478)
        at Tests.PageDisplay.readPage(PageDisplay.java:56)
        at Tests.PageDisplay.main(PageDisplay.java:37)
Exception in thread "main" 

is the error I get either way
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 10940527
Instead of

>>jep.read(in, doc);

try

htmlKit.read(in, doc);
0
 

Author Comment

by:tigress298
ID: 10940738
Created a pane
set editable to false
created editor kit with javax.swing.text.html.HTMLEditorKit@110fe28
created document with javax.swing.text.html.HTMLDocument@2bc3f5
set the editor kit
created url with http://metalab.unc.edu/javafaq/
opened the stream with sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@d3db51
javax.swing.text.ChangedCharSetException
        at javax.swing.text.html.parser.DocumentParser.handleEmptyTag(DocumentParser.java:169)
        at javax.swing.text.html.parser.Parser.startTag(Parser.java:372)
        at javax.swing.text.html.parser.Parser.parseTag(Parser.java:1846)
        at javax.swing.text.html.parser.Parser.parseContent(Parser.java:1881)
        at javax.swing.text.html.parser.Parser.parse(Parser.java:2047)
        at javax.swing.text.html.parser.DocumentParser.parse(DocumentParser.java:106)
        at javax.swing.text.html.parser.ParserDelegator.parse(ParserDelegator.java:78)
        at javax.swing.text.html.HTMLEditorKit.read(HTMLEditorKit.java:230)
        at javax.swing.text.DefaultEditorKit.read(DefaultEditorKit.java:132)
        at Tests.PageDisplay.readPage(PageDisplay.java:57)
        at Tests.PageDisplay.main(PageDisplay.java:37)

is now the error
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10940871
Yep. That fixed the first one. The second one (and if my memory doesn't fail me, in one of your previous questions i mentioned that Java's html parsing libraries are flaky) is due to the parser having fallen over on parsing the file. In my experience it usually does unless things that are not pretty simple and well-formed
0
 

Author Comment

by:tigress298
ID: 10940980
Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10941027
8-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question