Null pointer access: The variable xxx can only be null at this location

在编程过程中遇到一个警告,'Null pointer access: The variable xxx can only be null at this location'。问题出在试图对未初始化的HashMap进行put操作。解决方法是确保在使用HashMap之前正确创建并实例化它。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天写代码发现提示
在这里插入图片描述
代码如下:

private Q_learningRouter get_next_state(Q_learningRouter destination) {
    	HashMap<Q_learningRouter,Double> nextHosts = null;
    	double max1=0;
    	Q_learningRouter next = null; 
    	for(Connection con : getConnections()) {
    		DTNHost other = con.getOtherNode(getHost());
    		Q_learningRouter otherRouter = (Q_learningRouter)other.getRouter();
    		if(otherRouter.isTransferring()) {
    			continue;
    		}else {
    			nextHosts.put(otherRouter, otherRouter.get_max_q_value_to_destination(destination));
    		}
    	}//获得当前连接的router集合
    	if(!nextHosts.isEmpty()) {
    	for(Q_learningRouter key : nextHosts.keySet()) {//此时的router是可以直接传输的router,此时遍历选择最大q_value即可
    		if(key.get_max_q_value_to_destination(destination)>max1) {
    			max1 = key.get_max_q_value_to_destination(destination);
    			next = key;
    		}
    	}
    	}
    	
    	return next;
    }//得到下一状态,通过下一节点返回的q值进行选择;通过当前连接的router集合,通过router集合遍历得到最大q值,获取nextState;

上面的nextHosts提示该warning
原因:HashMap<Q_learningRouter,Double> nextHosts = null;
解决办法: HashMap<Q_learningRouter,Double> nextHosts = new HashMap<Q_learningRouter,Double>();
此时的HashMap并没有创建,所以不能成功put,需要new一个对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值