1 2 Previous Next 22 Replies Latest reply on Nov 26, 2010 5:32 PM by rfranzen Go to original post
      • 15. Re: java.lang.reflect.InvocationTargetException
        rfranzen
        Wow, Darryl, you are quick! You posted while I was busy fixing/testing. I didn't realize you had posted when I posted my update.

        Edited by: RichF on Nov 21, 2010 8:14 AM
        • 16. Re: java.lang.reflect.InvocationTargetException
          rfranzen
          You just can't knowingly ignore a bug
          And as soon as you kill one, another pops up!

          I'm working on activating Help from an HTML file, and I thought I had it:

          * http://r0k.us/rock/Junk/moreJunk/SIHwheel.html

          It seems to work fine. Probably too fine -- the menu allows you to have multiple help windows at once. Not really a problem. What is a problem is apparently more stale JVM data. If you leave that page and come back before the JVM has expired, Help no longer functions. The window pops up, but it is completely empty. The Java Console reports:
          Exception in thread "Thread-17" java.lang.NullPointerException
               at javax.swing.text.html.parser.Parser.errorContext(Unknown Source)
               at javax.swing.text.html.parser.Parser.parse(Unknown Source)
               at javax.swing.text.html.parser.DocumentParser.parse(Unknown Source)
               at javax.swing.text.html.parser.ParserDelegator.parse(Unknown Source)
               at javax.swing.text.html.HTMLEditorKit.read(Unknown Source)
               at javax.swing.JEditorPane.read(Unknown Source)
               at javax.swing.JEditorPane$PageLoader.run(Unknown Source)
          My menu constructor code for help is:
               JMenu help = new JMenu("Help");
               JMenuItem sihHelp = new JMenuItem("SIHwheel Help");
               sihHelp.addActionListener( new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                    try {
                        URL url = getClass().getResource("SIHhelp.html");
                        JFrame frame = new JFrame("SIHwheel Help");
                        JEditorPane ep = new JEditorPane();
                        ep.setEditable(false);
                        ep.setPage(url);
                        JScrollPane eps = new JScrollPane(ep);
                        eps.setPreferredSize(new Dimension(880, 600));
                        eps.setMinimumSize(new Dimension(220, 165));
                        frame.getContentPane().add(eps, BorderLayout.CENTER);
                        frame.pack();
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);
                    } catch (IOException ioE) {
                        System.err.println("Unable to display help pane");
                        ioE.printStackTrace();
                    }
                   }
               });
               help.add(sihHelp);
          On another note, I have tentatively chosen to include within the .jar file both SIHhelp.html and the image it displays. The .html file is only 3k, but the PNG image is 52k, increasing the jar file size from 80k to 130k in size. On the one hand, that image has been loading as part of the outer page anyway. On the other, it takes noticeably longer for the applet to start running. Should I move the image, and possibly the help file, outside of the .jar?
          • 17. Re: java.lang.reflect.InvocationTargetException
            mKorbel
            @ Rich

            please,

            replace your JFrame with JDialog, probably you have/could problem(s) move your frame toFront at Runtime, I can't found that now, but Darryl (un???)solved this issue with/for kevinWorkman???

            create your (now JFrame) JDialog plus JEditoPane outSide of try - catch blok

            if you want to display url contens, then pack this into invokeLater();
            1 person found this helpful
            • 18. Re: java.lang.reflect.InvocationTargetException
              rfranzen
              mKorbel, thank you. Your suggestions have improved my code, but they did not fix the problem. The new code is:
                   JMenu help = new JMenu("Help");
                   JMenuItem sihHelp = new JMenuItem("SIHwheel Help");
                   sihHelp.addActionListener( new ActionListener() {
                       public void actionPerformed(ActionEvent e) {
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                             JDialog jd = new JDialog((Dialog)null, "SIHwheel Help", false);
                             JEditorPane ep = new JEditorPane();
                             ep.setEditable(false);
                             try {
                                 URL url = getClass().getResource("SIHhelp.html");
                                 ep.setPage(url);
                                 JScrollPane eps = new JScrollPane(ep);
                                 eps.setPreferredSize(new Dimension(880, 600));
                                 jd.getContentPane().add(eps, BorderLayout.CENTER);
                             } catch (IOException ioE) {
                                 System.err.println("Unable to display help pane");
                                 ioE.printStackTrace();
                             }
                             jd.pack();
                             jd.setLocationRelativeTo(null);
                             jd.setVisible(true);
                            }
                        });
                       }
                   });
                   help.add(sihHelp);
              The help dialog (not frame :) ) still appears reliably the first time the page is accessed. However, if one leaves the page and comes back to it before the JVM is dead, then the dialog is blank. The error messages in the Java Console are still the same as I reported last post.

              Once working, I would like to make most of that a method. However, I cannot find out how to pass parameters to a runnable doohickey. I'd like to pass String for title bar, String of the URL, boolean modality, and Dimension for preferred width and height.
              • 19. Re: java.lang.reflect.InvocationTargetException
                mKorbel
                @ Rich

                please check your htlm Parser, with this code, maybe you missed some chars (this code is against all Nature Rules and Java Good Programing Comceptios too)
                 
                import java.awt.*;
                import java.awt.event.ActionEvent;
                import java.io.*;
                import javax.swing.*;
                import javax.swing.text.*;
                import javax.swing.text.html.*;
                import javax.swing.text.html.parser.ParserDelegator;
                
                public class LoadSync {
                
                    public static void main(String args[]) {
                        final String filename = "C:\\0500__HTML-Parser.htm";
                        JFrame frame = new JFrame("Loading/Saving Example");
                        Container content = frame.getContentPane();
                        final JEditorPane editorPane = new JEditorPane();
                        editorPane.setEditable(false);
                        JScrollPane scrollPane = new JScrollPane(editorPane);
                        content.add(scrollPane, BorderLayout.CENTER);
                        editorPane.setEditorKit(new HTMLEditorKit());
                        JPanel panel = new JPanel();
                        Action loadAction = new AbstractAction() { // Setup actions
                
                            {
                
                                putValue(Action.NAME, "Load");
                            }
                            private static final long serialVersionUID = 1L;
                
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                doLoadCommand(editorPane, filename);
                            }
                        };
                        JButton loadButton = new JButton(loadAction);
                        panel.add(loadButton);
                        content.add(panel, BorderLayout.SOUTH);
                        frame.setSize(250, 150);
                        frame.setVisible(true);
                    }
                
                    public static void doLoadCommand(JTextComponent textComponent, String filename) {
                        FileReader reader = null;
                        try {
                            System.out.println("Loading");
                            reader = new FileReader(filename);
                            HTMLEditorKit htmlKit = new HTMLEditorKit(); // Create empty HTMLDocument to read into
                            HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
                            HTMLEditorKit.Parser parser = new ParserDelegator(); // Create parser (javax.swing.text.html.parser.ParserDelegator)           
                            HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0); // Get parser callback from document          
                            parser.parse(reader, callback, true);  // Load it (true means to ignore character set)           
                            textComponent.setDocument(htmlDoc); // Replace document
                            System.out.println("Loaded");
                        } catch (IOException exception) {
                            System.out.println("Load oops");
                            exception.printStackTrace();
                        } finally {
                            if (reader != null) {
                                try {
                                    reader.close();
                                } catch (IOException ignoredException) {
                                }
                            }
                        }
                    }
                
                    private LoadSync() {
                    }
                }
                your JVM died because you forgot to set setDefaultCloseOperation for JDialog, by JMenuItem click you always create a new JDialog with ....., :-)

                maybe revalidate() + repainit(); can solve your blank JDialog contens (created container on RunTime)
                • 20. Re: java.lang.reflect.InvocationTargetException
                  rfranzen
                  Oh, heck! :( The problem is worse than I thought -- it seems to affect in-line HTML as well as showing an HTML page.
                  please check your htlm Parser, with this code
                  mKorbel, I have verified my [url http://r0k.us/rock/Junk/SIHhelp.html]HTML help page at http://validator.w3.org/ . It's fine. The one modification I had to make, from almost the same info on my primary page, is put the image in a separate table cell; Java HTML doesn't know how to flow lines of text to the left of an image which has been declared "align=right". (I had done that days ago; the validator likes either form.)

                  My latest test version is at: http://r0k.us/rock/Junk/moreJunk/SIHwheel.html

                  First, I'll talk about the in-line HTML problem. I didn't notice this before, but it is even affecting the live version of my applet. My help menu has 3 items, the HTML page in a JDialog, a "Show Log" stub in a showMessageDialog, and an "About" page also in a showMessageDialog. Only the "Show Log" will redisplay properly when the user leaves the page and comes back before the browser tab's JVM dies. It is also the only one of the 3 which has no HTML. Here is the code for the two message dialogs:
                       JMenuItem log = new JMenuItem("Show Log");
                       log.addActionListener( new ActionListener() {
                           public void actionPerformed(ActionEvent e) {
                            JOptionPane.showMessageDialog(null,
                                "Log not yet implemented.\n" +
                                "For now, use the Java Console.",
                                "Log", JOptionPane.PLAIN_MESSAGE);
                           }
                       });
                       help.add(log);
                       help.addSeparator();
                       JMenuItem about = new JMenuItem("About");
                       about.addActionListener( new ActionListener() {
                           public void actionPerformed(ActionEvent e) {
                            JOptionPane.showMessageDialog(null,
                                "<html><div align=center>" +
                                "<b>Interactive Color Wheel</b><br>" +
                                "v2.43, 2010-11-21<br>" +
                                "by Rich Franzen, ©1998-2010<br>" +
                                "<br>" +
                                "Experiment with all aspects of color,<br>" +
                                "and see names for a multitude of colors.<br>" +
                                "<br>" +
                                "released under the GNU General Public License<br>" +
                                "master URI: href=http://r0k.us/graphics/SIHwheel.html" +
                                "</div></html>",
                                "About", JOptionPane.PLAIN_MESSAGE);
                           }
                       });
                       help.add(about);
                  Note that the difference between the two menu items is that the second one has HTML and the first does not. (Actually I just added the last two parameters to the first ("Log", JOptionPane.PLAIN_MESSAGE) so that they would be the same. They probably won't be there next time I update the .jar.) The Log repeatedly displays properly on multiple sessions of the same JVM. The About, however, is blank on repeated JVM sessions once it has been shown properly the first time. No error is displayed in the Java Console.

                  Ok, back to my attempts to reliably display the HTML page on repeated JVM sessions. In order to both troubleshoot and solve the problem of passing parameters to a runnable doohickey, I wrote a new class. Here is the current attempt, with the menu stuff immediately preceding the code segment above:
                       JMenu help = new JMenu("Help");
                       JMenuItem sihHelp = new JMenuItem("SIHwheel Help");
                       sihHelp.addActionListener( new ActionListener() {
                           public void actionPerformed(ActionEvent e) {
                            Dimension size = new Dimension(880, 600);
                            HelpBox hb = new HelpBox("SIHwheel Help",
                                            "SIHhelp.html",
                                            false, size);
                           }
                       });
                       help.add(sihHelp);
                          ...
                      }  // end of pane constructor
                      ...
                  }  // end of class
                  
                  class HelpBox
                  {
                      private JDialog     jd;
                  
                      HelpBox(String pTitle, String pUrlS, boolean pModal, Dimension pSize)
                      {
                       final String     title = pTitle;
                       final String     urlS  = pUrlS;
                       final Dimension     size  = pSize;
                       final boolean     modal = pModal;
                  
                       SwingUtilities.invokeLater(new Runnable() {
                           public void run() {
                            jd = new JDialog((Frame)null, title, modal);
                            jd.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                            JEditorPane ep = new JEditorPane();
                            ep.setEditable(false);
                            try {
                                URL url = getClass().getResource(urlS);
                                ep.setPage(url);
                                JScrollPane eps = new JScrollPane(ep);
                                eps.setPreferredSize(size);
                                jd.getContentPane().add(eps, BorderLayout.CENTER);
                            } catch (IOException ioE) {
                                System.err.println("Unable to display help pane");
                                ioE.printStackTrace();
                            }
                            jd.pack();
                            jd.setLocationRelativeTo(null);
                            jd.setVisible(true);
                           }
                       });
                      }
                  }
                  Unfortunately, nothing changed. On displaying help, leaving page, coming back while JVM is still alive, and attempting to display help again, the help window is blank. Supposedly a brand-new Help object is created each time, with nothing declared static. The Java Console reports the same errors it has been:
                  Exception in thread "Thread-18" java.lang.NullPointerException
                       at javax.swing.text.html.parser.Parser.errorContext(Unknown Source)
                       at javax.swing.text.html.parser.Parser.parse(Unknown Source)
                       at javax.swing.text.html.parser.DocumentParser.parse(Unknown Source)
                       at javax.swing.text.html.parser.ParserDelegator.parse(Unknown Source)
                       at javax.swing.text.html.HTMLEditorKit.read(Unknown Source)
                       at javax.swing.JEditorPane.read(Unknown Source)
                       at javax.swing.JEditorPane$PageLoader.run(Unknown Source)
                  Taking both problems into account, I am wondering if the JVM itself is bugged on handling HTML over multiple sessions of the same JVM. If not, what else can I try?
                  • 21. Re: java.lang.reflect.InvocationTargetException
                    rfranzen
                    Taking both problems into account, I am wondering if the JVM itself is bugged on handling HTML over multiple sessions of the same JVM. If not, what else can I try?
                    Ooh! -- Found a related thread and a recent bug:

                    * Re: 1.6.0_22 HTMLEditorKit throws NullPointerException when reloaded
                    * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6993691

                    The thread mentions a workaround, and I'll be trying that in a few minutes. Even if it works, it won't help the problem with in-line HTML. But one step at a time...
                    • 22. Re: java.lang.reflect.InvocationTargetException
                      rfranzen
                      The thread mentions a workaround, and I'll be trying that in a few minutes. Even if it works, it won't help the problem with in-line HTML. But one step at a time...
                      The workaround works! Here's a code segment from the HelpBox class defined above:
                                jd = new JDialog((Frame)null, title, modal);
                                jd.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                                ParserDelegator workaround = new ParserDelegator();  // kludge for 1.6.0_22 bug
                                JEditorPane ep = new JEditorPane();
                                ep.setEditable(false);
                      The workaround is to instantiate a ParserDelegator. You don't need to do anything with it, just make it exist.

                      And, amazingly, it does fix the in-line HTML problem. Amazing, because the ParserDelegator is instantiated in one class, and my in-line HTML is in a different class.

                      I wonder if I should move the workaround line somewhere else. I probably will. If it is some sort of applet-global thing, it probably belongs in the top-level class, right?

                      Edited by: RichF on Nov 26, 2010 12:19 PM
                      I moved workaround to within main class, and everything still works. :)
                      public class SIHwheel extends JApplet
                      {
                          public static SIHControls controls;
                          public static SIHListPane colorList;
                          public static SIHCanvas paintingCanvas;
                      
                          // kludge for 1.6.0_22 HTML-in-reused-JVM bug; just make it exist
                          private static ParserDelegator workaround;
                      
                          // Static initialization of the static NTC data is necessary.
                          // Fallback to manual init() if cannot read color name dictionary.
                          static { if (!NTC.readCND(null))  NTC.init(); }
                          ...
                          public JPanel makeContent()
                          {   ...
                           paintingCanvas = new SIHCanvas(640, 640);
                           controls = new SIHControls();
                           colorList = new SIHListPane();
                           workaround = new ParserDelegator();
                              ...
                          }
                          ...
                      }
                      Stylistically, do you-all agree this is a better way to handle it?
                      1 2 Previous Next