1

When I run my simple Java browser, I try and visit a webpage such as http://google.com and it returns the NullPointerException error from my try catch code, how would I fix this?

Frame Class:

public class Frame extends JFrame {

public EditorPane pane;
public URLBar urlbar;

public static void main(String[] args) throws Exception {

    Frame frame = new Frame();

}

public Frame() throws Exception {
    super("Java Browser v1.0");

    JPanel mainPanel = new JPanel(new BorderLayout());
    URLBar addressBar = new URLBar("Enter URL here!", pane);
    EditorPane contentDisplay = new EditorPane(urlbar);

    mainPanel.add(contentDisplay, BorderLayout.CENTER);
    mainPanel.add(addressBar, BorderLayout.NORTH);

    add(mainPanel);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(700, 400);
    add(new JScrollPane(mainPanel));
    setVisible(true);
}

}

URLBar Class:

public class URLBar extends JTextField {

public EditorPane pane;

public URLBar(String text, EditorPane pane) {

    super(text);

     addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                loadContent(event.getActionCommand());
            }
        }
     );
}

public void loadContent(String userInput) {
    try
    {
        pane.setPage(userInput);
        setText(userInput);
    }
    catch (Exception e)
    {
        System.out.println("A wild exception appeared! Type: " + e);
    }
}

}

EditorPane Class:

public class EditorPane extends JEditorPane {

public URLBar urlbar;

public EditorPane(URLBar urlbar) {

    setEditable(false);
    setVisible(true);
    addHyperlinkListener(
            new HyperlinkListener() {
                public void hyperlinkUpdate(HyperlinkEvent event) {
                        if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED) {
                        urlbar.loadContent(event.getURL().toString());
                    }
                }
            }
    );

}

}
William Rose
  • 47
  • 2
  • 9
  • What is the full error message including stack trace? – KSFT Mar 29 '15 at 18:26
  • If you've done even a little searching on solving a NullPointerException (NPE), you'll know that the most important bit of information that we need is the exception's associated stacktrace and some identification of the line that causes it, something that the stacktrace will tell you, and unfortunately neither of which you've posted here with your question. Please fix this so that we can help you. – Hovercraft Full Of Eels Mar 29 '15 at 18:35
  • Most importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Mar 29 '15 at 18:35
  • Here is the stack trace: – William Rose Mar 29 '15 at 19:46
  • java.lang.NullPointerException at URLBar.loadContent(URLBar.java:29) at URLBar$1.actionPerformed(URLBar.java:20) – William Rose Mar 29 '15 at 19:46
  • Line 29 is pane.setPage(userInput); – William Rose Mar 29 '15 at 19:47

1 Answers1

0

It looks like you forgot to init the pane member in your URLBar constructor, which means it is null when you call loadContent.

Here's a fix:

public URLBar(String text, EditorPane pane) {

    super(text);

    this.pane = pane; // add this
     addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                loadContent(event.getActionCommand());
            }
        }
     );
}
Eran
  • 387,369
  • 54
  • 702
  • 768