I had Error like this
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at tugasakhir.Home.TambahActionPerformed(Home.java:285)
at tugasakhir.Home.access$300(Home.java:14)
at tugasakhir.Home$4.actionPerformed(Home.java:97)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at org.GNOME.Accessibility.AtkWrapper$6.dispatchEvent(AtkWrapper.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
and this is my code
TugasAkhir.java
package tugasakhir;
public class TugasAkhir {
public static Home hm=new Home();
public static DataCore dt=new DataCore();
public static void main(String[] args) {
hm.setVisible(true);
hm.pack();
hm.setLocationRelativeTo(null);
hm.setDefaultCloseOperation(Home.EXIT_ON_CLOSE);
dt.setVisible(true);
dt.pack();
dt.setLocationRelativeTo(null);
dt.setDefaultCloseOperation(DataCore.EXIT_ON_CLOSE);
dt.setState(1);
}
}
DataCore.java
Home hm=TugasAkhir.hm;
public int SearchTable(int j,String query){
for (int k = 0; k < Tables.getRowCount(); k++) {
if(Pattern.matches(".*"+query+".*", (String)Tables.getValueAt(k, j))){
System.out.println("Row: "+k+", Col: "+j);
return k;
}
}
return 0;
}
public void check(){
System.out.print(Tables.getValueAt(0, 1));
}
public String GetValue(int j, int k){
return (String)Tables.getValueAt(j, k);
}
public int GetInt(int j, int k){
return (Integer)Tables.getValueAt(j, k);
}
public void SetValue(int value, int j, int k){
Tables.setValueAt(value, j, k);
}
public void tambahini(String Nama, int jumlah){
int harga = 121;
javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) hm.Tabl.getModel();
model.addRow(new Object[]{Nama, harga, jumlah, harga * jumlah});
}
private void TambahActionPerformed(java.awt.event.ActionEvent evt) {
javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) Tables.getModel();
String nama = Nama.getText();
Integer jumlah = Integer.valueOf(Jumlah.getText());
Integer harga = Integer.valueOf(Harga.getText());
model.addRow(new Object[]{nama, harga, jumlah,});
}
private void KembaliActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Home hm = TugasAkhir.hm;
hm.setState(0);
this.setState(1);
}
private void HapusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) Tables.getModel();
model.removeRow(Tables.getSelectedRow());
}
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(DataCore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DataCore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DataCore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DataCore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DataCore().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Hapus;
private javax.swing.JTextField Harga;
private javax.swing.JTextField Jumlah;
private javax.swing.JButton Kembali;
private javax.swing.JTextField Nama;
public javax.swing.JTable Tables;
private javax.swing.JButton Tambah;
private javax.swing.JButton Ubah;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}
Home.java
/*
* 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 tugasakhir;
import java.util.regex.Pattern;
public class Home extends javax.swing.JFrame {
DataCore dc = TugasAkhir.dt;
public void addItemsinTable(String nama, int harga, int jumlah){
javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) this.Tabl.getModel();
model.addRow(new Object[]{nama, harga, jumlah, harga * jumlah});
}
private void TambahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
//Inisialisasi Variable\
String nama = Nama.getText();
int jumlah = Integer.valueOf(Jumlah.getText());
//Kirim data
dc.tambahini(nama,jumlah);
}
private void BersihActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Nama.setText("");
Jumlah.setText("");
Total.setText("");
Bayar.setText("");
}
private void LihatActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
DataCore dt = TugasAkhir.dt;
dt.setState(0);
this.setState(1);
}
private void OKActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
dc.check();
}
private void HapusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) Tabl.getModel();
model.removeRow(Tabl.getSelectedRow());
}
private void jScrollPane1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
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(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Home.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 Home().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField Bayar;
private javax.swing.JButton Bersih;
private javax.swing.JButton Hapus;
private javax.swing.JTextField Jumlah;
private javax.swing.JButton Lihat;
private javax.swing.JTextField Nama;
private javax.swing.JButton OK;
public javax.swing.JTable Tabl;
private javax.swing.JButton Tambah;
private javax.swing.JTextField Total;
private javax.swing.JButton Ubah;
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.JScrollPane jScrollPane1;
private java.awt.Menu menu1;
private java.awt.Menu menu2;
private java.awt.PopupMenu popupMenu1;
// End of variables declaration
}
i always have error when using method in another class i had try to get some values in jtable and send it to another class sorry this is my first question in there and i dont know it have limited string so i cut code from netbeans jframe Thanks