-1

次のような状況に遭遇しました。任意の文字列と NULL 値の Integer オブジェクトを連結すると、NullPointerException が発生します。

Integer nullInt = null;
String someString = "abc";

System.out.println(someString + "=" + nullInt)

結果として「abc = null」のようなものが期待されますが、代わりに、Java は連結する前に Integer オブジェクトのボックス化を解除しようとします。どうしてこんなことに?

ご協力いただきありがとうございます!

アップデート

これが元のコードです。上記の例で分解しました。

public class SomeClass {

   private String id;
   private String description;
   private Integer someInteger;
   private Date someDate;
   private Date someOtherInteger;

   // ...

   public String toString(){
       return "object [id=" + id
               + ", description=" + description
               + ", someInteger=" + someInteger
               + ", someDate=" + someDate
               + ", someOtherInteger=" + someOtherInteger;
   }

   // ...
}

"id" の値は "description" は null "someInteger" の値は "someDate" は null "someOtherInteger" は null

犯人は「someOtherInteger」です。「、someOtherInteger」+ someOtherIntegerという式を調べるEclipseデバッガーでエラーを再現できます。正確な例外メッセージは次のとおりです。「java.lang.NullPointerException at intValue()」

何か案は?

アップデート

これが実際のコードです。「lastMileage」は NULL です...

@Override
public String toString(){
    return "OwnedCar [vin=" + vin
            + ", description=" + description
            + ", manufactureYear=" + manufactureYear
            + ", purchasedDate=" + purchasedDate
            + ", lastMileage=" + lastMileage
            + ", lastMileageDate=" + lastMileageDate
            + ", milesPerDay=" + milesPerDay
            + ", licencePlateNumber=" + licencePlateNumber
            + ", relationship=" + relationship
            + ", lifeCycleStage=" + lifeCycleStage   
            + ", lastKnownDeliveryStatus=" + lastKnownDeliveryStatus
            + ", lastKnownDeliveryStatusChangeDate=" + lastKnownDeliveryStatusChangeDate
            + ", lastDeliveryStatusCheckDate=" + lastDeliveryStatusCheckDate
            + ", orderCanceledCode=" + orderCanceledCode
            + ", deactivatedIndicator=" + deactivatedIndicator
            + ", deactivationReason=" + deactivationReason
            + ", modelCode=" + modelCode
            + ", model=" + model  
            + ", carAge=" + carAge                
            + ", vinVerifiedDate=" + vinVerifiedDate
            + ", commissionNumber=" + commissionNumber
            + ", commissionNumberYear=" + commissionNumberYear
            + ", ownershipActivationCode=" + ownershipActivationCode
            + ", bid=" + bid
            + ", orderNumber=" + orderNumber
            + ", largePhotoUrl=" + largePhotoUrl
            + ", mediumPhotoUrl=" + mediumPhotoUrl
            + ", smallPhotoUrl=" + smallPhotoUrl
            + ", colourSalesFamily =( "+ colourSalesFamily != null ? colourSalesFamily.toString() : null + ")"
            + "]";
}

そして、ここにスタックトレースがあります:

