NullPointerException in Applet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teenIce
    New Member
    • Aug 2007
    • 36

    NullPointerException in Applet

    Hi all,

    Thanks in advance. I am new at Java. Currently I'm having two java files one is applet. when i try to preview the applet i get an error like this :

    java.lang.NullP ointerException
    at java.util.Hasht able.put(Unknow n Source)
    at javax.swing.JEd itorPane.regist erEditorKitForC ontentType(Unkn own Source)
    at javax.swing.JEd itorPane.regist erEditorKitForC ontentType(Unkn own Source)
    at javax.swing.JEd itorPane.loadDe faultKitsIfNece ssary(Unknown Source)
    at javax.swing.JEd itorPane.getKit TypeRegistry(Un known Source)
    at javax.swing.JEd itorPane.getEdi torKitClassName ForContentType( Unknown Source)
    at javax.swing.JTe xtPane.<init>(U nknown Source)

    what i'm doing wrong?pls help me guys...thanks
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Can you show a bit of (relevant) code please? btw, I changed your topic titile
    to something more to the point.

    kind regards,

    Jos

    Comment

    • nickyeng
      Contributor
      • Nov 2006
      • 252

      #3
      because you pointed to hashtable that doesn't have the key ?

      check your code that use that hashtable ...

      Comment

      • teenIce
        New Member
        • Aug 2007
        • 36

        #4
        Hi all,

        Thanks Josah. Erm, what do you mean by hashtable nick?I don't understand..Sor ry.btw this is my code that call JTextPane

        code :

        private JTextArea textArea = null;
        private JTextPane resultArea = null;
        private JComboBox langBox = null;

        private Map<Language, ConfigurationDi alog> configDialogs = new HashMap<Languag e, ConfigurationDi alog>();
        public void createAndShowGU I(String inputText) {
        JFrame frame = new JFrame("Test");
        frame.setDefaul tCloseOperation (JFrame.HIDE_ON _CLOSE);

        textArea = new JTextArea(input Text);
        // TODO: wrong line number is displayed for lines that are wrapped automatically:
        textArea.setLin eWrap(true);
        textArea.setWra pStyleWord(true );
        resultArea = new JTextPane();
        resultArea.setC ontentType("tex t/html");
        resultArea.setT ext("Results will appear here");
        JLabel label = new JLabel("Please type or paste text to check in the top area");
        JButton button = new JButton("Check text");
        button.setMnemo nic('c');
        button.addActio nListener(this) ;

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by teenIce
          Hi all,

          Thanks Josah. Erm, what do you mean by hashtable nick?I don't understand..Sor ry.btw this is my code that call JTextPane

          code :

          private JTextArea textArea = null;
          private JTextPane resultArea = null;
          private JComboBox langBox = null;

          private Map<Language, ConfigurationDi alog> configDialogs = new HashMap<Languag e, ConfigurationDi alog>();
          public void createAndShowGU I(String inputText) {
          JFrame frame = new JFrame("Test");
          frame.setDefaul tCloseOperation (JFrame.HIDE_ON _CLOSE);

          textArea = new JTextArea(input Text);
          // TODO: wrong line number is displayed for lines that are wrapped automatically:
          textArea.setLin eWrap(true);
          textArea.setWra pStyleWord(true );
          resultArea = new JTextPane();
          resultArea.setC ontentType("tex t/html");
          resultArea.setT ext("Results will appear here");
          JLabel label = new JLabel("Please type or paste text to check in the top area");
          JButton button = new JButton("Check text");
          button.setMnemo nic('c');
          button.addActio nListener(this) ;
          1.) Please use code tags when posting code.
          2.) If you look at that stacktrace again, you'll see that it tells you the exact line number where that exception was thrown. That line and the code lines around it are the relevant code that Jos asked you to post.

          Comment

          • teenIce
            New Member
            • Aug 2007
            • 36

            #6
            oh...thanks guys,

            ok...here is the code that is relevant to the trace and the full trace

            trace :

            Code:
            Java Plug-in 1.6.0_02
            Using JRE version 1.6.0_02 Java HotSpot(TM) Client VM
            User home directory = C:\Documents and Settings\Administrator
            network: Loading user-defined proxy configuration ...
            network: Done.
            network: Loading proxy configuration from Internet Explorer ...
            network: Done.
            network: Loading direct proxy configuration ...
            network: Done.
            network: Proxy Configuration: No proxy
            
            
            ----------------------------------------------------
            c:   clear console window
            f:   finalize objects on finalization queue
            g:   garbage collect
            h:   display this help message
            l:   dump classloader list
            m:   print memory usage
            o:   trigger logging
            p:   reload proxy configuration
            q:   hide console
            r:   reload policy configuration
            s:   dump system and deployment properties
            t:   dump thread list
            v:   dump thread stack
            x:   clear classloader cache
            0-5: set trace level to <n>
            ----------------------------------------------------
            
            liveconnect: Invoking JS method: document
            liveconnect: Invoking JS method: URL
            basic: Referencing classloader: sun.plugin.ClassLoaderInfo@12498b5, refcount=1
            basic: Added progress listener: sun.plugin.util.GrayBoxPainter@f5da06
            basic: Loading applet ...
            basic: Initializing applet ...
            basic: Starting applet ...
            basic: completed perf rollup
            liveconnect: Invoking method: public void test.check(java.lang.String)
            liveconnect: Needs conversion: java.lang.String --> java.lang.String
            java.lang.NullPointerException
            	at java.util.Hashtable.put(Unknown Source)
            	at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source)
            	at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source)
            	at javax.swing.JEditorPane.loadDefaultKitsIfNecessary(Unknown Source)
            	at javax.swing.JEditorPane.getKitTypeRegistry(Unknown Source)
            	at javax.swing.JEditorPane.getEditorKitClassNameForContentType(Unknown Source)
            	at javax.swing.JTextPane.<init>(Unknown Source)
            	at mypack.Main.createAndShowGUI(main.java:84)
            	at mypack.Main.openGui(main.java:236)
            	at test.check(test.java:27)
            	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            	at java.lang.reflect.Method.invoke(Unknown Source)
            	at sun.plugin.javascript.JSInvoke.invoke(Unknown Source)
            	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            	at java.lang.reflect.Method.invoke(Unknown Source)
            	at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
            	at sun.plugin.com.MethodDispatcher.invoke(Unknown Source)
            	at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)
            	at sun.plugin.com.DispatchImpl$1.run(Unknown Source)
            	at java.security.AccessController.doPrivileged(Native Method)
            	at sun.plugin.com.DispatchImpl.invoke(Unknown Source)
            java.lang.Exception: java.lang.NullPointerException
            	at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)
            	at sun.plugin.com.DispatchImpl$1.run(Unknown Source)
            	at java.security.AccessController.doPrivileged(Native Method)
            	at sun.plugin.com.DispatchImpl.invoke(Unknown Source)

            and the relevant code is :

            Code:
            private JTextArea textArea = null;
              private JTextPane resultArea = null;
              private JComboBox langBox = null;
             
              private Map<Language, ConfigurationDialog> configDialogs = new HashMap<Language, ConfigurationDialog>();
            
               public Main() {
              }
            
              public void createAndShowGUI(String inputText) {
                JFrame frame = new JFrame("Grammar Check Tool");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
                textArea = new JTextArea(inputText);
                // TODO: wrong line number is displayed for lines that are wrapped automatically:
                textArea.setLineWrap(true);
                textArea.setWrapStyleWord(true);
             	JTextPane resultArea = new JTextPane();
                resultArea.setContentType("text/html");
                resultArea.setText("Results will appear here");   
            	JLabel label = new JLabel("Please type or paste text to check in the top area");
                JButton button = new JButton("Check text");
                button.setMnemonic('c'); 
                button.addActionListener(this);
            
                JButton configButton = new JButton(OPTIONS_BUTTON);
                configButton.setMnemonic('o'); 
                configButton.addActionListener(this);
            
                JPanel panel = new JPanel();
                panel.setLayout(new GridBagLayout());
                GridBagConstraints buttonCons = new GridBagConstraints();
                buttonCons.gridx = 0;
                buttonCons.gridy = 0;
                panel.add(button, buttonCons);
                buttonCons.gridx = 1;
                buttonCons.gridy = 0;
                panel.add(new JLabel("  "), buttonCons);
                buttonCons.gridx = 2;
                buttonCons.gridy = 0;
              
                buttonCons.gridx = 3;
                buttonCons.gridy = 0;
                buttonCons.insets = new Insets(0, 10, 0, 0);
                //panel.add(configButton, buttonCons);
            
                Container contentPane = frame.getContentPane();
                GridBagLayout gridLayout = new GridBagLayout();
                contentPane.setLayout(gridLayout);
                GridBagConstraints cons = new GridBagConstraints();
                cons.fill = GridBagConstraints.BOTH;
                cons.weightx = 10.0f;
                cons.weighty = 10.0f;
                cons.gridx = 0;
                cons.gridy = 0;
                contentPane.add(new JScrollPane(textArea), cons);
                cons.gridy = 1;
                cons.weighty = 5.0f;
                contentPane.add(new JScrollPane(resultArea), cons);
            
                cons.fill = GridBagConstraints.NONE;
                cons.gridx = 0;
                cons.gridy = 2;
                cons.weighty = 0.0f;
                cons.insets = new Insets(3,3,3,3);
                //cons.fill = GridBagConstraints.NONE;
                contentPane.add(label, cons);
                cons.gridy = 3;
                contentPane.add(panel, cons);
                
                frame.pack();
                frame.setSize(600, 600);
                frame.setVisible(true);
              }
            Thanks for your help...

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Which of the lines is line #84 in method createAndShowGU I()?

              kind regards,

              Jos

              Comment

              • teenIce
                New Member
                • Aug 2007
                • 36

                #8
                Originally posted by JosAH
                Which of the lines is line #84 in method createAndShowGU I()?

                kind regards,

                Jos

                resultArea = new JTextPane();

                regards,
                teenICe

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Here is one nit that is a good recipe for brewing nullpointers.

                  You have [CODE=java] JTextPane resultArea = new JTextPane();[/CODE] inside your createGUI method. This hides the resultArea already defined in the class using [CODE=java]private JTextPane resultArea = null;[/CODE]. The later resultArea variable is thus probably not being initialized properly.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by teenIce
                    resultArea = new JTextPane();

                    regards,
                    teenICe
                    Got it: you have a member variable resultArea but you also define a local variable
                    with the same name; it effectively hides the member variable which will remain
                    null all the time. Don't define that local variable.

                    kind regards,

                    Jos

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by teenIce
                      resultArea = new JTextPane();

                      regards,
                      teenICe
                      Something is rotten in the state of Denmark here ...

                      Comment

                      • teenIce
                        New Member
                        • Aug 2007
                        • 36

                        #12
                        so..its means I must comment out this code :

                        Code:
                         private JTextPane resultArea = null;
                        thanks guys...
                        I'll try it... :)

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by teenIce
                          so..its means I must comment out this code :

                          Code:
                           private JTextPane resultArea = null;
                          thanks guys...
                          I'll try it... :)
                          That's not what Jos said. Read it again. Just don't define a new variable for it in your method.

                          @Jos, why would he get a nullpointer at that line? I'd expect the nullpointer to occur somewhere else, most likely outside the createGUI method.

                          Comment

                          • teenIce
                            New Member
                            • Aug 2007
                            • 36

                            #14
                            No...I try to comment out the code but still have same error...I don't know what I'm doing wrong.Hmm...may be I'm not understand enough.Can you guys explain it to me??Please...

                            Thanks in advance..

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by teenIce
                              No...I try to comment out the code but still have same error...I don't know what I'm doing wrong.Hmm...may be I'm not understand enough.Can you guys explain it to me??Please...

                              Thanks in advance..
                              Read the responses again. Commenting out lines of code that give errors does not neccessarily remove the errors.
                              In your method, you have [CODE=java]JTextPane resultArea = new JTextPane();[/CODE]
                              Just change that to [CODE=java] resultArea = new JTextPane();[/CODE] like you did with that text area.

                              Comment

                              Working...