我正在创建一个简单的应用程序,由于某种原因,我一直得到空指针异常。我的猜测是它与我的session.open在if语句中有关? 我得到的错误是在finally块上,它表示session.close();
<link href="http://freelancezonebd.com/sb-web/3/css/bootstrap.css" rel="stylesheet"/>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-nav-demo" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="#" class="navbar-brand"><span class="glyphicon glyphicon-picture" aria-hidden="true"></span> IMGS</a>
</div>
<div class="collapse navbar-collapse" id="bs-nav-demo">
<ul class="nav navbar-nav">
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Sign Up</a></li>
<li><a href="#">Login</a></li>
</ul>
</div>
</div>
</nav>
....
....
<script src="https://code.jquery.com/jquery-2.2.1.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
错误:
$("#dragme").draggable({
// You still need to set a value for containment
// else it won't be checked when evaluating the position
containment: 'document',
start: function(e, ui) {
// You simply set a left, top, right, bottom coordinates.
// You need to set it on start so if the elements have been
// resized, the containment follows.
var cont = [
$('#topleft').offset().left + $('#topleft').outerWidth(),
$('#toprow').offset().top,
$('#toprow').offset().left + $('#toprow').outerWidth() - ui.helper.width(),
$('#bottomrow').offset().top + $('#bottomrow').outerHeight() - ui.helper.height(),
]
$(this).draggable('instance').containment = cont;
}
});
答案 0 :(得分:1)
该行上的> pollutantmean <- function(directory, pollutant, id = 1:332)
{
sum = 0
length = 0
listCSV <- list.files()[id]
for (i in listCSV)
{
data <- read.csv(i)
no_missing <- data[!is.na(data$pollutant),]
sum = sum + sum(no_missing$pollutant)
length = length + length(no_missing$pollulant)
}
sum/length
}
,堆栈跟踪中没有其他内容,只能表示NullPointerException
为空。据我所知,唯一可能发生的方法是,session
赋值是否永远不会执行 - 这意味着session = sessionFactory.openSession();
或sessionFactory.openSession()
中发生异常,该异常被HibernateUtil.getSessionFactory()
块中引发的后一个异常隐藏。
我希望如果您在调试器中单步调试此代码,您会看到执行流程从这两行中的一行跳转到finally
块,然后在catch
上点击NullPointerException
(因为事务分配从未发生过)并跳转到transaction.rollback()
块,在那里它命中另一个finally
并挽救。
答案 1 :(得分:0)
由于session
和sessionFactory
都在try / catch块之外被初始化为null
,所以永远不要假设它们在finally
子句中不会为空并且调用{{1直接。将其更改为以下内容:
close()