java.lang.NullPointerException
    at ************.OwnedCar.toString(OwnedCar.java:671)
    at java.lang.String.valueOf(String.java:2826)
    at java.lang.StringBuilder.append(StringBuilder.java:115)
    at com.vw.dbs.******.***SessionAttributeListener.attributeReplaced(***SessionAttributeListener.java:46)
    at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1386)
    at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1291)
    at org.apache.catalina.session.StandardSessionFacade.setAttribute(StandardSessionFacade.java:130)
    at com.liferay.util.servlet.SharedSessionWrapper.setAttribute(SharedSessionWrapper.java:145)
    at com.liferay.portlet.PortletSessionImpl.setAttribute(PortletSessionImpl.java:273)
    at com.liferay.portlet.PortletSessionImpl.setAttribute(PortletSessionImpl.java:260)
    at org.springframework.web.portlet.context.PortletRequestAttributes.setAttribute(PortletRequestAttributes.java:156)
    at org.springframework.web.bind.support.DefaultSessionAttributeStore.storeAttribute(DefaultSessionAttributeStore.java:54)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.updateModelAttributes(HandlerMethodInvoker.java:214)
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:363)
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter.doHandle(AnnotationMethodHandlerAdapter.java:347)
    at org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter.handleRender(AnnotationMethodHandlerAdapter.java:289)
    at org.springframework.web.portlet.DispatcherPortlet.doRenderService(DispatcherPortlet.java:734)
    at org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:522)
    at org.springframework.web.portlet.FrameworkPortlet.doDispatch(FrameworkPortlet.java:470)
    at javax.portlet.GenericPortlet.render(GenericPortlet.java:233)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:101)
    at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:64)
    at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:92)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:638)
    at com.liferay.portlet.InvokerPortletImpl.invokeRender(InvokerPortletImpl.java:723)
    at com.liferay.portlet.InvokerPortletImpl.render(InvokerPortletImpl.java:425)
    at org.apache.jsp.html.portal.render_005fportlet_jsp._jspService(render_005fportlet_jsp.java:1440)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at com.liferay.portal.util.PortalImpl.renderPortlet(PortalImpl.java:3715)
    at com.liferay.portal.util.PortalUtil.renderPortlet(PortalUtil.java:1180)
    at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processPortlet(RuntimePortletUtil.java:160)
    at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processPortlet(RuntimePortletUtil.java:94)
    at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processTemplate(RuntimePortletUtil.java:256)
    at com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil.processTemplate(RuntimePortletUtil.java:181)
    at org.apache.jsp.html.portal.layout.view.portlet_jsp._jspService(portlet_jsp.java:821)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at com.liferay.portal.action.LayoutAction.includeLayoutContent(LayoutAction.java:361)
    at com.liferay.portal.action.LayoutAction.processLayout(LayoutAction.java:618)
    at com.liferay.portal.action.LayoutAction.execute(LayoutAction.java:229)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    at com.liferay.portal.struts.PortalRequestProcessor.process(PortalRequestProcessor.java:152)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at com.liferay.portal.servlet.MainServlet.callParentService(MainServlet.java:518)
    at com.liferay.portal.servlet.MainServlet.service(MainServlet.java:495)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:126)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:126)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.strip.StripFilter.processFilter(StripFilter.java:309)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.themepreview.ThemePreviewFilter.processFilter(ThemePreviewFilter.java:88)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.gzip.GZipFilter.processFilter(GZipFilter.java:137)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.secure.SecureFilter.processFilter(SecureFilter.java:182)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.autologin.AutoLoginFilter.processFilter(AutoLoginFilter.java:254)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    at com.liferay.portal.servlet.FriendlyURLServlet.service(FriendlyURLServlet.java:133)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:126)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:126)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.strip.StripFilter.processFilter(StripFilter.java:261)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.themepreview.ThemePreviewFilter.processFilter(ThemePreviewFilter.java:88)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.gzip.GZipFilter.processFilter(GZipFilter.java:126)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.secure.SecureFilter.processFilter(SecureFilter.java:182)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.i18n.I18nFilter.processFilter(I18nFilter.java:211)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:126)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:126)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:126)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.autologin.AutoLoginFilter.processFilter(AutoLoginFilter.java:254)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    at com.liferay.portal.servlet.I18nServlet.service(I18nServlet.java:101)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    at com.liferay.portal.servlet.filters.virtualhost.VirtualHostFilter.processFilter(VirtualHostFilter.java:310)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:126)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:126)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
    at com.liferay.portal.servlet.filters.threadlocal.ThreadLocalFilter.processFilter(ThreadLocalFilter.java:35)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:123)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:555)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
    at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
    at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:774)
    at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:703)
    at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:896)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
    at java.lang.Thread.run(Thread.java:662)
4

3 に答える 3

5

そうではありません

public static void main(String[] args) {
    Integer nullInt = null;
    String someString = "abc";

    System.out.println(someString + "=" + nullInt);
}

出力:

abc=null
于 2012-04-10T17:23:37.477 に答える
1

JLS 15.18.1.1(オペランドとの文字列連結+):

参照がnullの場合、文字列「null」(4つのASCII文字n、u、l、l)に変換されます。それ以外の場合、変換は、引数なしで参照されるオブジェクトのtoStringメソッドを呼び出すかのように実行されます。ただし、toStringメソッドを呼び出した結果がnullの場合は、代わりに文字列「null」が使用されます。

したがって、次のコード

String a = "abc=" + anInteger;

例外をスローすることはありません(abc=nullただし、書き込む場合があります)。ただし、演​​算を実行すると、次のようになります。

String a = "abc=" + (anInteger + 1)

anInteger開開時の場合は、明らかにRutimeにNPEをスローしnullます。コード全体を投稿しなかったのかもしれません


アップデート

OPは彼の答えを更新し、理由はさらに説明する必要があります

public class Main {

    public static void main(String[] args) {
        Integer i = null;
        System.out.println("throw a NPE " + i != null ? i.toString() : null);
    }
}

三項演算子の前後に括弧がないため、式は次のように評価されます。

System.out.println(("throw a NPE " + i) != null ? i.toString() : null);

最初に文字列連結が適用されるため、「NPE nullをスローする」は呼び出されずnull、呼び出されてNPEがスローされます。i.toString()

于 2012-04-10T19:04:29.817 に答える
1

私はあなたの行を実行し、それは私に書き込みますabc=null.

于 2012-04-10T17:23:36.737 に答える