当前位置: 首页 > 知识库问答 >
问题:

在访问另一个类java中的方法时出错[重复]

朱岳
2023-03-14

我犯了这样的错误

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)
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);
  }
}
    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                   
}
/*
 * 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                   
}

共有1个答案

吕皓
2023-03-14

堆栈跟踪声明NullPointerException抛出在tugasakhir.home.TambahActionPerformed(Home.java:285),因此错误出现在以下一行中:

    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);
        
    }                                      

就在第285行,但由于我不知道是哪一行,我需要知道哪一行是文件中的第285行。

 类似资料:
  • 问题内容: 我试图尽可能简化我的困境。我有三节课: Α: Beta: 主要: 我希望我不要过分简化。我的问题是如何从Alpha中的方法访问cBeta.DoSomethingBeta()? 问题答案: 你需要以某种方式为Alpha类提供对cBeta的引用。有三种方法可以做到这一点。 1)在构造函数中为Alphas提供Beta。在Alpha课堂上写道: 并从main()调用cAlpha = new A

  • 我是OOPs新手,如果这听起来很基本,我很抱歉。我有一个具有以下代码的类:- 现在假设我在同一个包中有另一个类“Main”。有什么方法我可以创建一个类的对象'内部'在'主'与类'外部'参考? 我尝试了以下操作(这是抛出错误):- “Main”和“Outer”类都在同一个包中。另外,我的主要目标是了解是否有任何方法可以在main方法中创建类Inner的对象,如下所示:-outerO1=newinne

  • 我试图理解变量的范围,以及如何使用从一个类初始化的变量,并在另一个类中使用它们,但接下来我的理解就卡住了。 我有一个类Test1,定义如下: 我在我的中调用这个类: 我有另一个类Test2: 如果随后在main中调用该方法,则值val将显示为0,而不是33。如何在另一个类中访问内存中的值? 谢谢

  • 问题内容: 您好我是Java的初学者,这是我的问题:我有带有以下变量的第一堂课: 我也有这堂课: 我的问题是:我想将number1和number2变量存储到ArrayList中,然后从类test中访问此ArrayList。我怎样才能做到这一点? 问题答案: 和测试类:

  • 我想在另一个类中使用那个擦除方法,但由于它看起来不是静态的,所以我不能这样做 那么我如何访问那个方法呢?

  • 想改进这个问题吗?通过编辑这篇文章添加细节并澄清问题。 我有A和B两个班: 正如您在类B中看到的,我有一个方法登录名,我在其中放入了两个变量。我想访问类A中这些变量的值。我怎么做? PS:我正在使用JAVA8,我是个新手。