Problem with initializing a class object

I am trying to write a more modified version of the infamous SimpleBrowser by allowing for the WebBrowser object property to receive a given java.net.URL object.

privatefinal WebBrowser webBrowser =new WebBrowser();

/**

* Perform setup

*/

privatevoid setupSimpleBrowser(){

SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

initObjects();

initComponents();

}

});

}

/**

* Initialize objects

*/

privatevoid initObjects(){

WebBrowser.setDebug(true);// SET TO TRUE TO SEE trace() DEBUG STATEMENTS

}

/**

* Initialize components

*/

privatevoid initComponents(){

setTitle(myName);

generateWebBrowser();

webAddressTextField =new JTextField(51);

generateJButton();

p1 =new JPanel();

p2 =new JPanel();

addToPanel();

forceTFFocus();

addToFrame();

showFrame();

}

/**

* Generate {@link #webBrowser}

*/

privatevoid generateWebBrowser(){

//Use below code to check the status of the navigation process,

//or register a listener for the notification events.

webBrowser.addWebBrowserListener(

new WebBrowserListener(){

boolean isFirstPage =true;

publicvoid downloadStarted(WebBrowserEvent event){;}

publicvoid downloadCompleted(WebBrowserEvent event){;}

publicvoid downloadProgress(WebBrowserEvent event){;}

publicvoid downloadError(WebBrowserEvent event){;}

publicvoid documentCompleted(WebBrowserEvent event){

// Uncomment below code to test getContent()/setContent()/

// executeScript() APIs.

// As the setContent() call will invoke this event, which falls

// into a loop, so check if this event is fired by the first

// loaded page.

/*

if (isFirstPage) {

testDOMAPI(webBrowser);

isFirstPage = false;

}

*/

}

publicvoid titleChange(WebBrowserEvent event){;}

publicvoid statusTextChange(WebBrowserEvent event){;}

publicvoid windowClose(WebBrowserEvent event){;}

publicvoid initializationCompleted(WebBrowserEvent event){;}

});

setWebBrowserURL();

System.out.println(webBrowser.getURL().toString());

}

/**

* Set {@link #webBrowser} with either instantiable {@link java.net.URL} or with {@link #DEFAULT_URL_PATH}

*/

privatevoid setWebBrowserURL(){

try{

URL url = getURL();

System.out.println("url = " + url);

String urlPath = getURLPath();

System.out.println("urlPath = " + urlPath);

if (url !=null){

webBrowser.setURL(url);

}elseif (urlPath !=null && !urlPath.equals("")){

webBrowser.setURL(new URL(urlPath));

}else{

System.out.println("setting webBrowser with " + DEFAULT_URL_PATH);

webBrowser.setURL(new URL(DEFAULT_URL_PATH));

System.out.println("set up initial homepage of " + DEFAULT_URL_PATH);

}

}catch (Exception e){

try{

System.out.println("oops! setting webBrowser with " + DEFAULT_URL_PATH);

webBrowser.setURL(new URL(DEFAULT_URL_PATH));

}catch (Exception e2){

e2.printStackTrace();

}

}

}

However, the moment you leave setWebBrowserURL(), or somehow, within the Thread that is running between setWebBrowserURL() and the System.out.println() line, this happens:

url =null

urlPath =null

setting webBrowser with http://java.net

set up initial homepage of http://java.net

*** Jtrace: You can't call this method before WebBrowser is initialized!

Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException

at com.ppowell.tools.ObjectTools.SimpleBrowser.generateWebBrowser(SimpleBrowser.java:349)

at com.ppowell.tools.ObjectTools.SimpleBrowser.initComponents(SimpleBrowser.java:384)

at com.ppowell.tools.ObjectTools.SimpleBrowser.access$600(SimpleBrowser.java:42)

at com.ppowell.tools.ObjectTools.SimpleBrowser$4.run(SimpleBrowser.java:447)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

*** Jtrace: Process event tonative browser: -1, 0,

*** Jtrace: Send data to socket: -1,0,</html><body></html>

I can't figure out why webBrowser will not allow for me to determine the given java.net.URL object parameter which will allow for the display of the given URL.

This is based upon http://www.koders.com/java/fidC1435DC25F674DBFCB9C50BE409180D2CAD108E1.aspx

Thanx

Phil

[9045 byte] By [ppowell777a] at [2007-11-26 17:41:15]
# 1

I believe your NullPointerException is happening at this line (without seeing the line numbers in your source file, which YOU can do):

> System.out.println(webBrowser.getURL().toString());

Isn't that line 349, related to this part of the stack trace?

> at com.ppowell.tools.ObjectTools.SimpleBrowser.generateWebBrowser(SimpleBrowser.java:349)

If so, then I'd conclude that webBrowser.getURL() is returning null, so you can't invoke toString() on that.

warnerjaa at 2007-7-9 0:09:26 > top of Java-index,Java Essentials,New To Java...
# 2

> I believe your NullPointerException is happening at

> this line (without seeing the line numbers in your

> source file, which YOU can do):

>

> >

> System.out.println(webBrowser.getURL().toString());

> Isn't that line 349, related to this part of the

> stack trace?

> > at

> com.ppowell.tools.ObjectTools.SimpleBrowser.generateWe

> bBrowser(SimpleBrowser.java:349)

>

> If so, then I'd conclude that webBrowser.getURL() is

> returning null, so you can't invoke toString() on

> that.

It is line 349, it is the line that has System.out.println(webBrowser.getURL().toString()), Removing that line removes the NullPointerException being thrown, however, now I am suddenly unable to use setURL() as it constantly receives a null value when I am clearly sending it a non-null value.

/**

* Set {@link #webBrowser} with either instantiable {@link java.net.URL} or with {@link #DEFAULT_URL_PATH}

*/

private void setWebBrowserURL() {

try {

URL url = getURL();

System.out.println("url = " + url);

String urlPath = getURLPath();

System.out.println("urlPath = " + urlPath);

if (url != null) {

webBrowser.setURL(url);

} else if (urlPath != null && !urlPath.equals("")) {

webBrowser.setURL(new URL(urlPath));

} else {

System.out.println("setting webBrowser with " + DEFAULT_URL_PATH);

webBrowser.setURL(new URL(DEFAULT_URL_PATH));

System.out.println("set up initial homepage of " + DEFAULT_URL_PATH);

}

} catch (Exception e) {

try {

System.out.println("oops! setting webBrowser with " + DEFAULT_URL_PATH);

webBrowser.setURL(new URL(DEFAULT_URL_PATH));

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

Phil

ppowell777a at 2007-7-9 0:09:26 > top of Java-index,Java Essentials,New To Java...