Web应用程序中的搜索查询中的java.lang.NullPointerException - JSP

时间:2014-01-10 13:38:02

标签: sql jsp jpa web-applications

我要访问我的数据库以获取信息,并且它会继续抛出空的异常。

我呼叫对象和技术人员对象/

addTech.jsp:

    <%@page import="com.example.backend.BackendFactory"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.example.model.*"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Adding Tech</title>
</head>
<%
    String TechId, TechName, TechUserName, TechPass, TechPassVer, TechRank, TechSecQ, TechSecA, TechEmail, TechPhone;
    TechName = request.getParameter("TechName");
    TechUserName = request.getParameter("TechUserName");
    TechPass = request.getParameter("TechPass");
    TechPassVer = request.getParameter("TechPassVer");
    TechRank = request.getParameter("TechRank");
    TechSecQ = request.getParameter("TechSecQ");
    TechSecA = request.getParameter("TechSecA");
    TechEmail = request.getParameter("TechEmail");
    TechPhone = request.getParameter("TechPhone");
    if (TechName != null && TechUserName != null && TechPass != null
            && TechPassVer != null && TechRank != null
            && TechSecQ != null && TechSecA != null
            && TechEmail != null && TechPhone != null)
    {
        Technician t = new Technician(TechName, TechUserName, TechPass,
                new ArrayList<Call>(), TechnicianRank.Beginner,
                TechSecQ, TechSecA, TechPhone, TechEmail, (float) 0.0);
        BackendFactory.getInstance().addTechnician(t);
    }
%>
<body>
    <h1>Adding Technican</h1>
    <form name="tempForm" action="addTech.jsp" method="get">
        <table align="left" border="0" cellpadding="1" cellspacing="1"
            style="width: 500px;">
            <tbody>
                <tr>
                    <td><strong style="color: rgb(255, 0, 0);"><u>Tech
                                Details:</u></strong></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Id:</td>
                    <td><input name="TechId" readonly="readonly" type="text"
                        value=<%out.print(BackendFactory.getInstance().getTechnicianCurrentId());%> /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Name:</td>
                    <td><input name="TechName" type="text" value="Your first name" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>User name:</td>
                    <td><input name="TechUserName" type="text"
                        value="Your last name" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Password:</td>
                    <td><input name="TechPass" type="password" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Verify Password:</td>
                    <td><input name="TechPassVer" type="password" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Rank:</td>
                    <td><input name="TechRank" readonly="readonly" type="text"
                        value="Beginer" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Security Q:</td>
                    <td><input name="TechSecQ" size="30" type="text"
                        value="Whats your grandmother birth?" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Security A:</td>
                    <td><input name="TechSecA" size="30" type="text"
                        value="Dont remember..." /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Email:</td>
                    <td><input name="TechEmail" type="text"
                        value="example@gmail.com" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Phone:</td>
                    <td><input name="TechPhone" type="text" value="0123456789" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><input name="addTech" type="submit" value="Submit" /></td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>

addCall.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="com.google.gwt.dom.client.SelectElement"%>
<%@page import="sun.reflect.CallerSensitive"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<html>
<%@page import="com.example.model.*"%>
<%@page import="com.example.backend.BackendFactory"%>
<%@page import="com.example.backend.ServerBackend"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>

<head>
<meta http-equiv="Content-Type"
    content="text/html; charset=windows-1255">
