Solved

null pointer? Where/why?

Posted on 2004-04-28
9
482 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 86
Starting to learn JAVA, 7 59
Why method in Java which is called from Runnable run() doesn't need to be 'static'? 1 22
Arrays.asList  VS  ArrayList 4 45
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now