Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2007

Tip: Looking for answers? Try searching our database.

Problem with initializing a class object

Thread view: 
phillip.s.powell@gmail.com - 07 Feb 2007 19:59 GMT
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.

[code]
private final WebBrowser webBrowser = new WebBrowser();

   /**
    * Perform setup
    */
   private void setupSimpleBrowser() {
       SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               initObjects();
               initComponents();
           }
       });
   }

   /**
    * Initialize objects
    */
   private void initObjects() {
       WebBrowser.setDebug(true); // SET TO TRUE TO SEE trace() DEBUG
STATEMENTS
   }

   /**
    * Initialize components
    */
   private void initComponents() {
       setTitle(myName);
       generateWebBrowser();
       webAddressTextField = new JTextField(51);
       generateJButton();
       p1 = new JPanel();
       p2 = new JPanel();
       addToPanel();
       forceTFFocus();
       addToFrame();
       showFrame();
   }

   /**
    * Generate {@link #webBrowser}
    */
   private void 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;

           public void downloadStarted(WebBrowserEvent event) {;}
           public void downloadCompleted(WebBrowserEvent event) {;}
           public void downloadProgress(WebBrowserEvent event) {;}
           public void downloadError(WebBrowserEvent event) {;}
           public void 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;
               }
                */
           }
           public void titleChange(WebBrowserEvent event) {;}
           public void statusTextChange(WebBrowserEvent event) {;}
           public void windowClose(WebBrowserEvent event) {;}
           public void 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}
    */
   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();
           }
       }
   }
[/code]

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

[code]
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 to native browser: -1, 0,
*** Jtrace: Send data to socket: -1,0,</html><body></html>

[/code]

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
a248@mailinator.com - 07 Feb 2007 20:27 GMT
On Feb 7, 8:59 pm, "phillip.s.pow...@gmail.com"
<phillip.s.pow...@gmail.com> wrote:
> This is based uponhttp://www.koders.com/java/
> fidC1435DC25F674DBFCB9C50BE409180D2CAD108E1.aspx

By removing the original copyright statement and then publishing the
code you have committed a copyright violation. The original is
distributed under the GPL, so you have violated the GPL, too. SHAME ON
YOU!

What about finally learning Java, so you don't have to steal other
person's code and pissing on their copyright?

John
phillip.s.powell@gmail.com - 07 Feb 2007 20:37 GMT
On Feb 7, 3:27 pm, a...@mailinator.com wrote:
> On Feb 7, 8:59 pm, "phillip.s.pow...@gmail.com"
>
[quoted text clipped - 9 lines]
> What about finally learning Java, so you don't have to steal other
> person's code and pissing on their copyright?

What about not wasting my time trying to prove you don't know the
answer to this but know how to act like a jackass?

> John
Lew - 07 Feb 2007 21:40 GMT
a...@mailinator.com wrote:
>> By removing the original copyright statement and then publishing the
>> code you have committed a copyright violation. The original is
[quoted text clipped - 3 lines]
>> What about finally learning Java, so you don't have to steal other
>> person's code and pissing on their copyright?

> What about not wasting my time trying to prove you don't know the
> answer to this but know how to act like a jackass?

Now, you see, you really shouldn't respond like that. If the poster were a
troll they'd be laughing in their beer over your response, and if they weren't
then you just hurt their feelings, for they were merely trying to protect you
from possible legal exposure.

Besides, if you violated a copyright then acting aggrieved actually weakens
your case.

Another thing, /ad hominem/ attacks like that are, to many, /prima facie/
evidence that the attacker is the one in the wrong. You might not care what
the bazillions of other readers on the forum think of you, and that is a good
thing, but even so you may wish to consider the message you are actually
conveying.

- Lew
Eric Sosman - 07 Feb 2007 22:50 GMT
Lew wrote On 02/07/07 16:40,:
> [...]
>
[quoted text clipped - 3 lines]
> thing, but even so you may wish to consider the message you are actually
> conveying.

   Actually, he *does* care what the other readers think of
