Solved

null pointer? Where/why?

Posted on 2004-04-28
9
481 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert ArrayList of custom objects to JSON 4 53
IT Company 5 69
changePi Challenge 15 77
Java asynchronous logging 4 35
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

8 Experts available now in Live!

Get 1:1 Help Now