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

2017/12/3

jtab- first, next,previous last button

divinity divinity

2017/12/3

#
hi pple I am practicing on this java gui interface, where it will hve to go from the first, next previous or last record. right now I am working on the next button but run into an runtime error.can some tell me what to do and how to fix it. here is the codes, where in the codes did i went wary
public class jtab extends javax.swing.JFrame {

    Connection conn;
    ResultSet rs;
    Statement stmt;
    
    
    public jtab() {
        initComponents();
        DoConnect();
        
    }
    public void DoConnect(){
        
        try{
            
            //connect to the database
            String host ="jdbc:JTab://localhost:3306/lecturer";
            String uName = "admin";
            String uPass ="admin";
            
            //execute some sql and load the record into the result set
            stmt =  conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
            String sql =  "SELECT * FROM jtab";
            rs = stmt.executeQuery(sql);
            
            //move the cursor the first record and get the data
            rs.next();
            int studentID = rs.getInt("studentID");
            String ID = Integer.toString(studentID);
            String date = rs.getString("date");
            String coursecrn = rs.getString("course_crn");
            String coursecode = rs.getString("course_code");
            String subject = rs.getString("subject");
            
            //display the first record in the field
            txtfieldstudentID.setText(ID);
            txtfieldDate.setText(date);
            txtfieldcourse_crn.setText(coursecrn);
            txtfieldcourse_code.setText(coursecode);
            txtfieldsubject.setText(subject);
            
        }
        catch(SQLException err){
            JOptionPane.showMessageDialog(jtab.this, err.getMessage());
        }
    }
this is the codes ah used for the next button
 private void btnnextActionPerformed(java.awt.event.ActionEvent evt) {                                        
        try{
            if(rs.next()){
                
            }
            else{
                
                rs.previous();
                 JOptionPane.showMessageDialog(jtab.this,"End of File");
            }
           
        }catch(SQLException err){
              
            JOptionPane.showMessageDialog(jtab.this,err.getMessage());
        }
        
        try{
            
            if(rs.next()){
                int studentID = rs.getInt("studentID");
                String ID = Integer.toString(studentID);
                String date = rs.getString("Date");
                String coursecrn = rs.getString("coursecrn");
                String coursecode = rs.getString("coursecode");
                String subject = rs.getString("subject");
                
                txtfieldstudentID.setText(ID);
                txtfieldDate.setText(date);
                txtfieldcourse_crn.setText(coursecrn);
                txtfieldcourse_code.setText(coursecode);
                txtfieldsubject.setText(subject);
                
            }
            else{
                
                rs.previous();
                JOptionPane.showMessageDialog(jtab.this, "End of File");
            }
        }
        catch(SQLException err){
            JOptionPane.showMessageDialog(jtab.this, err.getMessage());
        }
    }                                       
   
divinity divinity

2017/12/3

#
the error is on line 23, line 10 and this line
     new jtab().setVisible(true);
divinity divinity

2017/12/3

#
this is the error ah get
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at jtab.jtab.DoConnect(jtab.java:46)
	at jtab.jtab.<init>(jtab.java:33)
	at jtab.jtab$9.run(jtab.java:449)
	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 STOPPED (total time: 48 minutes 58 seconds)
divinity divinity

2017/12/3

#
line 1 error, what does that mean
danpost danpost

2017/12/3

#
divinity wrote...
line 1 error, what does that mean
I do not see where 'conn' is being set any object to it (its value remains 'null', which you cannot execute any method on -- hence the error).
You need to login to post a reply.