Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
General <strong>Java</strong> Questions IV<br />
case 4:<br />
case 5:<br />
case 6:<br />
case 7:<br />
case 8:<br />
case 9:<br />
case 10:<br />
/* do stuff */<br />
break;<br />
case 11:<br />
/* other stuff */<br />
}<br />
Jan Schaumann <br />
Q: Is there a general reason why nullpointer exceptions occur?<br />
Answer: Of course there is. A NullPointerException happens when you have a<br />
reference, it's set to null (or is a class or instance variable and has never been<br />
initialized), and you try to perform some operation on the object it points to. Since it<br />
really doesn't point to an object, you can't do that.<br />
--<br />
Chris Smith<br />
Q: I am aware that the exceptions in the catch statements must be of the same<br />
type or a super type of the exception thrown in the try block.<br />
My question is this:<br />
Is there any significance in the ordering of the catch statements after a try block?<br />
Answer: It is required to design you catch statement sequences with most derived<br />
exception class type first and progressing to the highest superclass. Else, the code<br />
will not compile.<br />
For a obvious reason, if the a catch block for a given class precedes a catch block for<br />
a type that is derived from the first, the second catch block will never be executed.<br />
--<br />
D. Michael Nelson<br />
Q: I wrote a program that use a few RS232 ports. The operators are unskilled<br />
and often start multiple instances of the program. Will someone please be so kind<br />
and tell me how I can prevent them doing it?<br />
Answer 1: The first instance might write a file. Subsequent instances could check for<br />
the existence of that file, or else check it's contents.<br />
Another method could involve creating a server socket on a specific port.<br />
Subsequent efforts to create a socket on that same port would throw an exception.<br />
Answer 2: Actually a better way is to (on launch):<br />
1) Check if the file exists. If not, create it, open it and run. Leave it open until you quit,<br />
upon which time you close it.<br />
2) If the file _does_ exist, try to delete it. If the delete fails, then someone else has it<br />
open, which means another copy of your app is running. Inform the user and quit.<br />
3) If you succeeded in deleting it, then you are the first. Now create, open and run.<br />
file:///F|/350_t/350_tips/general_java-IV.htm (6 of 10) [2002-02-27 21:18:34]