Hi ,
I used a simply method cu return a reference to a Date object:
public Date getCurrentTime() {
return new java.util.Date();
}
Adrian
2009/3/11 Ewald Ertl <[email protected]>
>
> Hi,
>
> On 11 Mrz., 07:15, hyde <[email protected]> wrote:
> > Hi all,
> >
> > building my homework work fine but when running the program I get the
> > following errors:
> >
> > run:
> > Exception in thread "main" java.lang.NullPointerException
> > at MyOwnEvent.getCurrentTime(MyOwnEvent.java:20)
>
> Here you see, that on line 20 of MyOwnEvent.java you get the error.
>
> > at MyOwnListenerImpl.somethingHappened(MyOwnListenerImpl.java:
> > 4)
> > at MyOwnEventSource.triggerSomethingEvent
> > (MyOwnEventSource.java:31)
> > at MyOwnEventExample.main(MyOwnEventExample.java:12)
> > Java Result: 1
> > BUILD SUCCESSFUL (total time: 0 seconds)
> >
> > I´ m a little bit confused about how to return the date and time. I
> > think there lies the root problem that leads to these errrors. Here´s
> > a glance at my "MyOwnEvent.java":
> >
> > [...]
> >
> > import java.util.EventObject;
> > import java.util.Date;
> >
> > public class MyOwnEvent extends EventObject {
> >
> > /** Creates a new instance of MyEvent */
> > public MyOwnEvent(Object source) {
> > super(source);
> > }
> >
> > public MyOwnEvent(Object source, String myOwnEventName) {
> > super(source);
> > this.myOwnEventName = myOwnEventName;
> > }
> >
> > private String myOwnEventName;
> > private Date myOwnEventDate;
> >
> > public long getCurrentTime() {
> > return myOwnEventDate.getTime();
> myOwnEventDate is defined as a private field in your class.
> The following method setMyOwnEventDate is under comment, so it
> can't be called to set the myOwnEventDate, so the variable is null.
> In getCurrentTime() you try to call the method getTime on
> myOwnEventDate, which
> does not reference to a valid object.
>
> > }
> >
> > // public void setMyOwnEventDate(Date myOwnEventDate) {
> > // this.myOwnEventDate = myOwnEventDate;
> > // }
> >
> > }
> >
> > I always get in trouble with the myOwnEventDate variable. Does it
> > really have to by a Date ? When using the myOwnEventDate within
> > getCurrentTime it´s only possible to use "getTime". That´s why I
> > declared the returning value of getCurrentTime as "long". Something´s
> You can also use a Date object as return. The caller receives the
> reference to the
> object and the garbage collector does not recycle it.
>
> HTH
> Ewald
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---