Solved

null pointer? Where/why?

Posted on 2004-04-28
9
486 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

622 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