IDE:Eclipse Luna SR2 Java版本:8更新45 服务器:Apache Tomcat 8.0.21
我正在Eclipse中创建动态Web应用程序,并为单一分配创建关联的REST和SOAP Web服务。我的问题是关于Web服务的客户端。客户端将要求用户通过在控制台中键入选项号来从控制台中显示的一系列选项中进行选择。然后它将根据所选的选项进行操作。当我尝试第一个选项“查看所有文章”时,它给了我一个空指针异常。完整的详细信息,包括类和控制台输出如下。
控制台输出:
java.lang.NullPointerException
at uts.assignment2.soap.client.NewsClient.main(NewsClient.java:24)
NewsClient.java:
package uts.assignment2.soap.client;
import java.rmi.RemoteException;
import java.util.Scanner;
import javax.xml.rpc.ServiceException;
import au.edu.uts.www._31284.wsd_news.*;
public class NewsClient {
public static void main(String[] args) throws ServiceException,
RemoteException {
NewsSOAPServiceLocator locator = new NewsSOAPServiceLocator();
NewsSOAP service = locator.getNewsSOAPPort();
Scanner scanner = new Scanner(System.in);
for (;;) {
System.out.println("Select an option:");
System.out.println("1: view all articles");
System.out.println("2: delete an article");
System.out.println("3: exit");
System.out.print("enter choice: ");
int option = scanner.nextInt();
switch (option) {
case 1:
try {
for (int i = 0; i < service.getArticles().length; i++) {
Article article = service.getArticle(i);
System.out.println(article);
System.out.println();
//System.out.println(article.getAuthor());
//System.out.println();
//System.out.println(article.getPublishedDate());
//System.out.println();
//System.out.println(article.getShortText());
}
} catch (NullPointerException e) {
e.printStackTrace();
}
break;
case 2:
System.out
.print("Enter the ID of the article you wish to delete: ");
int id = scanner.nextInt();
service.deleteArticle(id);
break;
case 3:
System.exit(0);
break;
}
}
}
}
NewsSOAP.java(服务本身 - 客户端代理由JAX-WS从wsdl自动生成):
package uts.assignment2.soap;
import java.io.IOException;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
import uts.assignment2.*;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.xml.bind.JAXBException;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
@WebService(targetNamespace="http://client.soap.assignment2.uts")
public class NewsSOAP {
@Resource
private WebServiceContext context;
private NewsApplication getNewsApp() throws JAXBException, IOException {
ServletContext application = (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
synchronized (application) {
NewsApplication newsApp = (NewsApplication) application.getAttribute("newsApp");
if (newsApp == null) {
newsApp = new NewsApplication();
newsApp.setFilePath(application.getRealPath("WEB-INF/news.xml"));
application.setAttribute("newsApp", newsApp);
}
return newsApp;
}
}
@WebMethod
public News getArticles() {
News news = null;
try {
news = getNewsApp().getArticles();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return news;
}
@WebMethod
public Article getArticle(int id) {
Article article = null;
try {
article = getNewsApp().getArticles().getArticle(id);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
return article;
}
@WebMethod
public void deleteArticle(int id) {
try {
getNewsApp().getArticles().removeArticle(getNewsApp().getArticles().getArticle(id));
getNewsApp().updateXML();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
System.out.println("The ID entered does not match an article in our records. Please enter a valid ID: ");
}
}
}
我的问题是我无法弄清楚客户端在哪里掉下来。我可以看到NullPointerException指向哪一行,但我无法弄清楚它在哪里落下。任何帮助表示赞赏。如果需要任何额外信息,请询问,我会尽力提供。谢谢。
答案 0 :(得分:0)
请参阅以下代码中的评论。
NewsSOAP.java
@WebMethod
public News getArticles() {
News news = null;
try {
news = getNewsApp().getArticles(); //Recursive call of getArticles() within it's own method. Which in turn setting news=null, causing null pointer exception at service.getArticles().length in for loop.
}
NewsClient.java:
for (int i = 0; i < service.getArticles().length; i++)