user1705260
user1705260

Reputation: 41

Can't instantiate class error in JSF, caused by NullPointerException

Here is my managed Bean class,

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??

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

Upvotes: 1

Views: 11889

Answers (1)

Stephen C
Stephen C

Reputation: 719551

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.

That's about as far as I can go without knowing what the Connector class is and how its getConn() method works.


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:

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 could be 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 returns null
  • It cannot be line 9 - because res and pieModel must be non-null.
  • It cannot be line 12 - because nothing there can return a null.

Hence it can only happen on line 6.

Upvotes: 2

Related Questions