The solutions mentioned below are given by different people. Check it out which will work out for your application.
Solution 1:
Null Pointer Exceptions usually occur when you try to use an object that has been declared but not yet instantiated.
Let's say you declared an ArrayList in your code, e.g. private ArrayList someList.
If you attempt to use someList without first instantiating the list: someList.add(object o); without first calling someList = new ArrayList(), you would get a null pointer Exception.
Fixing null pointer exceptions is code dependent. There is no overall solution: it depends on what caused the exception.
Solution 2:
Don't invoke operations on null objects. You can test for the reserved word null before calling a method. Sometimes it is valid for a variable to contain a null reference. In order to "fix" a null variable you need to look at where you think you should be constructing the object in question and see what's going wrong.
Solution 3:
Do some things to avoid null pointer exceptions. Be careful with these suggestions to only use them when appropriate. When I initialize something, I try to give it a default value that means "nothing there" without setting it to null. For example, String name=""; HashMap hm=new HashMap().
When I write methods, I don't return null unless it is crucial to do so. The idea is to send back something so that methods that work with it don't throw a NullPointerException but will work acceptably within your application (i.e. won't yield false positives). Otherwise, always check for a null object before invoking any method.
Solution 1:
Null Pointer Exceptions usually occur when you try to use an object that has been declared but not yet instantiated.
Let's say you declared an ArrayList in your code, e.g. private ArrayList someList.
If you attempt to use someList without first instantiating the list: someList.add(object o); without first calling someList = new ArrayList(), you would get a null pointer Exception.
Fixing null pointer exceptions is code dependent. There is no overall solution: it depends on what caused the exception.
Solution 2:
Don't invoke operations on null objects. You can test for the reserved word null before calling a method. Sometimes it is valid for a variable to contain a null reference. In order to "fix" a null variable you need to look at where you think you should be constructing the object in question and see what's going wrong.
Solution 3:
Do some things to avoid null pointer exceptions. Be careful with these suggestions to only use them when appropriate. When I initialize something, I try to give it a default value that means "nothing there" without setting it to null. For example, String name=""; HashMap hm=new HashMap().
When I write methods, I don't return null unless it is crucial to do so. The idea is to send back something so that methods that work with it don't throw a NullPointerException but will work acceptably within your application (i.e. won't yield false positives). Otherwise, always check for a null object before invoking any method.
Comments
Post a Comment