This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/10/24

runtime error on database

divinity divinity

2017/10/24

#
hi all am doing this grading system, where I am trying to connect the login to the database but I am getting this runtime error. can someone explain. and how to fix it.
package GradingSystem;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBCon {

    public ResultSet rs;
    public Statement stmt;

     public DBCon(){
         
         try{
             Class.forName("com.mysql.jdbc.Driver");
             Connection con = DriverManager.getConnection("jdbc: mysql: //localhost:3306/gradingsystem", "dbuser","dbpass");
             this.stmt = con.createStatement();
             
         }
         catch(ClassNotFoundException | SQLException e){
             System.out.println(e);
             
         }
     }
     public void testsel(){
         try{
             
             this.rs = stmt.executeQuery("SELECT*FROM student");
             while(this.rs.next()){
                 System.out.println(rs.getInt(1)+ "" + rs.getString(2) + rs.getString(3));
             }
         }
         catch(SQLException ex){
             Logger.getLogger(DBCon.class.getName()).log(Level.SEVERE, null, ex);
         }
     }

}

this is the runtime error msg
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at GradingSystem.DBCon.testsel(DBCon.java:32)
	at GradingSystem.Login$3.run(Login.java:176)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
	at java.awt.EventQueue.access$300(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:706)
	at java.awt.EventQueue$3.run(EventQueue.java:704)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
BUILD SUCCESSFUL (total time: 6 seconds)
divinity divinity

2017/10/24

#
the error is on line 27
danpost danpost

2017/10/24

#
Try inserting the following (just for testing) at line 17 above:
System.out.println("'con' is"+(con == null ? "" : " not")+" null");
and report back with result.
divinity divinity

2017/10/24

#
hi danpost I did as u say but got the same error on the same line
  java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DBCon().testsel();
                new Login().setVisible(true);
            }
        });
divinity divinity

2017/10/24

#
another I got is on line 4, so it is two errors, line 27 and line 4
danpost danpost

2017/10/24

#
divinity wrote...
another I got is on line 4, so it is two errors, line 27 and line 4
It is line 27 that is the issue. You should have a line of output starting with "'con' is ...". How did that line in the output finish up??
You need to login to post a reply.