本帖最后由 王琪 于 2014-6-8 20:52 编辑
- class Single{
- private Single(){
- }
- <b> public Single getSingle(){
- Single s= new Single();
- return s;
- }</b>
- public void print(){
- System.out.println("Hello word..");
- }
- }
- public class SingleDemo {
- public static void main(String[]args){
- Single d=null;
- <b> d=d.getSingle();
- d.print();</b>
- }
- }
复制代码 为什么会出现
- Exception in thread "main" java.lang.NullPoint
- at SingleDemo.main(SingleDemo.java:15)
复制代码 但是这样就对了
- class Single{
- private Single(){
- }
- public static Single getSingle(){
- Single s= new Single();
- return s;
- }
- public void print(){
- System.out.println("Hello word..");
- }
- }
- public class SingleDemo {
- public static void main(String[]args){
- Single d=null;
- d=d.getSingle();
- d.print();
- }
- }
复制代码 hello word..
|