-1

I am trying to make a GUI of a calculator which solves Parallel and Perpendicular Equations. It works when the GUI is not implemented but when I implement the GUI there are errors coming out such as nullpointerexception and numberformatexception. please do help me in resolving this.

import java.awt.*;

public class SuntayProjGUI {

JFrame frame;
private JTextField Ax;
private JTextField By;
private JTextField C;
private JTextField slopeLine;
private JTextField yintLine;

BigDecimal xCoefficient, yCoefficient, b, slope1, slope2, yIntercept1, yIntercept2, xCoord, yCoord; // declaration. Obvious naman na 'to





/**
 * Launch the application.
 * @param args
 * @wbp.parser.entryPoint
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                SuntayProjGUI window = new SuntayProjGUI();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * @wbp.parser.entryPoint
 */
public SuntayProjGUI(){
    initialize();
}

public void initialize(){
    frame = new JFrame();
    frame.getContentPane().setBackground(Color.PINK);
    frame.getContentPane().setLayout(null);

    Ax = new JTextField();
    Ax.setBounds(46, 142, 116, 22);
    frame.getContentPane().add(Ax);
    Ax.setColumns(10);

    By = new JTextField();
    By.setBounds(46, 191, 116, 22);
    frame.getContentPane().add(By);
    By.setColumns(10);

    C = new JTextField();
    C.setBounds(46, 191, 116, 22);
    frame.getContentPane().add(C);
    C.setColumns(10);

    JLabel lblPleaseChooseWhat = new JLabel("Please choose what inputs this calculator will receive");
    lblPleaseChooseWhat.setBounds(12, 44, 302, 16);
    frame.getContentPane().add(lblPleaseChooseWhat);

    JComboBox comboBox = new JComboBox();
    comboBox.setBounds(22, 76, 147, 22);
    comboBox.addItem("Line with Y-intercept");
    comboBox.addItem("Line with Point");
    frame.getContentPane().add(comboBox);


    //Computations 

    xCoefficient = new BigDecimal(Ax.getText());
    yCoefficient = new BigDecimal(By.getText());
    b = new BigDecimal(C.getText());

    slope1 = getSlope(xCoefficient, yCoefficient);
    yIntercept1 = getYIntercept(yCoefficient, b);

    JLabel lblA = new JLabel("A :");
    lblA.setBounds(12, 148, 36, 16);
    frame.getContentPane().add(lblA);

    JLabel lblB = new JLabel("B:");
    lblB.setBounds(12, 194, 56, 16);
    frame.getContentPane().add(lblB);

    JLabel lblC = new JLabel("C:");
    lblC.setBounds(12, 240, 56, 16);
    frame.getContentPane().add(lblC);

    C = new JTextField();
    C.setBounds(46, 237, 116, 22);
    frame.getContentPane().add(C);
    C.setColumns(10);

    JLabel lblLineAx = new JLabel("Line: Ax + By = C");
    lblLineAx.setBounds(12, 111, 137, 16);
    frame.getContentPane().add(lblLineAx);

    JButton btnEnter = new JButton("Enter");
    btnEnter.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame.setVisible(false);

            if(comboBox.equals("Line with Y-intercept")){
            Line_with_yint lwy = new Line_with_yint();
            lwy.frame.setVisible(true);
                        }

            else if(comboBox.equals("Line with Point")){
                Line_with_point lwp = new Line_with_point();
                lwp.frame.setVisible(true);
            }
        }
    });
    btnEnter.setBounds(217, 383, 97, 25);
    frame.getContentPane().add(btnEnter);

    JLabel lblSlopeOfThe = new JLabel("Slope of the Line: ");
    lblSlopeOfThe.setBounds(12, 291, 114, 16);
    frame.getContentPane().add(lblSlopeOfThe);

    slopeLine = new JTextField();
    slopeLine.setEnabled(false);
    slopeLine.setEditable(false);
    slopeLine.setBounds(151, 288, 116, 22);
    frame.getContentPane().add(slopeLine);
    slopeLine.setColumns(10);
    slopeLine.setText(slope1.toString());

    JLabel lblYinterceptOfThe = new JLabel("Y-Intercept of the Line:");
    lblYinterceptOfThe.setBounds(12, 332, 137, 16);
    frame.getContentPane().add(lblYinterceptOfThe);

    yintLine = new JTextField();
    yintLine.setEnabled(false);
    yintLine.setEditable(false);
    yintLine.setBounds(151, 329, 116, 22);
    frame.getContentPane().add(yintLine);
    yintLine.setColumns(10);
    yintLine.setText(yIntercept1.toString());

    JButton btnCalculate = new JButton("Calculate");
    btnCalculate.setBounds(217, 236, 97, 25);
    frame.getContentPane().add(btnCalculate);


}
public static BigDecimal getSlope(BigDecimal x, BigDecimal y)
{
    y = y.multiply(new BigDecimal(-1)); // yung pagmultiply sa -1 yung pagtranspose ng Ax + By = C -> By = -Ax + C
    y = x.divide(y, 4, RoundingMode.CEILING); // eto yung pagdivide nung coefficient ni y sa both sides ng equation -> y = -Ax/B + C/B
    return y;
}

