Index: trunk/modules/luni/src/main/java/java/net/Socket.java =================================================================== --- trunk/modules/luni/src/main/java/java/net/Socket.java (revision 430284) +++ trunk/modules/luni/src/main/java/java/net/Socket.java (working copy) @@ -548,9 +548,11 @@ * if an error occurs setting the option */ public void setKeepAlive(boolean value) throws SocketException { - checkClosedAndCreate(true); - impl.setOption(SocketOptions.SO_KEEPALIVE, value ? Boolean.TRUE - : Boolean.FALSE); + if (impl != null) { + checkClosedAndCreate(true); + impl.setOption(SocketOptions.SO_KEEPALIVE, value ? Boolean.TRUE + : Boolean.FALSE); + } } // static native int getSocketFlags(); Index: trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java =================================================================== --- trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java (revision 430284) +++ trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java (working copy) @@ -29,6 +29,7 @@ import java.net.Socket; import java.net.SocketAddress; import java.net.SocketException; +import java.net.SocketImpl; import java.net.SocketTimeoutException; import java.net.UnknownHostException; import java.security.Permission; @@ -620,7 +621,7 @@ /** * @tests java.net.Socket#setKeepAlive(boolean) */ - public void test_setKeepAliveZ() { + public void test_setKeepAliveZ() throws Exception { // There is not really a good test for this as it is there to detect // crashed machines. Just make sure we can set it try { @@ -634,7 +635,14 @@ } catch (Exception e) { handleException(e, SO_KEEPALIVE); } + // regression test for HARMONY-1136 + new testSocket((SocketImpl) null).setKeepAlive(true); } + class testSocket extends Socket { + public testSocket(SocketImpl impl) throws SocketException { + super(impl); + } + } /** * @tests java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory)