Exception in thread "main" java.lang.NullPointerException怎么解决

xuyanwen605 2011-08-01 01:43:10
在以前的webservice中添加新的接口 项目是使用spring注解来实现依赖注入的, 写好方法后 在main方法中测试报错,
其他类都很正常, new的类调用时总是报空指针。。 注入哪里有问题 谢谢 大神们。。。


类代码。
@Component("muB2CDSG")
public class MUB2CDSGClientImpl implements MUB2CDSGClient {
@Autowired
private ParameterService parameterService;

@Autowired
private CustomerService customerService;

@Autowired
private TmsContextInfo tmsContextInfo;public void init()
{
if(userId == 0){
userId = customerService.getUserId();
}
if(salesChannel == null || salesChannel.equals("")){
salesChannel = customerService.getSalesChannel();
}
if(usm == null){
try {
String uniHost = tmsContextInfo.getUniHost();
wsUrl = uniHost + SALE_WS_NAME;
URL url = new URL(wsUrl);

usm = new UniDsgManagerImplServiceLocator().getUniDsgManagerImplPort(url);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
}

@Override
public UniDsgResult queryDsgInfo(String flightNo, String cabCode,
String date) throws TMSFirstException {
init();

UniDsgReq req=new UniDsgReq();
req.setSaleChannel(salesChannel);
req.setUserId(String.valueOf(userId));
req.setFlightNo(flightNo);
req.setCityPair(null);
req.setCabCode(cabCode);

try {
UniDsgResult result=usm.displaySegment(req);
logger.info("MsgCode : " + result.getMsgCode());
logger.info("MsgDesc : " + result.getMsgDesc());

return result;
} catch (RemoteException e) {
e.printStackTrace();
}

return null;
}




main方法
	public static void main(String[] args) {

try {
UniDsgResult result=new UniDsgResult();
result=dsgService.queryDsgInfo("MU5710", "y", "20110729");
System.out.println(result.getMsgCode());
System.out.println(result.getMsgDesc());

UniDsgSegment segment=result.getSegments(0);
System.out.println(segment.getCabin());
System.out.println(segment.getFlight());
System.out.println(segment.getStops());
for(int i=0;i<segment.getDstcity().length;i++){
System.out.println(segment.getDstcity(i));
}

System.out.println("***************");

for(int i=0;i<segment.getOrgcity().length;i++){
System.out.println(segment.getOrgcity(i));
}

// System.out.println(result.g());
System.out.println("***************");
} catch (TMSFirstException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
...全文
412 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
binggan90du 2011-08-15
  • 打赏
  • 举报
回复
应该是配置文件的问题,检查一下你的配置
fxhbeyond 2011-08-15
  • 打赏
  • 举报
回复
引用错了 我想说8L的才是正解[Quote=引用 9 楼 fxhbeyond 的回复:]
正解

引用 7 楼 xuyanwen605 的回复:
引用 6 楼 ilrxx 的回复:

spring的这种问题一般都是你没有注入的原因,调试很简单,你看下你新加的接口和以前成功添加的接口有什么不同,肯定是没有注入成功,所以初始化时候内存中没有这个新的对象,你调用时候才空指针
没办法调试。。。 在运行main方法时报了空指针,
Exception in thread "main"……
[/Quote]
fxhbeyond 2011-08-15
  • 打赏
  • 举报
回复
正解[Quote=引用 7 楼 xuyanwen605 的回复:]
引用 6 楼 ilrxx 的回复:

spring的这种问题一般都是你没有注入的原因,调试很简单,你看下你新加的接口和以前成功添加的接口有什么不同,肯定是没有注入成功,所以初始化时候内存中没有这个新的对象,你调用时候才空指针
没办法调试。。。 在运行main方法时报了空指针,
Exception in thread "main" java.lang.NullPointerExcepti……
[/Quote]
JavaMan_KA 2011-08-07
  • 打赏
  • 举报
回复
关键是使用main方法的这个类有没有让spring来管理,如果没有的话,就无法使用spring的@Autowired为该类自动注入。此时就要使用BeanFactory为其手动注入了。我看楼主的问题是出在这个地方吧。
xuyanwen605 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ilrxx 的回复:]

spring的这种问题一般都是你没有注入的原因,调试很简单,你看下你新加的接口和以前成功添加的接口有什么不同,肯定是没有注入成功,所以初始化时候内存中没有这个新的对象,你调用时候才空指针
[/Quote]没办法调试。。。 在运行main方法时报了空指针,
Exception in thread "main" java.lang.NullPointerException
at net.rytong.tms.dao.ws.impl.Test.main(Test.java:22)
上边main方法的22行是 try{
。。。再写别的代码 还是22行报错。。。 是private static DSGService dsgService;报错了么。。
ilrxx 2011-08-01
  • 打赏
  • 举报
回复
spring的这种问题一般都是你没有注入的原因,调试很简单,你看下你新加的接口和以前成功添加的接口有什么不同,肯定是没有注入成功,所以初始化时候内存中没有这个新的对象,你调用时候才空指针
xuyanwen605 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dikeboy1234 的回复:]

用set get试试 字段是private 的 应该没权访问吧
[/Quote]好像不需要getset方法。 其他的类里都没有getset方法。。只是加了@Autowired
贪睡的兔子 2011-08-01
  • 打赏
  • 举报
回复
用set get试试 字段是private 的 应该没权访问吧
xuyanwen605 2011-08-01
  • 打赏
  • 举报
回复
没人么。。。。。。。。。。。。速度来人啊。。。。。。。。。。
xuyanwen605 2011-08-01
  • 打赏
  • 举报
回复
是在以前的项目新加的方法。 其他都很正常 就这个新加的 报错, 是注入的问题吧。。
xuyanwen605 2011-08-01
  • 打赏
  • 举报
回复
少发东西了。 重新发

service代码

@Component("dsgService")
public class DSGServiceImpl implements DSGService {

@Autowired
private MUB2CDSGClient muB2CDSG;

@Override
public UniDsgResult queryDsgInfo(String flightNo, String cabCode,
String date) throws TMSFirstException {

return muB2CDSG.queryDsgInfo(flightNo, cabCode, date);
}



main方法代码
	@Autowired
private static DSGService dsgService;

public static void main(String[] args) {

try {
UniDsgResult result=new UniDsgResult();
result=dsgService.queryDsgInfo("MU5710", "y", "20110729");
System.out.println(result.getMsgCode());
System.out.println(result.getMsgDesc());

UniDsgSegment segment=result.getSegments(0);
System.out.println(segment.getCabin());
System.out.println(segment.getFlight());
System.out.println(segment.getStops());
for(int i=0;i<segment.getDstcity().length;i++){
System.out.println(segment.getDstcity(i));
}

System.out.println("***************");

for(int i=0;i<segment.getOrgcity().length;i++){
System.out.println(segment.getOrgcity(i));
}

// System.out.println(result.g());
System.out.println("***************");
} catch (TMSFirstException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