I am trying to take a value from MSSQL to a class. In my login GUI frame the variable passes exactly right, but when i pass that class object to another frame the name (String ograd) variable returns as null.
Here’s the teachermanagement.java file:
package Model; public class teachermanagement { private int ogrID; String ograd, ogrsoyad; public teachermanagement(String ograd, String ogrsoyad) { this.ograd = ograd; this.ogrsoyad = ogrsoyad; } public int getogrID() { return ogrID; } public void setogrID(int ogrID) { this.ogrID = ogrID; } public String getograd() { return ograd; } public void setograd(String ograd) { this.ograd = ograd; } public String getogrsoyad() { return ogrsoyad; } public void setogrsoyad(String ogrsoyad) { this.ogrsoyad = ogrsoyad; } }
loginGUI.java file where i take variables from my MSSQL database with Button click, which works fine.
package View; import Helper.dbconnection; import Model.teachermanagement; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; public class logingui extends javax.swing.JFrame { private dbconnection conn = new dbconnection(); public logingui() { initComponents(); } @SuppressWarnings("unchecked") Generated Code private void ogretmenadActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if(ogretmenad.getText().length() == 0 | ogretmenpw.getText().length() == 0) JOptionPane.showMessageDialog(null, "Lütfen tüm alanları doldurunuz.", "Uyarı", JOptionPane.INFORMATION_MESSAGE); else { try { Connection con = conn.DBConn(); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT * from ogretmen"); while(rs.next()) { if(ogretmenad.getText().equals(rs.getString("ograd")) && ogretmenpw.getText().equals(rs.getString("ogrsifre"))) { teachermanagement tm = new teachermanagement(); tm.setogrID(rs.getInt("ogrID")); tm.setograd(rs.getString("ograd")); tm.setogrsoyad(rs.getString("ogrsoyad")); //THIS IS WHERE I TRY TO PASS NAME VARIABLE TO ANOTHER FRAME ogretmengui tgui = new ogretmengui(tm); tgui.setVisible(true); dispose(); } } } catch (SQLException ex) { System.out.println(ex); } } } public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(logingui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(logingui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(logingui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(logingui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new logingui().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTextField ogretmenad; private javax.swing.JPasswordField ogretmenpw; // End of variables declaration }
dbconnection.java in Helper package:
package Helper; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; public class dbconnection { Connection c = null; public dbconnection() {} public Connection DBConn() { try { this.c = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; databaseName=schoolms", "sa", "1234"); return c; } catch (SQLException ex) { Logger.getLogger(dbconnection.class.getName()).log(Level.SEVERE, null, ex); } return c; } }
and this is the teacherGUI frame i am trying to pass my variable to:
package View; import Model.teachermanagement; public class ogretmengui extends javax.swing.JFrame { static teachermanagement ogr = new teachermanagement(); public ogretmengui(teachermanagement ogr) { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("OKUL YÖNETİMİ SİSTEMİ"); setResizable(false); jLabel1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N jLabel1.setText("Hoşgeldiniz, " + ogr.getograd()); jButton1.setText("Çıkış Yap"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE) .addGap(18, 18, 18) .addComponent(jButton1) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(262, Short.MAX_VALUE)) ); pack(); setLocationRelativeTo(null); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { dispose(); } public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(ogretmengui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ogretmengui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ogretmengui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ogretmengui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { ogretmengui frame = new ogretmengui(ogr); frame.setVisible(true); } catch (Exception ex) { System.out.println(ex); } } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; // End of variables declaration }
As you can see in teacherGUI.java I am trying to change label text with the variable i took from loginGUI frame, but it returns as NULL. When i try to print variable in loginGUI it returns the correct value, but in teacherGUI frame this is not the case. Any help would be appreciated.
PS: I hid loginGUI innit components to shorten the code because there is nothing wrong with that file.
Advertisement
Answer
You’re creating too many teachermanagement objects and ignoring the parameter passed into the constructor:
Code 1:
// create a new teachermanagement object here teachermanagement tm = new teachermanagement(); // ..... // and pass it into a constructor here: ogretmengui tgui = new ogretmengui(tm); tgui.setVisible(true); dispose();
but you ignore the tm object in the constructor:
public class ogretmengui extends javax.swing.JFrame { static teachermanagement ogr = new teachermanagement(); // object 1 // ogr passed into the constructor is object 2, and is ignored public ogretmengui(teachermanagement ogr) { initComponents(); }
Instead you should not create a new teachermanagement in the ogretmengui class, but rather use the parameter to set a field:
public class ogretmengui extends javax.swing.JFrame { // should not be static // static teachermanagement ogr = new teachermanagement(); // object 1 private teachermanagement ogr; // should not be static and should not be initialized // ogr passed into the constructor and used to set the variable: public ogretmengui(teachermanagement ogr) { this.ogr = ogr; // ***** here ***** initComponents(); }
You may also be running into problems in that JFrames are not modal and do not block program flow (such as a JOptionPane or modal JDialog will). Please have a look at The Use of Multiple JFrames: Good or Bad Practice?.
Also, while not related to your question or problem, consider having a look at this Java Naming Conventions document: Class names should start with upper-case letters, variables with lower-case letters. Again, this has nothing to do with your actual question or problem, but by following these conventions, you will make your code easier for others (us) to understand, and that is usually a good thing.