Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Null Pointer Exception in JDBC

843859Jan 19 2008 — edited Nov 20 2014
Hello friends,
I want to retrive data from my database through SQL but Iam facing a probem with my code, a runtime error Named "Null Pointer Exception occured" while retrieving data from database, using resultset,
the snapshot of my code is as follows:----
        String str = null;
        PreparedStatement pstmt = null;
        Statement stat = null;
        ResultSet result = null;
        Connection con = null;
        
        String name = null;
        String dat = null;
        String mont = null;
        String year = null;
        try
        {
            str="SELECT * FROM date_of_birth WHERE id = ?";
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:myData","sa","12345678");
            stat = con.createStatement();
            pstmt.setInt(1,1);
            pstmt = con.prepareStatement(str);
            result = pstmt.executeQuery();
            while(result.next())
            {
                name = result.getString("nam");
                dat = result.getNString("dat");
                mont = result.getNString("mont");
                year = result.getString("yer");
            }
            jLabel2.setText(name);
            jLabel4.setText(dat);
            jLabel6.setText(mont);
            jLabel8.setText(year);
            stat.close();
            pstmt.close();
            result.close();
            con.close();
        }
        catch(Exception e)
        {
            System.out.println("Error Occurred "+e.toString());
        }        
        initComponents();
    }
In this code I got null pointer runtime exception, when I use resultset querry......
So, it would be kind enough for me to solve this problem, Iam using Windows XP Service Pack 2 and SQL Server 2005 Developer Edition Database
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 17 2008
Added on Jan 19 2008
14 comments
202 views