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;
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.
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()
.....
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.