Solved

null pointer? Where/why?

Posted on 2004-04-28
9
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

759 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