0

My app started with many exceptions. I was able to figure out some, but I am stuck now with this exception:

java.lang.NullPointerException
    at Asym.initialize(Asym.java:304)
    at Asym.<init>(Asym.java:45)
    at Asym$1.run(Asym.java:196)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "main" java.lang.NullPointerException
    at Asym.computethestuff(Asym.java:186)
    at Asym.main(Asym.java:205)

Here's my GUI code (removed my methods)

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.math.BigInteger;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Asym {



    public JFrame frmRsa;
    private static JTextField pf;
    private static JTextField qf;
    private static JTextField nf;
    private static JTextField phif;
    public static JTextField mf;
    static JLabel dl;
    static JLabel rl;
    static JLabel el;
    static JLabel cl;
    static JLabel df;
    static JLabel ef;

    public Asym() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Asym window = new Asym();
                    window.frmRsa.setVisible(true);
                } catch (Exception e) {
                    System.out.println("Error with your swing");
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * Create the application.
     */

    private void initialize() {
        frmRsa = new JFrame();
        frmRsa.setTitle("RSA");
        frmRsa.setBounds(100, 100, 363, 735);
        frmRsa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmRsa.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.setBounds(12, 70, 326, 146);
        frmRsa.getContentPane().add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("Generating two BIG prime numbers, P, and Q");
        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblNewLabel.setBounds(12, 13, 314, 27);
        panel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("P");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblNewLabel_1.setBounds(12, 45, 18, 16);
        panel.add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("Q");
        lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblNewLabel_2.setBounds(12, 74, 18, 16);
        panel.add(lblNewLabel_2);

        pf = new JTextField();
        pf.setBounds(36, 43, 283, 22);
        panel.add(pf);
        pf.setColumns(10);

        qf = new JTextField();
        qf.setColumns(10);
        qf.setBounds(36, 74, 283, 22);
        panel.add(qf);

        JButton btnGenerateNew = new JButton("Generate new");
        btnGenerateNew.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                generatePrimes();
            }
        });
        btnGenerateNew.setBounds(201, 108, 113, 25);
        panel.add(btnGenerateNew);

        JButton btnUseThesePrimes = new JButton("Use these primes");
        btnUseThesePrimes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                computethestuff();
            }
        });
        btnUseThesePrimes.setBounds(46, 108, 142, 25);
        panel.add(btnUseThesePrimes);

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel_1.setLayout(null);
        panel_1.setBounds(12, 229, 326, 107);
        frmRsa.getContentPane().add(panel_1);

        JLabel lblFromThePrimes = new JLabel("From the primes above, we get n, and Phi");
        lblFromThePrimes.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblFromThePrimes.setBounds(12, 13, 314, 27);
        panel_1.add(lblFromThePrimes);

        JLabel lblN = new JLabel("N");
        lblN.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblN.setBounds(12, 45, 18, 16);
        panel_1.add(lblN);

        JLabel lblPhi = new JLabel("Phi");
        lblPhi.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblPhi.setBounds(12, 74, 24, 16);
        panel_1.add(lblPhi);

        nf.setColumns(10);
        nf.setBounds(36, 43, 283, 22);
        panel_1.add(nf);

        phif.setColumns(10);
        phif.setBounds(36, 74, 283, 22);
        panel_1.add(phif);

        JLabel lblNewLabel_6 = new JLabel("");
        lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblNewLabel_6.setBounds(51, 46, 263, 16);
        panel_1.add(lblNewLabel_6);

        JLabel label = new JLabel("");
        label.setFont(new Font("Tahoma", Font.PLAIN, 15));
        label.setBounds(48, 75, 266, 16);
        panel_1.add(label);

        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel_2.setLayout(null);
        panel_2.setBounds(12, 349, 326, 107);
        frmRsa.getContentPane().add(panel_2);

        JLabel lblAndFinallyWe = new JLabel("And finally we got the keys");
        lblAndFinallyWe.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblAndFinallyWe.setBounds(12, 13, 314, 27);
        panel_2.add(lblAndFinallyWe);

        JLabel lblE = new JLabel("e");
        lblE.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblE.setBounds(12, 45, 18, 16);
        panel_2.add(lblE);

        JLabel lblD = new JLabel("d");

        lblD.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblD.setBounds(12, 74, 18, 16);
        panel_2.add(lblD);

        ef = new JLabel("");
        ef.setFont(new Font("Tahoma", Font.PLAIN, 15));
        ef.setBounds(51, 45, 263, 16);
        panel_2.add(ef);

        df = new JLabel("");
        df.setFont(new Font("Tahoma", Font.PLAIN, 15));
        df.setBounds(48, 74, 266, 16);
        panel_2.add(df);

        JLabel lblNewLabel_3 = new JLabel("<html>Asyemmetric Encryption\r\n<br>using RSA Algorithm</html>");
        lblNewLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 16));
        lblNewLabel_3.setBounds(12, 13, 321, 44);
        frmRsa.getContentPane().add(lblNewLabel_3);

        JPanel panel_3 = new JPanel();
        panel_3.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel_3.setLayout(null);
        panel_3.setBounds(12, 466, 326, 107);
        frmRsa.getContentPane().add(panel_3);

        JLabel lblEnterYourSecret = new JLabel("Enter your secret message here");
        lblEnterYourSecret.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblEnterYourSecret.setBounds(12, 13, 314, 27);
        panel_3.add(lblEnterYourSecret);

        mf = new JTextField();
        mf.setColumns(10);
        mf.setBounds(36, 43, 283, 22);
        panel_3.add(mf);

        JButton btnAndHitThis = new JButton("And hit this magic button");
        btnAndHitThis.setBounds(130, 78, 184, 25);
        panel_3.add(btnAndHitThis);

        JPanel panel_4 = new JPanel();
        panel_4.setBounds(12, 586, 321, 94);
        frmRsa.getContentPane().add(panel_4);
        panel_4.setLayout(null);

        JLabel lblNewLabel_4 = new JLabel("Ciphered");
        lblNewLabel_4.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblNewLabel_4.setBounds(8, 5, 77, 16);
        panel_4.add(lblNewLabel_4);

        JLabel lblNewLabel_5 = new JLabel("Decrypted");
        lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblNewLabel_5.setBounds(8, 46, 77, 16);
        panel_4.add(lblNewLabel_5);

        cl = new JLabel("");
        cl.setFont(new Font("Tahoma", Font.PLAIN, 15));
        cl.setBounds(97, 5, 204, 16);
        panel_4.add(cl);

        dl = new JLabel("");
        dl.setFont(new Font("Tahoma", Font.PLAIN, 15));
        dl.setBounds(97, 46, 204, 16);
        panel_4.add(dl);

        rl = new JLabel("");

        rl.setFont(new Font("Tahoma", Font.PLAIN, 15));
        rl.setBounds(97, 65, 204, 16);
        panel_4.add(rl);

        JLabel label_6 = new JLabel("");
        label_6.setFont(new Font("Tahoma", Font.PLAIN, 15));
        label_6.setBounds(313, 13, 0, 0);
        panel_4.add(label_6);

        JLabel lblEncrypted = new JLabel("Encrypted");
        lblEncrypted.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblEncrypted.setBounds(8, 26, 77, 16);
        panel_4.add(lblEncrypted);

        JLabel lblRestored = new JLabel("Restored");
        lblRestored.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblRestored.setBounds(8, 65, 77, 16);
        panel_4.add(lblRestored);

        el = new JLabel("");
        el.setFont(new Font("Tahoma", Font.PLAIN, 15));
        el.setBounds(97, 26, 204, 16);
        panel_4.add(el);

        JLabel label_7 = new JLabel("");
        label_7.setFont(new Font("Tahoma", Font.PLAIN, 15));
        label_7.setBounds(252, 34, 0, 0);
        panel_4.add(label_7);

    }
}

I manually relocated some declerations because I thought I need to. Like moving jLabels (dl, rl, el, cl, df, ef) to the begning.

Any information is appreciated!

Abdulrahman Hassoun
  • 1,151
  • 1
  • 9
  • 15
  • Which line is 304? – Roman Puchkovskiy Dec 10 '17 at 19:10
  • 1
    The heuristic for debugging a NullPointerException is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Dec 10 '17 at 19:12

0 Answers0