What is a NullPointerException, and how do I fix it?
2017-02-02 12:07
483 查看
What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them? What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely? java nullpointerexception
| |||
This question's answers are a collaborative effort: | |||
comments disabled on deleted / locked posts / reviews |
12 Answers
activeoldestvotesup vote1622down voteaccepted | When you declare a reference variable (i.e. an object) you are really creating a pointer to an object. Consider the following code where you declare a variable of primitive type int: int x; x = 10; In this example the variable x is an intand Java will initialize it to 0 for you. When you assign it to 10 in the second line your value 10 is written into the memory location pointed to by x. But, when you try to declare a reference type something different happens. Take the following code: Integer num; num = new Integer(10); The first line declares a variable named num, but, it does not contain a primitive value. Instead it contains a pointer (because the type is Integerwhich is a reference type). Since you did not say as yet what to point to Java sets it to null, meaning "I am pointing at nothing". In the second line, the newkeyword is used to instantiate (or create) an object of type Integer and the pointer variable num is assigned this object. You can now reference the object using the dereferencing operator .(a dot). The Exceptionthat you asked about occurs when you declare a variable but did not create an object. If you attempt to dereference numBEFORE creating the object you get a NullPointerException. In the most trivial cases the compiler will catch the problem and let you know that "num may not have been initialized" but sometime you write code that does not directly create the object. For instance you may have a method as follows: public void doSomething(SomeObject obj){ //do something to obj } in which case you are not creating the object obj, rather assuming that is was created before the doSomethingmethod was called. Unfortunately it is possible to call the method like this: doSomething(null); in which case objis null. If the method is intended to do something to the passed-in object, it is appropriate to throw the NullPointerExceptionbecause it's a user error and the user will need that information for debugging purposes. Alternatively, there may be cases where the purpose of the method is not solely to operate on the passed in object, and therefore a null parameter may be acceptable. In this case, you would need to check for a null parameter and behave differently. You should also explain this in the documentation. For example, doSomethingcould be written as: /**@param obj An optional foo for ____. May be null, in which case * the result will be ____. */ public void doSomething(SomeObject obj){ if(obj != null){ //do something } else { //do something else } } Finally, How to pinpoint the exception location & cause using Stack Trace |
相关文章推荐
- What is tethering and how do you enable tethering?
- 【转】Sys.WebForms.PageRequestManagerParserErrorException - what it is and how to avoid it
- What is love ? and how to do?
- What is a NullPointerException?
- Sys.WebForms.PageRequestManagerParserErrorException - what it is and how to avoid it
- What is a "sosreport" and how do I create it in Red Hat Enterprise Linux 4.6 and later
- What is virtual memory, how is it implemented, and why do operating systems use it?
- Sys.WebForms.PageRequestManagerParserErrorException - what it is and how to avoid it
- Sys.WebForms.PageRequestManagerParserErrorException - what it is and how to avoid it
- What is Logical Volume Management and How Do You Enable It in Ubuntu?
- What is the PPA and How to do with it ?
- What is overfitting and how can I avoid it?
- (Page 3 of 3 )A walking tour of JavaBeans What JavaBeans is, how it works, and why you want to use it
- What is JNI Graphics and how to use it?
- (Page 1 of 3 )A walking tour of JavaBeans What JavaBeans is, how it works, and why you want to use it
- what is the difference between "isempty"and "isnull"in the qt bool QString::isEmpty () const Retur.
- What is xylitol and how does it help to prevent cavities?
- (Page 2 of 3 )A walking tour of JavaBeans 2 :What JavaBeans is, how it works, and why you want to use it
- What is SPAWN concurrent program and how to write such program?
- What is global registry and how to know a repository is a global registry