public static BigDecimal getReciprocalSlope(BigDecimal x, BigDecimal y)
{
    y = y.divide(x, 4, RoundingMode.CEILING).multiply(new BigDecimal(-1)); // eto yung reciprocal. obviously. balaiktarin lang. kung kanina 
    return y;
}

public static BigDecimal getYIntercept(BigDecimal y, BigDecimal b)
{
    b = b.divide(y, 4, RoundingMode.CEILING); // yung pagkuha ng y-intercept similar dun sa getSlope pero ang difference since walang transposition, divide lang. 
    return b;
}

public static void getGEandSE(BigDecimal slope, BigDecimal xCoord, BigDecimal yCoord, BigDecimal yIntercept, BigDecimal x, BigDecimal y)
{
    BigDecimal parallelA, parallelB, parallelC, perpendicularA, perpendicularB, perpendicularC;
    if (x.compareTo(BigDecimal.ZERO) < 0) // itong part na 'to, kapag yung divisor (kasi diba either si y or x yung divisor, kapag slope na normal, si y, kapag nirereciprocate for perpendicular, si x diba.) negative, gagawing positive since lagi namang positive kapag nagdidivide both sides diba
        x = x.multiply(new BigDecimal(-1));
    if (y.compareTo(BigDecimal.ZERO) < 0)
        y = y.multiply(new BigDecimal(-1));

    if (yIntercept == null)
    {
        yCoord = yCoord.multiply(new BigDecimal(-1));
        xCoord = xCoord.multiply(new BigDecimal(-1));
        parallelA = slope.multiply(y).multiply(new BigDecimal(-1)); // eto yung diba kapag points ang given, y - y1 = m(x - x1). Yung coefficient ni x kasi si parallelA tapos transpose kaya may -1 tapos para mawala yung fraction, mumultiply by y. Gets naman kung bakit diba? Dito nagaganap yung -mx + y - y1 = mx1
        parallelC = (xCoord.multiply(slope).multiply(new BigDecimal(-1))).add(yCoord).multiply(y); // kapag si C naman, diba y - y1 = m(x - x1) dito nagaganap yung didistribute si M tsaka ttranspose sa kabila. From y -y1 = m(x - x1) -> y - y1 + mx1 = mx
        perpendicularA = getReciprocalSlope(x, y).multiply(x).multiply(new BigDecimal(-1)); // same principle lang, difference lang neto yung imbis na slope yung mumultiply, yung reciprocal nya (yung function dun na reciprocalSlope)
        perpendicularC = (xCoord.multiply(getReciprocalSlope(x, y).multiply(new BigDecimal(-1))).add(yCoord)).multiply(x);

        if (parallelC.compareTo(BigDecimal.ZERO) > 0)
            System.out.println("Parallel Line GE: " + parallelA + "x + " + y + "y + " + parallelC + " = 0");
        else
            System.out.println("Parallel Line GE: " + parallelA + "x + " + y + "y - " + parallelC.multiply(new BigDecimal(-1)) + " = 0");

        System.out.println("Parallel Line SE: " + parallelA + "x + " + y + "y = " + parallelC);

        if (perpendicularC.compareTo(BigDecimal.ZERO) > 0)
            System.out.println("Perpendicular Line GE: " + perpendicularA + "x + " + x + "y + " + perpendicularC + " = 0");
        else
            System.out.println("Perpendicular Line GE: " + perpendicularA + "x + " + x + "y - " + perpendicularC.multiply(new BigDecimal(-1)) + " = 0");

        System.out.println("Perpendicular Line SE: " + perpendicularA + "x + " + x + "y = " + perpendicularC.multiply(new BigDecimal(-1)));
    }
    else
    {
        parallelA = slope.multiply(new BigDecimal(-1)).multiply(y); // gets mo na siguro 'to. Kung ano nasa notes mo at yung pagkakahawig nya sa nasa taas ganun din
        parallelC = yIntercept.multiply(new BigDecimal(-1)).multiply(y);
        perpendicularA = getReciprocalSlope(x, y).multiply(new BigDecimal(-1)).multiply(x);
        perpendicularC = yIntercept.multiply(new BigDecimal(-1)).multiply(x);

        if (parallelC.compareTo(BigDecimal.ZERO) > 0)
            System.out.println("Parallel Line GE: " + parallelA + "x + " + y + "y + " + parallelC + " = 0");
        else
            System.out.println("Parallel Line GE: " + parallelA + "x + " + y + "y - " + parallelC.multiply(new BigDecimal(-1)) + " = 0");

        System.out.println("Parallel Line SE: " + parallelA + "x + " + y + "y = " + parallelC.multiply(new BigDecimal(-1)));

        if (perpendicularC.compareTo(BigDecimal.ZERO) > 0)
            System.out.println("Perpendicular Line GE: " + perpendicularA + "x + " + x + "y + " + perpendicularC + " = 0");
        else
            System.out.println("Perpendicular Line GE: " + perpendicularA + "x + " + x + "y - " + perpendicularC.multiply(new BigDecimal(-1)) + " = 0");

        System.out.println("Perpendicular Line SE: " + perpendicularA + "x + " + x + "y = " + perpendicularC);
    }
}

}

