java.lang.NullPointerException BigDecimal

时间:2015-03-04 17:04:40

标签: java swing bigdecimal jformattedtextfield

我在java项目中工作。

这是Article(Artikujt)类 emri =名字, cmimi =价格;

import java.math.BigDecimal;
import java.sql.*;

public class Artikujt {
   Connection conn;
   String emri;
   BigDecimal cmimi;
   int kat_id;

public String getEmri() {
    return emri;
}

public void setEmri(String emri) {
    this.emri = emri;
}

public BigDecimal getCmimi() {
    return cmimi;
}

public void setCmimi(BigDecimal cmimi) {
    this.cmimi = cmimi;
}

public int getKat_id() {
    return kat_id;
}

public void setKat_id(int kat_id) {
    this.kat_id = kat_id;
}

public boolean insertData(){
    boolean result = false;
    try {
        String sql = "INSERT INTO dbo.Artikujt(emri,cmimi,kategoria_id) VALUES(?, ?, ?)";
        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setString(1, emri);
        ps.setBigDecimal(2, cmimi);
        ps.setInt(3, kat_id);

        if(!ps.execute()){
            result = true;
        }
        ps.close();
        conn.close();

    }catch(SQLException e){
        e.getStackTrace();
    }
    return result;
}

}

这是在jFrame类中的insertData()方法。

public void insertData(){
try {
 Artikujt a = new Artikujt();
 msgError.setText("");
 String emri = emriField.getText();
 DefaultTableModel model = (DefaultTableModel) a_table.getModel();


    if(!emriField.getText().trim().equals("")){
        int i = 4;
        String cmimi = cmimiField.getText();
        BigDecimal bigValue = new BigDecimal(cmimi);      

        System.out.println(bigValue);

        a.setEmri(emriField.getText());
        a.setCmimi(bigValue);
        a.setKat_id(i);


        if(a.insertData()){
            msgError.setText(emri + " eshte regjistruar me sukses.");
            conn.close();
        }else {
            msgError.setText("Ju lutem provoni perseri.");
            conn.close();
        }
    }else {
        msgError.setText("Ju lutem plotesoni te gjitha fushat.");
        conn.close();
    }
}catch(SQLException se){
    se.getMessage();
}
}

因此,当我填写所有字段并单击添加按钮时,我得到此nullpointExcption:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Interface.Artikujt.insertData(Artikujt.java:49)
at View.ArtikujtFrame.insertData(ArtikujtFrame.java:183)
at View.ArtikujtFrame.jButton2ActionPerformed(ArtikujtFrame.java:223)
at View.ArtikujtFrame.access$300(ArtikujtFrame.java:26)
at View.ArtikujtFrame$4.actionPerformed(ArtikujtFrame.java:119)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
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:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
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)

有人可以帮助我,我认为当我尝试将数据从jframe放到sql时出现错误,尤其是cmimi(BigDecimal)。

谢谢你, em7wo。

0 个答案:

没有答案