java.lang.NullPointerException Null pointer exception problem

1. The so-called pointer is a reference to an object in java. Such as String s; this s is a pointer. 

2. The so-called null pointer means that the content of the pointer is empty, such as the above s, if it is made to point to null, it is a null pointer.

3. The so-called null pointer exception is that a pointer is a null pointer, and you have to operate it. Since it points to a null object, it cannot use the method of this object. For example, if the above s is null, you have to use the method of s, such as s.equals(String x); then a null pointer exception will be generated.


The following lists several situations in which a null pointer exception may occur and the corresponding solutions: 
Code segment 1: 
  out.println(request.getParameter("username")); 
  Analysis: The function of code segment 1 is very simple, that is, to output user input" username" value. 
       Explanation: It appears that the above statement does not find any syntax errors, and in most cases no problems are encountered. However, if a user does not provide the value of the form field "username" when entering data, or bypasses the form for direct input in some way, the value of this request.getParameter("username") is empty (note that it is not empty). A string is an empty object null.), the println method of the out object cannot directly operate on the empty object, so the JSP page where the code segment 1 is located will throw a "Java.lang.NullPointerException" exception. And even when the object may be empty, some methods such as toString(), equal(Object obj), etc. of Java.lang.Object or the Object object itself are called. 

Code snippet 2: 
  String userName = request.getParameter("username"); 
  If (userName.equals("root")) 
  {....} 
  Analysis: The function of code snippet 2 is to detect the username provided by the user, if it is Username is "root"                                                                        

      Note: In code segment 2, if a user does not provide the value of the form field "username", the string object userName is a null value, and a null object cannot be directly compared with another object. Similarly, code segment 2 is where The JSP page will throw a null pointer error. 

A little trick: If you want to compare the return value of a method with a constant, put the constant first to avoid calling the equals method of the null object. For example:  

If ("root".equals(userName)) 
  {....}    Even if the userName object returns a null object, there will be no null pointer exception here, and it can work as usual. Code segment 3:    String userName = session.getAttribute("session.username").toString();          Analysis: The function of code segment 3 is to take out the value of session.username in the session and assign the value to the string object userName .        Note: Under normal circumstances, if the user is already in a session, there will be no problem; however, if the application server restarts at this time, and the user has not logged in again, (it may also be that the user closes the browser, but The original page is still opened.) Then, the value of the session will be invalid at this time, and the value of session.username in the session will be empty. Direct execution of the toString() operation on a null object will cause the system to throw a null pointer exception. Snippet 4:  public static void main(String args[]){         Person p=null;          p. 
   














    System.out.println(p.getName()); 



分析:声明一个Person对象,并打印出该对象的中的Name名字。 

说明:这个时候你的p就出现空指针异常,因为你只是声明了这个Person类型的对象并没有创建对象,所以它的堆里面没有地址引用,切忌你要用对 象掉用方法的时候一定要创建对象

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325852190&siteId=291194637