编写UPnP控制点应用程序。但是我一直得到一个nullpointerexception: 这里的第一个代码是导致npe的原因。以粗体显示,调用createAbsoluteURL。其余的是不同类(URIUtil)中的方法,持有对createAbsoluteURL / URI的不同调用
content = new MediaStoreContent(
context,
**new URLBuilder() {
public String getURL(DIDLObject object) {
return URIUtil.createAbsoluteURL(
addy,
getHttpServerService().getLocalPort(),
URI.create("/" + object.getId())
).toString();
}**
public String getObjectId(String urlPath) {
return urlPath.substring(1); // Cut the slash
}
}
);
public static URL createAbsoluteURL(InetAddress address, int localStreamPort, URI relativeOrNot) throws IllegalArgumentException {
try {
if (address instanceof Inet6Address) {
return createAbsoluteURL(new URL("http://[" + address.getHostAddress() + "]:" + localStreamPort), relativeOrNot);
} else if (address instanceof Inet4Address) {
return createAbsoluteURL(new URL("http://" + address.getHostAddress() + ":" + localStreamPort), relativeOrNot);
} else {
throw new IllegalArgumentException("InetAddress is neither IPv4 nor IPv6: " + address);
}
} catch (Exception ex) {
throw new IllegalArgumentException("Address, port, and URI can not be converted to URL", ex);
}
}
public static URL createAbsoluteURL(URL base, URI relativeOrNot) throws IllegalArgumentException {
if (base == null && !relativeOrNot.isAbsolute()) {
throw new IllegalArgumentException("Base URL is null and given URI is not absolute");
} else if (base == null && relativeOrNot.isAbsolute()) {
try {
return relativeOrNot.toURL();
} catch (Exception ex) {
throw new IllegalArgumentException("Base URL was null and given URI can't be converted to URL");
}
} else {
try {
assert base != null;
URI baseURI = base.toURI();
URI absoluteURI = createAbsoluteURI(baseURI, relativeOrNot);
return absoluteURI.toURL();
} catch (Exception ex) {
throw new IllegalArgumentException(
"Base URL is not an URI, or can't create absolute URI (null?), " +
"or absolute URI can not be converted to URL", ex);
}
}
}
public static URI createAbsoluteURI(URI base, URI relativeOrNot) throws IllegalArgumentException {
if (base == null && !relativeOrNot.isAbsolute()) {
throw new IllegalArgumentException("Base URI is null and given URI is not absolute");
} else if (base == null && relativeOrNot.isAbsolute()) {
return relativeOrNot;
} else {
assert base != null;
// If the given base URI has no path we give it a root path
if (base.getPath().length() == 0) {
try {
base = new URI(base.getScheme(), base.getAuthority(), "/", base.getQuery(), base.getFragment());
} catch (Exception ex) {
throw new IllegalArgumentException(ex);
}
}
return base.resolve(relativeOrNot);
}
}
这是我的日志:
E/AndroidRuntime( 9824): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 9824): at org.teleal.cling.android.browser.MediaServerS
erviceImpl$ContentHttpServerConnection.<init>(MediaServerServiceImpl.java:159)
E/AndroidRuntime( 9824): at org.teleal.cling.android.browser.MediaServerS
erviceImpl.onCreate(MediaServerServiceImpl.java:72)
E/AndroidRuntime( 9824): at android.app.ActivityThread.handleCreateServic
e(ActivityThread.java:2959)
E/AndroidRuntime( 9824): ... 10 more
答案 0 :(得分:1)
至少有两件事可能为空并导致您突出显示的部分中的NPE:
getHttpServerService()
object
在调用该方法之前,您确定您的对象已初始化吗?