<title>Insert title here</title>
</head>
<body>
    <h1>Adding Call
        <%/*out.print(backend.findTechByName("sabag"));*/%>

        </h1>
    <form name="tempForm" action="addCall.jsp" method="get">
        <%!ServerBackend backend = BackendFactory.getInstance();%>
        <%
            /*backend.addTechnician(new Technician());*/
            //
            String desc, tech, techRank, ClientAddress, ClientFirst, ClientLast;
            desc = request.getParameter("desc");
            tech = request.getParameter("tech");
            ClientAddress = request.getParameter("ClientAddress");
            ClientFirst = request.getParameter("ClientFirst");
            ClientLast = request.getParameter("ClientLast");
            Technician t=null ;
            if (tech != null)
            {
                try
                {
                    t=backend.findTechByName(tech);
                } catch (Exception e)
                {
                    t = new Technician();
                    t.setName(tech);
                    backend.addTechnician(t);
                }
            }
            if (desc != null && tech != null && ClientAddress != null
                    && ClientFirst != null && ClientLast != null)
            {
                Call call = new Call();
                call.setDescription(desc);
                call.setCaller(new Client(ClientFirst + ClientLast,
                        new Address(ClientAddress, ClientAddress,
                                ClientAddress, 45)));
                call.setTechnician(t);
                backend.addCall(call);
            }
            /**/
        %>
        <table align="left" border="0" cellpadding="1" cellspacing="1"
            style="width: 500px;">
            <tbody>
                <tr>
                    <td><strong style="color: rgb(255, 0, 0);"><u>Call
                                Details:</u></strong></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Id:</td>
                    <td>
                        <div>
                            <input name="id" readonly="readonly" type="text"
                                value=<%out.print(backend.getCallCurrentId());%> />
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Description:</td>
                    <td><textarea name="desc"></textarea></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Technician:</td>
                    <td><select name="tech" size="1">
                            <%
                                try
                                        {
                                        List<String> lst=backend.getAllTechsNames();
                                        for(String s : lst) {
                            %>
                            <option value="100">
                                <%
                                    out.print(s);
                                %>
                            </option>
                            <%
                                } 
                                        }
                                        catch(Exception e)
                                        {
                            %>
                            <option>
                                <%
                                    out.println("fdghjk");
                                %>
                            </option>
                            <%
                                }
                            %>
                    </select>&nbsp;</td>
                </tr>
                <tr>
                    <td><strong><u><span
                                style="color: rgb(255, 0, 0);">Caller Details:</span></u></strong></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>First name</td>
                    <td><input name="ClientFirst" type="text"
                        value="Your first name" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Last name</td>
                    <td><input name="ClientLast" type="text"
                        value="Your last name" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>Address</td>
                    <td><input name="ClientAddress" type="text"
                        value="Your address" /></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><input name="addCall" type="submit" value="Submit" /></td>
                </tr>
            </tbody>
        </table>
    </form>
</body>
</html>

而且最重要的是: DBBackend代码:

package com.example.backend;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import com.example.model.Call;
import com.example.model.CallStatus;
import com.example.model.SparePart;
import com.example.model.Technician;
import com.example.model.ThingToDo;

public class DBbackend implements Backend
{
    private EntityManagerFactory emf = null;
    private EntityManager em = null;
    private String PERSISTENCE_UNIT_NAME = "transactions-optional";

    //
    public DBbackend()
    {
        emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        em = emf.createEntityManager();
    }

    private void insert(Object obj) throws Exception
    {
        em.getTransaction().begin();
        em.persist(obj);
        try
        {
            em.getTransaction().commit();
        } catch (Exception e)
        {
            em.clear();
            throw e;
        }
    }

