General Java Questions I
   case 11:
       /* other stuff */
}
Jan Schaumann 
 Q: Is there a general reason why nullpointer exceptions occur?
Answer: Of course there is. A NullPointerException happens when you have a
reference, it s set to null (or is a class or instance variable and has never been
initialized), and you try to perform some operation on the object it points to. Since it
really doesn t point to an object, you can t do that.
  
Chris Smith
 Q: I am aware that the exceptions in the catch statements must be of the same
type or a super type of the exception thrown in the try block.
My question is this:
Is there any significance in the ordering of the catch statements after a try block?
Answer: It is required to design you catch statement sequences with most derived
exception class type first and progressing to the highest superclass. Else, the code
will not compile. 
For a obvious reason, if the a catch block for a given class precedes a catch block for
a type that is derived from the first, the second catch block will never be executed.
  
D. Michael Nelson
 Q: I wrote a program that use a few RS232 ports. The operators are unskilled
and often start multiple instances of the program. Will someone please be so kind
and tell me how I can prevent them doing it?
Answer 1: The first instance might write a file. Subsequent instances could check for
the existence of that file, or else check it s contents.
Another method could involve creating a server socket on a specific port. Subsequent
efforts to create a socket on that same port would throw an exception.
Answer 2: Actually a better way is to (on launch):
1) Check if the file exists. If not, create it, open it and run. Leave it open until you quit,
upon which time you close it.
2) If the file _does_ exist, try to delete it. If the delete fails, then someone else has it
open, which means another copy of your app is running. Inform the user and quit.
3) If you succeeded in deleting it, then you are the first. Now create, open and run.
Doing the above prevents the problem of having the semaphore file left around when
the system crashes. I implemented it recently in one of our apps, and it works like a
charm.
   
Burt Johnson
MindStorm Productions, Inc.
http://www.mindstorm inc.com
file:///F|/a_jsite/350_tips/general_java I.htm (29 of 33) [2001 07 08 11:24:51]






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java web hosting

 

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved