0

From frame3 I'm calling frame4 on the click of a button and passing a data value savings from frame 3 to frame 4. When I print the value from the constructor using a dialog box, it prints accurate value. But why does when I try to perform adding operation on saving I get null pointer exception?

// 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
         */

/*************************************/


}
}

console reads:

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)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) *"From frame3 im calling frame4 .."* Eeeek! See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) See also [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) – Andrew Thompson Jun 26 '18 at 13:15
  • .. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 26 '18 at 13:24
  • The error message tells you the line number of the statement causing the problem. So figure out which variable on that line is null and fix it. – camickr Jun 26 '18 at 13:47

1 Answers1

0

Maybe the code frame.getContentPane() is null.

Shikhar Chaudhary
  • 425
  • 1
  • 6
  • 11