    @Override
    public int getNextCallId()
    {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void removeCall(int id) throws Exception
    {
        // TODO Auto-generated method stub

    }

    @Override
    public ArrayList<Call> getCallsByTech(Technician t) throws Exception
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void updateCall(Call call) throws Exception
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void save()
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void addCall(Call call) throws Exception
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void addTechnician(Technician technician) throws Exception
    {
        // TODO Auto-generated method stub
        insert(technician);
    }

    @Override
    public Technician getTechnicianByUserName(String userName) throws Exception
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Call getCall(int id) throws Exception
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void addCallToTech(Call call, Technician t) throws Exception
    {
        // TODO Auto-generated method stub
        call.setTechnician(t);
        insert(call);
    }

    @Override
    public Technician findTechByName(String name) throws Exception
    {
        // TODO Auto-generated method stub
        Query q = em.createQuery(
                "SELECT technician FROM Technician technician "
                        + "WHERE technician.name = :Name ", Technician.class);
        q.setParameter("Name", name);
        return (Technician) q.getSingleResult();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String> getAllTechsNames() throws Exception
    {
        // TODO Auto-generated method stub
        TypedQuery<Technician> query =
                em.createQuery("SELECT p FROM Technician p", Technician.class);
            List<Technician> results = query.getResultList();
        List<String> nameList = new ArrayList<String>();
        for (Technician item : results)
            nameList.add(item.getName());

        return nameList;
    }

    @Override
    public ArrayList<String> blabla() throws Exception
    {
        // TODO Auto-generated method stub
        Query q = em.createQuery(
                "SELECT technician FROM Technician technician",
                Technician.class);
        @SuppressWarnings("unchecked")
        List<Technician> lst = q.getResultList();
        ArrayList<String> nameList = new ArrayList<String>();
        for (Technician item : lst)
            nameList.add(item.getName());
        return nameList;
    }

    @Override
    public int getTechnicianCurrentId()
    {
        // TODO Auto-generated method stub
        return Technician.getIdCount();
    }

    @Override
    public int getCallCurrentId()
    {
        // TODO Auto-generated method stub
        return Call.getIdCount();
    }

}

堆栈跟踪:

来源:com.google.appengine.datanucleus.query.QueryEntityPKFetchFieldManager.fetchIntField(QueryEntityPKFetchFieldManager.java:70)类:com.google.appengine.datanucleus.query.QueryEntityPKFetchFieldManager方法:fetchIntField行:70Source:org.datanucleus.identity .IdentityUtils.getApplicationIdentityForResultSetRow(IdentityUtils.java:93)类:org.datanucleus.identity.IdentityUtils方法:getApplicationIdentityForResultSetRow行:93Source:com.google.appengine.datanucleus.EntityUtils.entityToPojo(EntityUtils.java:1011)类:com.google .appengine.datanucleus.EntityUtils方法:entityToPojo行:1011Source:com.google.appengine.datanucleus.query.DatastoreQuery $ 2.apply(DatastoreQuery.java:229)类:com.google.appengine.datanucleus.query.DatastoreQuery $ 2方法: apply Line:229Source:com.google.appengine.datanucleus.query.DatastoreQuery $ 2.apply(DatastoreQuery.java:226)类:com.google.appengine.datanucleus.query.DatastoreQuery $ 2方法:apply Line:226Source:com.goog le.appengine.datanucleus.query.LazyResult.resolveNext(LazyResult.java:96)类:com.google.appengine.datanucleus.query.LazyResult方法:resolveNext行:96Source:com.google.appengine.datanucleus.query.LazyResult。 resolveAll(LazyResult.java:121)类:com.google.appengine.datanucleus.query.LazyResult方法:resolveAll行:121Source:com.google.appengine.datanucleus.query.LazyResult.size(LazyResult.java:115)类: com.google.appengine.datanucleus.query.LazyResult方法:size行:115Source:com.google.appengine.datanucleus.query.StreamingQueryResult.size(StreamingQueryResult.java:151)类:com.google.appengine.datanucleus.query。 StreamingQueryResult方法:size行:151Source:org.datanucleus.store.query.AbstractQueryResult.toArray(AbstractQueryResult.java:400)类:org.datanucleus.store.query.AbstractQueryResult方法:toArray Line:400Source:java.util.ArrayList。 addAll(ArrayList.java:530)类:java.util.ArrayList方法:addAll Line:530Source:org.datanucleus.query。 evaluateator.JavaQueryEvaluator.execute(JavaQueryEvaluator.java:160)类:org.datanucleus.query.evaluator.JavaQueryEvaluator方法:执行Line:160Source:org.datanucleus.query.evaluator.JPQLEvaluator.execute(JPQLEvaluator.java:112)类: org.datanucleus.query.evaluator.JPQLEvaluator方法:执行Line:112Source:com.google.appengine.datanucleus.query.JPQLQuery.performExecute(JPQLQuery.java:200)类:com.google.appengine.datanucleus.query.JPQLQuery方法:performExecute Line:200Source:org.datanucleus.store.query.Query.executeQuery(Query.java:1789)类:org.datanucleus.store.query.Query方法:executeQuery Line:1789Source:org.datanucleus.store.query。 Query.executeWithMap(Query.java:1693)类:org.datanucleus.store.query.Query方法:executeWithMap行:1693Source:org.datanucleus.api.jpa.JPAQuery.getSingleResult(JPAQuery.java:232)类:org。 datanucleus.api.jpa.JPAQuery方法:getSingleResult行:232Source:com.example.backend.DBbackend.findTechByName(DBbackend.jav a:124)类:com.example.backend.DBbackend方法:findTechByName行:124Source:com.example.backend.ServerBackend.findTechByName(ServerBackend.java:101)类:com.example.backend.ServerBackend方法:findTechByName行: 101Source:org.apache.jsp.addCall_jsp._jspService(addCall_jsp.java:81)类:org.apache.jsp.addCall_jsp方法:_jspService行:81Source:org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java: 97)类:org.apache.jasper.runtime.HttpJspBase方法:service行:97Source:javax.servlet.http.HttpServlet.service(HttpServlet.java:717)类:javax.servlet.http.HttpServlet方法:service line: 717Source:org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)类:org.apache.jasper.servlet.JspServletWrapper方法:service行:377Source:org.apache.jasper.servlet.JspServlet.serviceJspFile( JspServlet.java:313)类:org.apache.jasper.servlet.JspServlet方法:serviceJspFile行:313Source:org.apache.jasper.servlet.JspServle t.service(JspServlet.java:260)类:org.apache.jasper.servlet.JspServlet方法:service行:260Source:com.google.appengine.tools.development.PrivilegedJspServlet.access $ 101(PrivilegedJspServlet.java:23)类:com.google.appengine.tools.development.PrivilegedJspServlet方法:访问$ 101行:23Source:com.google.appengine.tools.development.PrivilegedJspServlet $ 2.run(PrivilegedJspServlet.java:61)类:com.google.appengine.tools .development.PrivilegedJspServlet $ 2方法:run Line:61Source:java.security.AccessController.doPrivileged(Native Method)类:java.security.AccessController方法:doPrivileged Line:-2Source:com.google.appengine.tools.development.PrivilegedJspServlet。 service(PrivilegedJspServlet.java:58)类:com.google.appengine.tools.development.PrivilegedJspServlet方法:service行:58Source:javax.servlet.http.HttpServlet.service(HttpServlet.java:717)类:javax.servlet。 http.HttpServlet方法:service line:717Source:org.mortbay.jetty.servlet.Servlet Holder.handle(ServletHolder.java:511)类:org.mortbay.jetty.servlet.ServletHolder方法:handle行:511Source:org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1166)类: org.mortbay.jetty.servlet.ServletHandler $ CachedChain方法:doFilter Line:1166Source:com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)类:com.google.appengine.api。 socket.dev.DevSocketFilter方法:doFilter行:74Source:org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)类:org.mortbay.jetty.servlet.ServletHandler $ CachedChain方法:doFilter Line: 1157Source:com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)类:com.google.appengine.tools.development.ResponseRewriterFilter方法:doFilter Line:123Source:org.mortbay.jetty.servlet。 ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)类:org.mortbay.jetty.servlet.ServletHand ler $ CachedChain方法:doFilter Line:1157Source:com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)类:com.google.appengine.tools.development.HeaderVerificationFilter方法:doFilter Line:34Source: org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)类:org.mortbay.jetty.servlet.ServletHandler $ CachedChain方法:doFilter Line:1157Source:com.google.appengine.api.blobstore。 dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)类:com.google.appengine.api.blobstore.dev.ServeBlobFilter方法:doFilter行:63Source:org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler。 java:1157)类:org.mortbay.jetty.servlet.ServletHandler $ CachedChain方法:doFilter行:1157Source:com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)类:com.google。 apphosting.utils.servlet.TransactionCleanupFilter方法:doFilter行:43Source:org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)类:org.mortbay.jetty.servlet.ServletHandler $ CachedChain方法:doFilter Line:1157Source:com.google.appengine。 tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)类:com.google.appengine.tools.development.StaticFileFilter方法:doFilter行:125Source:org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler。 java:1157)类:org.mortbay.jetty.servlet.ServletHandler $ CachedChain方法:doFilter行:1157Source:com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:368)类:com.google。 appengine.tools.development.DevAppServerModulesFilter方法:doDirectRequest行:368Source:com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:351)类:com.google.appengine.tools.development.DevAppServerModulesFilter Met hod:doDirectModuleRequest行:351Source:com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)类:com.google.appengine.tools.development.DevAppServerModulesFilter方法:doFilter行:116Source:org.mortbay .jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157)类:org.mortbay.jetty.servlet.ServletHandler $ CachedChain方法:doFilter行:1157Source:org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler .java:388)类:org.mortbay.jetty.servlet.ServletHandler方法:handle Line:388Source:org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)类:org.mortbay.jetty.security .SecurityHandler方法:handle Line:216Source:org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)类:org.mortbay.jetty.servlet.SessionHandler方法:handle Line:182Source:org.mortbay.jetty .handler.ContextHandler.handle(ContextHandler.java:765)类:org.mortbay.j etty.handler.ContextHandler方法:handle Line:765Source:org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)类:org.mortbay.jetty.webapp.WebAppContext方法:handle Line:418Source:com。 google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:97)类:com.google.appengine.tools.development.DevAppEngineWebAppContext方法:handle Line:97Source:org.mortbay.jetty.handler.HandlerWrapper.handle( HandlerWrapper.java:152)类:org.mortbay.jetty.handler.HandlerWrapper方法:handle Line:152Source:com.google.appengine.tools.development.JettyContainerService $ ApiProxyHandler.handle(JettyContainerService.java:485)Class:com。 google.appengine.tools.development.JettyContainerService $ ApiProxyHandler方法:handle Line:485Source:org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)类:org.mortbay.jetty.handler.HandlerWrapper方法:handle行:152Source:org.mortbay.jetty.Server.handle(Server.java: 326)类:org.mortbay.jetty.Server方法:handle行:326Source:org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)类:org.mortbay.jetty.HttpConnection方法:handleRequest行:542Source: org.mortbay.jetty.HttpConnection $ RequestHandler.headerComplete(HttpConnection.java:923)类:org.mortbay.jetty.HttpConnection $ RequestHandler方法:headerComplete行:923Source:org.mortbay.jetty.HttpParser.parseNext(HttpParser.java: 547)类:org.mortbay.jetty.HttpParser方法:parseNext行:547Source:org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)类:org.mortbay.jetty.HttpParser方法:parseAvailable行:212Source: org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)类:org.mortbay.jetty.HttpConnection方法:handle Line:404Source:org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)类:org.mortbay.io.nio.SelectChannelEndPoint方法:运行Line:409Source:org.mortbay.thread.QueuedThreadPoo l $ PoolThread.run(QueuedThreadPool.java:582)类:org.mortbay.thread.QueuedThreadPool $ PoolThread方法:运行Line:582}

我无法从em中选择任何内容!!!

1 个答案:

答案 0 :(得分:0)

只是一个提示,当你有一个可以为null的变量时,使用它作为参数,而不是调用方法。

如果你使用。在变量为null之后的运算符,您将获得null异常。例如:

 if(  variable_could_be_null.equals(variable_known_to_not_be_null) )

最终会在某些时候给你一个null异常。但这不会:

 if(  variable_known_to_not_be_null.equals(variable_could_be_null) )

因为在这里你总是使用。定义了您知道的变量的运算符。

在使用可能为null的值之前,请检查它:

if(variable_could_be_null!=null)
{
  ...
}