Hi everybody.

I'm trying to display web page that located in my computer or just (google web page).
But everytime I'mgetting nullpointer exception.

Please help me to figure out the problem.

here is the absolute path that I'm using:

serg@serg-PORTEGE-Z835:~$ find $PWD -type f -name test1.html
/home/serg/Projects/java/GUI/src/its/TestData/test1.html

But the error that I'm getting is null pointer exception

I put a coments in the code so you can see were is the error

(By the way I just start learning GUI, so the problem could be in some other statement =) )

package its.HTML;

import javax.swing.JEditorPane;
import javax.swing.JFrame;

public class SimpleHTMLFrame extends JFrame{
	private JEditorPane ediPane;
	//private static String HTMLSource = "http://www.google.com";
	
	// I think this cause error
	private static String HTMLSource = "/home/serg/Projects/java/GUI/src/its/TestData/test1.html";
	private static String workingDir;
	
	public SimpleHTMLFrame(String URLname) {
		this.setSize(210,300);
		this.setLocation(200,200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400,400);
		this.setVisible(true);
		this.setTitle("HTML display");
		
		try {
			ediPane = new JEditorPane(HTMLSource);
			ediPane.setEditable(false);
			
			} catch (Exception e) {
			
                        //ERROR
			// I have Exception in thread "main" java.lang.NullPointerException in here
			this.getContentPane().add(ediPane);
		}
	}
	public static void main(String[] args) {
		SimpleHTMLFrame shf = new SimpleHTMLFrame(HTMLSource);
	}
	
}

What line is the NPE on? What variable has a null value?

Add a call to the printStackTrace method to get the full text of the error message.

What line is the NPE on? What variable has a null value?

Add a call to the printStackTrace method to get the full text of the error message.

so I added printStackTrace and it gave me this:

java.net.MalformedURLException: no protocol: /home/serg/Projects/java/GUI/src/its/TestData/test1.html
	at java.net.URL.<init>(URL.java:583)
	at java.net.URL.<init>(URL.java:480)
	at java.net.URL.<init>(URL.java:429)
	at javax.swing.JEditorPane.setPage(JEditorPane.java:938)
	at javax.swing.JEditorPane.<init>(JEditorPane.java:273)
	at its.HTML.SimpleHTMLFrame.<init>(SimpleHTMLFrame.java:23)
	at its.HTML.SimpleHTMLFrame.main(SimpleHTMLFrame.java:34)
Exception in thread "main" java.lang.NullPointerException
	at java.awt.Container.addImpl(Container.java:1037)
	at java.awt.Container.add(Container.java:373)
	at its.HTML.SimpleHTMLFrame.<init>(SimpleHTMLFrame.java:30)
	at its.HTML.SimpleHTMLFrame.main(SimpleHTMLFrame.java:34)

Please post your current code that has the problem.

Where do you add anything to the JFrame?

Please post your current code that has the problem.

Where do you add anything to the JFrame?

YOU ARE THE BEST!

When you sad "Where you add anything to the JFrame?", Ilooked thrue the code and found that I add it in catch statement.
I changed it ! so now it works fine!
thank you very much!

This question has already been answered. Start a new discussion instead.