-1

I am having the following exception:

Exception in thread "main" java.lang.NullPointerException
    at Studentlist.Studentlist(Studentlist.java:79)
    at Studentlist.main(Studentlist.java:38)

on line 38: I am passing to input to getstudentsprofile.

on line79: I am doing :

int z= array1.length;

How to debug this issue. I am reading lot of records and everything it is crashing at one place with this exception. How to debug this? how can I see when the exception happened what are the values?

Infact I have :

catch (IOException ee)
        {
            ee.printStackTrace();
        }      

but no luck.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
The Learner
  • 3,867
  • 14
  • 40
  • 50

8 Answers8

4

seems like array1 is null and NullPointerException is not a IOException so you need to catch

catch (NullPointerException npe) {
            npe.printStackTrace();
}

or even better don't give your code to raise this condition for that statement, check for null

int z = SOME_DEFAULT_VALUE;
if(arr!=null){
 z = arr.length;
}
jmj
  • 237,923
  • 42
  • 401
  • 438
4

You should not be catching NullPointer exception as it's a RunTimeException and this occurs usually due to issue in your code that does not get caught at compile time. Please check these discussions for more details.

Is Catching a Null Pointer Exception a Code Smell?

If catching null pointer exception is not a good practice, is catching exception a good one?

Community
  • 1
  • 1
Anupam Saini
  • 2,431
  • 2
  • 23
  • 30
3

Check whether array1 is null

if(array1 ! = null) { // you should always avoid NPEs

  int z= array1.length;
}

Or

catch (IOException | NullPointerException e) // you can multi-catch exceptions, java7 new feature
   e.printStackTrace();
}    

NullPointerException and IOException are different. Follow java tutorial for Exceptions .

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
2

array1 is not initialised that's why you get the NullPointerException.

Obviously, an IOException is not a NullPointerException thats why it doesn't go in the catch block.

To debug a java program use an IDE that has built in debug functionality. You will find very easy to use Netbeans. Others like Eclipse and JDeveloper have nice features too.

Rakibul Haq
  • 1,348
  • 23
  • 32
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
2

array1 at line 79 is null, but you should not catch NullPointerException as it most likely signals a bug in your program. Instead you should check if the reference is != null (if null is a valid value in your program) or, if not, signal the caller code that it provided an invalid value by, for example, throwing an InvalidArgumentException.

if(array1 ! = null) { //if array1 being null is a valid value in a program
    int z= array1.length;
}
//continue with your processing

or

if(array1 ! = null) { //if array1 being null is not a valid value in a program
    throw new IllegalArgumentException("You should pass a not null array for processing");
}
dcernahoschi
  • 14,968
  • 5
  • 37
  • 59
2

NullPointerException is occurred because of you not initialize array1 that means the array1 is not point to any memory location.You have to initialize array1 like int[] arrray1 = new int[size];

Rajshri
  • 4,163
  • 2
  • 15
  • 17
2

Just Handle it with try/catch.... No need to use if condition...

try{

     int z= array1.length;

 }catch(final IOException|NullPointerException ex){    

          // This Nested catch works with Java 7 and above

 }
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
2

Hey do not catch the NullPointerException and it is not a good practice. Rather , try to fix the problem. Runtime exceptions are given to fix the programming errors. In your case, array1 is NULL , because it is not been initialized. Do that and it should work.

sakthisundar
  • 3,278
  • 3
  • 16
  • 29