Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

THROW AN EXCEPTION???

Status
Not open for further replies.

Larson

Programmer
Nov 7, 2002
5
0
0
CA
IS THERE ANY WAY TO THROW A NULL POINTER EXCEPTION AT THE BEGINNING OF YOUR METHOD THE SAME WAY YOU CAN THROW AN IOException, OR AN InterruptedException??????
 
Im not sure what you are asking here.
While I have never tried it, surely throwing a NullPointerExction would be the same as throwing any other exception. But since you are asking how to do it I would guess it is not.

Can you not create your own exception and throw it at the start of the method instead perhaps?

BTW Why do you want to throw an exception at the start of a method? ----------------------------------------
There are no onions, only magic
----------------------------------------
 
Yea, it turns out i wasnt thinking right and you can just throw it like any normal exception at the top of your method.

as to your question, You throw exceptions at the top of your method if you are going to have lets say a break in your loop, you would put "throws InterruptedException" at the top of your method.

public static void main (String args []) throws InterruptedException

the reason i was doing this because I was getting a runtime error of a NullPointerException in my method so I tried throwing an exception to that error( the throws NullPointerException) to see if that would eliminate my runtime error, but it did not do as i was hoping. Still getting the same error...Cant figure it out, its probably something SO small

Heres the method where i am recieving this error

public static String takeout(String str,int start, int stop)throws NullPointerException{
String res=null;
res=str.substring (start,stop).trim();
//takeout one part of the text file;
if (res.indexOf("\"")>-1)
res=res.substring(0,res.length()-1);
return res;
 
Hi Larson,

You do not need to explicitly define your method as throwing NullPointerException because it is a descendant of RuntimeException, which can be thrown during the normal operation of the Java Virtual Machine. If you want to track down exactly where your app is trying to use a null you could try catching the exception at some point and printing the stack trace.

try {
takeout(..., ...);
} catch (Exception e) {
e.printStackTrace();
}

This will dump the nested chain of method calls to your console and should help to pinpoint where the problem occurred.

HTH
scrat
 
hey Larson,

hope by this time u had got what caused the null pointer exception but just i case (anybody else would need it)
it seem that the value of start is greater that stop hence the exception
just try giving the condition
if (stop >start)
{
res=str.substring (start,stop).trim()
.....

u may not get the exception
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top