This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EXIT_ON_CLOSE  RSS feed

 
chandrima bhat
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JFrame f;
f.setDefaultCloseOption(JFrame.EXIT_ON_CLOSE);

I wrote a program using the above code, but its giving errors. Can anyone help me with a correct way of setting setDefaultCloseOption in Java?
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it helps if you can post the exact error message you are getting. However in this case I imagine the problem is that you haven't initialised f. The variable f is still null because you haven't called the JFrame constructor, you have only defined it as being a JFrame. You cannot call any methods on a null variable. Change the code to this:
 
magic zha
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it must be:
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cant see the diference
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no difference. The reason I kept the first two lines separate in my example was to stress the difference between variable declaration and initialisation.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
of course

perhaps magic didnt want a title for the frame
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miguel and Stuart are correct; splitting the declaration into two lines doesn't change the meaning at all, and in fact the compiler will very likely emit the identical bytecode in both cases.

Since we're quibbling over small details, however, I can't help but remark that Stuart's comment on the first line that "f is still null here" isn't quite right. f's value at that point is undefined; if you tried to read the value, the compiler would reject the code because f hasn't been initialized. If f were a member, then it would be automatically initialized to null, but that's not the case for locals.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, spot on Ernest - thanks for that
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!