在frame3
中,我单击按钮时调用frame4
,并将节省的数据值从第3帧传递到第4帧。
当我使用对话框从构造函数打印值时,它将打印准确的值。但是为什么当我尝试在保存时执行添加操作时却得到空指针异常?
// giving accurate value
JOptionPane.showMessageDialog(null, "from frame4 test1: "+savings);
BigDecimal bd= new BigDecimal(2);
savings_new=savings;
savings_new=savings_new.add(bd);
// giving null pointer exception
JOptionPane.showMessageDialog(null, "from frm4 test3: "+savings_new);
Frame4
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
public class Frame4 {
private JFrame frame;
private JLabel eid;
private String id_str3;
static int id_int3;
static BigDecimal savings,savings_new;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame4 window = new Frame4(0, null);
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Frame4(int id,BigDecimal save) {
initialize();
this.id_int3=id;
this.savings=save;
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(200,100,800,550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JOptionPane.showMessageDialog(null, "from frame4 test1: "+savings); // giving accurate value
BigDecimal bd= new BigDecimal(2);
savings_new=savings;
savings_new=savings_new.add(bd);
JOptionPane.showMessageDialog(null, "from frm4 test3: "+savings_new);
id_str3=Integer.toString(id_int3);
eid = new JLabel("");
eid.setFont(new Font("Tahoma", Font.PLAIN, 14));
eid.setBounds(143, 83, 46, 13);
frame.getContentPane().add(eid); //fetching id from frame1
eid.setText(id_str3);
JLabel lblEmployeeId = new JLabel("Employee ID:");
lblEmployeeId.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblEmployeeId.setBounds(43, 79, 83, 23);
frame.getContentPane().add(lblEmployeeId);
JLabel lblDob = new JLabel("Name:");
lblDob.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblDob.setBounds(214, 83, 46, 14);
frame.getContentPane().add(lblDob);
JLabel name = new JLabel("");
name.setFont(new Font("Tahoma", Font.PLAIN, 13));
name.setBounds(280, 83, 88, 14);
frame.getContentPane().add(name);
JLabel lblDob_1 = new JLabel("DOB:");
lblDob_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblDob_1.setBounds(378, 83, 46, 14);
frame.getContentPane().add(lblDob_1);
JLabel dob = new JLabel("");
dob.setFont(new Font("Tahoma", Font.PLAIN, 13));
dob.setBounds(434, 82, 74, 15);
frame.getContentPane().add(dob);
JLabel lblDivision = new JLabel("Division:");
lblDivision.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblDivision.setBounds(535, 83, 66, 14);
frame.getContentPane().add(lblDivision);
JLabel div = new JLabel("");
div.setFont(new Font("Tahoma", Font.PLAIN, 13));
div.setBounds(611, 82, 77, 15);
frame.getContentPane().add(div);
JSeparator separator = new JSeparator();
separator.setBounds(43, 128, 665, 15);
frame.getContentPane().add(separator);
JButton btnLogout = new JButton("LOGOUT");
btnLogout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame = new JFrame("Exit");
if(JOptionPane.showConfirmDialog(frame, "Confirm if you want to exit","Login system",
JOptionPane.YES_NO_OPTION)==JOptionPane.YES_NO_OPTION) {
System.exit(0);
}
}}
);
btnLogout.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnLogout.setBounds(588, 28, 89, 23);
frame.getContentPane().add(btnLogout);
JLabel label = new JLabel("");
label.setBounds(74, 238, 46, 14);
frame.getContentPane().add(label);
// label.setText(savings.toString());
/**
* Header fetching details start :
*/
Connection conn= null;
try {
Class.forName ("org.h2.Driver");
conn=DriverManager.getConnection("jdbc:h2:tcp://localhost/~/test;ACCESS_MODE_DATA=rws","sa","");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
try {
String fetch_query= "SELECT * FROM employee_master WHERE id=?";
PreparedStatement statement = conn.prepareStatement(fetch_query);
statement.setString(1,id_str3);
ResultSet rs= statement.executeQuery();
if(rs.next()) {
name.setText(rs.getString("name"));
dob.setText(rs.getString("dob"));
div.setText(rs.getString("division"));
}
} catch (SQLException e) {
e.printStackTrace();
}
/**
* :Header fetching close
*/
/*************************************/
}
}
控制台显示为:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Frame4.initialize(Frame4.java:62)
at Frame4.<init>(Frame4.java:43)
at Frame3$5.actionPerformed(Frame3.java:479)
答案 0 :(得分:0)
也许代码frame.getContentPane()
为空。