and when I try to run this, there are errors:

java.lang.NumberFormatException
at java.math.BigDecimal.<init>(BigDecimal.java:596)
at java.math.BigDecimal.<init>(BigDecimal.java:383)
at java.math.BigDecimal.<init>(BigDecimal.java:806)
at SuntayProjGUI.initialize(SuntayProjGUI.java:83)
at SuntayProjGUI.<init>(SuntayProjGUI.java:47)
at SuntayProjGUI$1.run(SuntayProjGUI.java:34)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
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:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
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)

thank you for answering

  • 2
    Your stacktrace points to line 79. So I would recommend to use a debugger and check this line. – shalama Dec 14 '16 at 13:05
  • Try to use Double datatype instead of BigDouble if precision is not much important, hope it could solve your problem and instead of passing double values from textfields as:- new Double(c.getText()) use Double.parseDouble method. – Manish Sakpal Dec 14 '16 at 13:05

2 Answers2

1

You'r trying to get a text from a text box which should be empty after initializing. Therefore you'r calling new BigDecimal("") which will throw an NumberFormatException. The NullpointerException will probably get thrown because new BigDecimal failed to create an Object. You need to do the computations after the fields got filled.

EDIT: Also it looks like C hasn't been initialized at this point of code.

xCoefficient = new BigDecimal(Ax.getText());
yCoefficient = new BigDecimal(By.getText());
b = new BigDecimal(C.getText());

EDIT2: You could move everything which should only be done after entering values into a method and call this method via a button.

Nordiii
  • 446
  • 1
  • 3
  • 10
0

@Nordiii already explained the reason behind your problem. So, i am not repeating it.

The computation part of your code should be in actionPerformed method of your Calculate Button.

EDIT:

btnCalculate.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent arg0)
    {
        xCoefficient = new BigDecimal(Ax.getText());
        yCoefficient = new BigDecimal(By.getText());
        b = new BigDecimal(C.getText());

        slope1 = getSlope(xCoefficient, yCoefficient);
        yIntercept1 = getYIntercept(yCoefficient, b);

        slopeLine.setText(slope1.toString());

        yintLine.setText(yIntercept1.toString());
    }
});

Also set default frame size

frame.setBounds(100, 100, 468, 369);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
saify
  • 70
  • 1
  • 1
  • 9
  • If you are new to GUI designing; its better to code yourself instead of using predefined snippets from IDE. – saify Dec 14 '16 at 15:16