java 无法在 JSF 中实例化类错误,由 NullPointerException 引起
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13507752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't instantiate class error in JSF, caused by NullPointerException
提问by user1705260
Here is my managed Bean class,
这是我的托管 Bean 类,
public class ChartBean implements Serializable {
private PieChartModel pieModel;
public ChartBean(){
createPieModel();
}
public PieChartModel getPieModel() {
return pieModel;
}
private void createPieModel(){
try {
pieModel = new PieChartModel();
String query = "SELECT b.countryname,count(b.countryname) FROM info.summery a,info.countrymcc b;";
Connector conn = new Connector();
Statement str = (Statement) conn.getConn().createStatement();
ResultSet res = str.executeQuery(query);
while(res.next()){
pieModel.set(res.getString(1), Integer.parseInt(res.getString(2)));
}
} catch (SQLException ex) {
Logger.getLogger(ChartBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
But the problem is when it is compiled it gives an error like this "Cant instantiate class: org.primefaces.examples.view.ChartBean". What is the reason??
但问题是当它被编译时,它会给出这样的错误“无法实例化类:org.primefaces.examples.view.ChartBean”。是什么原因??
StackTrace:
堆栈跟踪:
Caused by: java.lang.NullPointerException at
org.primefaces.examples.view.ChartBean.createPieModel(ChartBean.java:45) at
org.primefaces.examples.view.ChartBean.<init>(ChartBean.java:32) at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorI??
mpl.java:39) at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorA??
ccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at
java.lang.Class.newInstance0(Class.java:355) at java.lang.Class
回答by Stephen C
By a process of elimination, the problem is happening because conn.getConn()
is returning null
. You should be able to simply confirm that the exception occurs at that line (by checking the line number!), and we know that conn
cannot be null
, so it must be result of getConn()
that is null
.
通过消除过程,问题正在发生,因为conn.getConn()
正在返回null
。您应该能够简单地确认异常发生在该行(通过检查行号!),我们知道conn
不可能是null
,所以它必须是结果getConn()
是null
。
That's about as far as I can go without knowing what the Connector
class is and how its getConn()
method works.
在不知道Connector
类是什么以及它的getConn()
方法如何工作的情况下,我可以做到这一点。
For the record, here's how I eliminated other possibilities.
为了记录,以下是我消除其他可能性的方法。
The NPE is being thrown in the createPieModel
call ... and not in some method called from createPieModel
:
NPE 在createPieModel
调用中被抛出......而不是在调用的某些方法中createPieModel
:
1. private void createPieModel(){
2. try {
3. pieModel = new PieChartModel();
4. String query = "SELECT b.countryname,count(b.countryname) FROM info.summery a,info.countrymcc b;";
5. Connector conn = new Connector();
6. Statement str = (Statement) conn.getConn().createStatement();
7. ResultSet res = str.executeQuery(query);
8. while(res.next()){
9. pieModel.set(res.getString(1), Integer.parseInt(res.getString(2)));
10. }
11. } catch (SQLException ex) {
12. Logger.getLogger(ChartBean.class.getName()).log(Level.SEVERE, null, ex);
13. }
14. }
- It cannot be line 3 because any NPE would be thrown in the constructor.
- It cannot be line 4
- It cannot be line 5 - see line 3
- It couldbe line 6
- It cannot be line 7 - because
str
must be non-null (if we get that far) - It cannot be line 8 - because
executeQuery
never returnsnull
- It cannot be line 9 - because
res
andpieModel
must be non-null. - It cannot be line 12 - because nothing there can return a
null
.
- 它不能是第 3 行,因为任何 NPE 都会在构造函数中抛出。
- 它不能是第 4 行
- 它不能是第 5 行 - 见第 3 行
- 这可能是6号线
- 它不能是第 7 行 - 因为
str
必须是非空的(如果我们得到那么远) - 它不能是第 8 行 - 因为
executeQuery
永远不会返回null
- 它不能是第 9 行 - 因为
res
并且pieModel
必须是非空的。 - 它不能是第 12 行 - 因为没有任何东西可以返回
null
.
Hence it can onlyhappen on line 6.
因此它只能发生在第 6 行。