I am trying to use a method coming from a separate class. The problem is that it returns a nullpointerexception.
The original platform of the project file was JDK 12. After knowing that connect/J is dropped in JDK 8, I converted the platform to JDK 1.7. Still, the predicament occurs.
I use mysql-connector-java-5.1.47 to connect to PHPMyAdmin.
Here are the codes that I used:
the main JFrame class that I called the method:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyApps;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import MyLibs.*;
import MyLibs.CarRegFunc.*;
import MyLibs.DataBaseRequiredFunctions.*;
public class carreg extends javax.swing.JFrame {
/**
* Creates new form carreg
*/
public carreg() {
//initComponents();
setTitle("Guide Dead");
crdbrf.createTable(this);
crdbrf.autoID(this);
txtbrand.requestFocus();
crdbrf.tableUpdate(this);
}
CRDataBaseRequiredFunctions crdbrf = new CRDataBaseRequiredFunctions();
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
Generated Code
private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
crdbrf.selectRow(this);
}
/**
* @param args the command line arguments
*/
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(carreg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(carreg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(carreg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(carreg.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 carreg().setVisible(true);
}
});
}
public JTextField getDescr() {
return descr;
}
public JTextField getTxtbrand() {
return txtbrand;
}
public JTextField getTxtmodel() {
return txtmodel;
}
public JTextField getTxtregno() {
return txtregno;
}
public JTextField getTxtrfee() {
return txtrfee;
}
public JComboBox<String> getComavl() {
return comavl;
}
public JTable getJTable1() {
return jTable1;
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> comavl;
private javax.swing.JTextField descr;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField txtbrand;
private javax.swing.JTextField txtmodel;
private javax.swing.JTextField txtregno;
private javax.swing.JTextField txtrfee;
// End of variables declaration
}
the connection class:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyLibs;
import MyApps.carreg;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author MESIAS
*/
public class DataBaseConnect {
Connection con;
public void getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/losrent?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "");
} catch (ClassNotFoundException ex) {
Logger.getLogger(carreg.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(carreg.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
The class that holds the method to be used:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyLibs.DataBaseRequiredFunctions;
import java.sql.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import MyApps.*;
import MyLibs.*;
/**
*
* @author MESIAS
*/
public class CRDataBaseRequiredFunctions implements IDataBaseRequiredFunctions{
Connection con;
PreparedStatement pst;
Statement s;
ResultSet rs;
DataBaseConnect dbc = new DataBaseConnect();
public void autoID(carreg cr){
dbc.getConnection();
try{
s = con.createStatement();
rs = s.executeQuery("select Max(car_no) from carregistration");
rs.next();
rs.getString("Max(car_no)");
if(rs.getString("Max(car_no)") == null){
cr.getTxtregno().setText("CAR001");
}else{
long id = Long.parseLong(rs.getString("Max(car_no)").substring(3,rs.getString("Max(car_no)").length()));
id++;
cr.getTxtregno().setText("CAR" + String.format("%03d", id));
}
}catch(SQLException se){
System.out.println(se.getMessage());
}
}
public void tableUpdate(carreg cr){
int c;
dbc.getConnection();
try{
pst = con.prepareStatement("select * from carregistration");
rs = pst.executeQuery();
ResultSetMetaData rd = rs.getMetaData();
c = rd.getColumnCount();
DefaultTableModel df = (DefaultTableModel)cr.getJTable1().getModel();
df.setRowCount(0);
while(rs.next()){
Vector v2 = new Vector();
for(int i=1; i<=c; i++){
v2.add(rs.getString("car_no"));
v2.add(rs.getString("brand"));
v2.add(rs.getString("model"));
v2.add(rs.getString("rent_fee"));
v2.add(rs.getString("available"));
v2.add(rs.getString("descript"));
}
df.addRow(v2);
}
}catch(SQLException se){
System.out.println(se.getMessage());
}
}
public void createTable(carreg cr){
dbc.getConnection();
try{
DatabaseMetaData md = con.getMetaData();
rs = md.getTables(null, null, "carregistration", null);
if(rs.next() == false){
s = con.createStatement();
String qry ="create table carregistration(id int(11) NOT NULL AUTO_INCREMENT, car_no varchar(255) NOT NULL, brand varchar(255) NOT NULL, model varchar(255) NOT NULL, rent_fee double(12,2) NOT NULL, available varchar(255) NOT NULL, descript varchar(255) NOT NULL, PRIMARY KEY(id))";
s.executeUpdate(qry);
}
}
catch(SQLException se)
{
System.out.println(se.getMessage());
}
}
public void selectRow(carreg cr){
DefaultTableModel d1 = (DefaultTableModel)cr.getJTable1().getModel();
int selectIndex = cr.getJTable1().getSelectedRow();
cr.getTxtregno().setText(d1.getValueAt(selectIndex, 0).toString());
cr.getTxtbrand().setText(d1.getValueAt(selectIndex, 1).toString());
cr.getTxtmodel().setText(d1.getValueAt(selectIndex, 2).toString());
cr.getTxtrfee().setText(d1.getValueAt(selectIndex, 3).toString());
cr.getComavl().setSelectedItem(d1.getValueAt(selectIndex, 4).toString());
cr.getDescr().setText(d1.getValueAt(selectIndex, 5).toString());
}
}
The report after running it states this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MyLibs.DataBaseRequiredFunctions.CRDataBaseRequiredFunctions.createTable(CRDataBaseRequiredFunctions.java:85)
at MyApps.carreg.<init>(carreg.java:31)
at MyApps.carreg$10.run(carreg.java:329)
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: 1 second)
I expect that the program will work but did not and return a nullpointerexception.
UPDATE: as I am browsing the net, it seems to be that the problem is with the DataBaseConnection. It returns null for some reason. Only thing is I still do not know how to remedy this problem.
UPDATE: another few minutes of browsing later, I changed my whole DataBaseConnection class like this:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package MyLibs;
import MyApps.carreg;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author MESIAS
*/
public class DataBaseConnect {
private static Connection con;
public static void createConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/losrent?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC", "root", "");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static Connection getConnection(){
return con;
}
public static void closeConnection(){
if(con!=null){
try {
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
The message still gives out a nullpointerexception but it pertains to other cause than before:
run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MyLibs.DataBaseRequiredFunctions.CRDataBaseRequiredFunctions.autoID(CRDataBaseRequiredFunctions.java:40)
at MyApps.carreg.<init>(carreg.java:32)
at MyApps.carreg$10.run(carreg.java:329)
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: 1 second)
Any help is appreciated!
This is now the cause of why AutoID is returning null:
cr.getTxtregno().setText("CAR" + String.format("%03d", id));
UPDATE: when I quote out the problem, it just continues to go onward. All that is inside the public carreg() is returning nullpointerexception. When I quote it out again, it just continues. Help is appreciated!
UPDATE: It turns out that when I close all the files that I opened with NetBeans, it works. NetBeans always gives out such unique products.