him, because he's trying to get free help from them.  If they
don't feel kindly towards him, he won't get helped.  Maybe
worse, he actually will get "helped," but by people who try to
strike back by deliberately misleading him.  (Some answers to
"do my homework" questions are like that; it's not unheard-of.)

Signature

Eric.Sosman@sun.com

Lew - 07 Feb 2007 22:58 GMT
Lew wrote
>> [...]
>>
>> You might not care what
>> the bazillions of other readers on the forum think of you, and that is a good
>> thing, ...

>     Actually, he *does* care what the other readers think of
> him, because he's trying to get free help from them.  If they
> don't feel kindly towards him, he won't get helped.  Maybe
> worse, he actually will get "helped," but by people who try to
> strike back by deliberately misleading him.  (Some answers to
> "do my homework" questions are like that; it's not unheard-of.)

It is unfortunate that people would be that cruel. It is a reminder to the
rest of us to ponder carefully the value of free advice, lest we discover that
we were short-changed.

- Lew
Eric Sosman - 08 Feb 2007 03:17 GMT
> Lew wrote
>>> [...]
[quoted text clipped - 12 lines]
> the rest of us to ponder carefully the value of free advice, lest we
> discover that we were short-changed.

    My own incorrect posts are mistakes, not acts of malice.
However, I tend to drop out of threads with tiresome or
offensive participants ("tiresome" and "offensive" being my
own notions and not necessarily objective), meaning that I
don't see or correct mistakes others might make.  I'd imagine
there are others who act similarly.

    One needn't provoke active hostility to get inferior help;
mere disregard will do.

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Lew - 08 Feb 2007 03:32 GMT
>  ... I tend to drop out of threads with tiresome or
> offensive participants ("tiresome" and "offensive" being my
[quoted text clipped - 4 lines]
>     One needn't provoke active hostility to get inferior help;
> mere disregard will do.

Your points are well taken. Not just in this forum, but in most social
contexts there is a give and take between participants; members who disturb
the mores tend to receive less cooperative behavior. It seems not only logical
but just.

The flip side is that occasional outbursts are tolerated from anyone, if they
don't develop a reputation for overindulgence in them and if they don't cause
major damage. Some of us, myself included, cannot unhypocritically decry the
use of sarcasm, derision, self-aggrandizement or other hostile behaviors;
others may cut people some slack for more altruistic reasons.

- Lew
phillip.s.powell@gmail.com - 08 Feb 2007 13:45 GMT
> Lew wrote
>
[quoted text clipped - 13 lines]
> rest of us to ponder carefully the value of free advice, lest we discover that
> we were short-changed.

Also the often-maligned approach of "second opinion" is valuable as
well, because the first advice may either be bogus or unintentionally
misleading, or just plain wrong.  It's just when it goes to the level
of illogical conclusion-drawing and "ad hominem" that it makes me
wonder if they have any social skills whatsoever, thus, you are forced
to seek a "second opinion".

> - Lew
Nigel Wade - 08 Feb 2007 10:51 GMT
> 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.

You've only supplied partial code, so it's only possible to provide guesses as
to what is going wrong.

> [code]
> private final WebBrowser webBrowser = new WebBrowser();

What class does this code belong to, where is it's constructor being invoked?

I will attempt to guess at the problem. I think you probably have a race
condition between the thread which is executing the above constructor for
WebBrowser(), and the code below which is explicitly run on the EDT. It looks
like the code below is attempting to set the URL on the WebBrowser before it
has been properly constructed by whichever thread is constructing it.

>     /**
>      * Perform setup
[quoted text clipped - 7 lines]
>         });
>     }

> [/code]

> Phil

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

phillip.s.powell@gmail.com - 08 Feb 2007 13:52 GMT
> phillip.s.pow...@gmail.com wrote:
> > I am trying to write a more modified version of the infamous
[quoted text clipped - 14 lines]
> like the code below is attempting to set the URL on the WebBrowser before it
> has been properly constructed by whichever thread is constructing it.

You might be right, unfortunately I cannot verify it.  However, I can
say that the org.jdesktop.jdic.browser.WebBrowser object works when I
do things this way:

[code]
/**
    * {@link org.jdesktop.jdic.browser.WebBrowser}
    */
   private WebBrowser browser;

/**
    * Perform setup
    */
   private void setupSimpleBrowser() {
       initObjects();
       initComponents();
   }

 /**
    * Initialize objects
    */
   private void initObjects() {
       WebBrowser.setDebug(false); // SET TO TRUE TO SEE trace()
DEBUG STATEMENTS
       browser = new WebBrowser();
   }

 /**
    * Initialize components
    */
   private void initComponents() {
       setTitle(myName);
       webAddressTextField = new JTextField(51);
       generateJButton();
       p1 = new JPanel(true);
       p2 = new JPanel(true);  // MUST BE SET BEFORE GOING TO
generateWebBrowser()
       generateWebBrowser();   // WILL ADD LOCAL WebBrowser INSTANCE
ONTO JPanel p2 HERE
       addToPanel();           // FOR NOW WILL ONLY ADD JPanel p1
       forceTFFocus();
       addToFrame();
       showFrame();
   }

/**
    * Generate {@link org.jdesktop.jdic.browser.WebBrowser}
    */
   private void generateWebBrowser() {

       //Use below code to check the status of the navigation
process,
       //or register a listener for the notification events.
       browser.addWebBrowserListener(
               new WebBrowserListener() {
           boolean isFirstPage = true;

           public void initializationCompleted(WebBrowserEvent event)
{;}
           public void downloadStarted(WebBrowserEvent event) {;}
           public void downloadCompleted(WebBrowserEvent event) {;}
           public void downloadProgress(WebBrowserEvent event) {;}
           public void downloadError(WebBrowserEvent event) {;}
           public void 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(browser);
                   isFirstPage = false;
               }
                */
           }
           public void titleChange(WebBrowserEvent event) {;}
           public void statusTextChange(WebBrowserEvent event) {;}
           public void windowClose(WebBrowserEvent event) {;}
       });

       setWebBrowserURL();
   }

/**
    * Set {@link #browser} with either instantiable {@link
java.net.URL} or with {@link #DEFAULT_URL_PATH}
    */
   private void setWebBrowserURL() {
       try {
           URL url = getURL();
           String urlPath = getURLPath();
           if (url != null) {
               browser.setURL(url);
           } else if (urlPath != null && !urlPath.equals("")) {
               browser.setURL(new URL(urlPath));
           } else {
               browser.setURL(new
URL(SimpleBrowser.DEFAULT_URL_PATH));
           }
       } catch (Exception e) {
           try {
               browser.setURL(new
URL(SimpleBrowser.DEFAULT_URL_PATH));
           } catch (Exception e2) {
               e2.printStackTrace();
               return;
           }
       }
   }
[/code]

It appears that the crucial element is within initObjects():

WebBrowser.setDebug(false);
webBrowser = new WebBrowser();

In short, you must run the WebBrowser static mehod setDebug() before
you even instantiate a WebBrowser object, otherwise, while the object
will exist regardless, it will not actually set the java.net.URL
parameter unless you have run setDebug() first.

Phil

> >     /**
> >      * Perform setup
[quoted text clipped - 17 lines]
> E-mail :    n...@ion.le.ac.uk
> Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555
Nigel Wade - 09 Feb 2007 09:54 GMT
>> phillip.s.pow...@gmail.com wrote:
>  > > I am trying to write a more modified version of the infamous
[quoted text clipped - 142 lines]
>
> Philin

The above is different from your initial code in one very crucial respect. In
the above you create your WebBrowser object in initObjects, which is being run
by the EDT. In your original code you never showed where your WebBrowser was
being created, and if it was not in the EDT that might be the cause of a race
condition.

What happens if you reverse the order of the above statements, but keep them
within initObjects()?

The error message in the original post seems to be due to this line of code:
  webBrowser.setURL(new URL(DEFAULT_URL_PATH));
being executed before the construction of webBrowser is complete, when some of
its fields are still null. So the construction of webBrowser and this code must
be being executed in parallel, i.e. on different threads.

I may be wrong of course, but the available evidence points me to that
conclusion.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.