Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nullpointerexception和NumberFormatException java GUI_Java_User Interface_Nullpointerexception_Numberformatexception - Fatal编程技术网

Nullpointerexception和NumberFormatException java GUI

Nullpointerexception和NumberFormatException java GUI,java,user-interface,nullpointerexception,numberformatexception,Java,User Interface,Nullpointerexception,Numberformatexception,我正在尝试做一个计算器的图形用户界面,它可以解平行和垂直方程。当GUI未实现时,它可以工作,但当我实现GUI时,会出现诸如nullpointerexception和numberformatexception之类的错误。请帮我解决这个问题 import java.awt.*; 公共类SuntayProjGUI{ JFrame frame; private JTextField Ax; private JTextField By; private JTextField C; private JTe

我正在尝试做一个计算器的图形用户界面,它可以解平行和垂直方程。当GUI未实现时,它可以工作,但当我实现GUI时,会出现诸如nullpointerexception和numberformatexception之类的错误。请帮我解决这个问题

import java.awt.*;
公共类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);
    }
}
JFrame;
私有jtextax;
私人JTextfieldby;
私有jtextc字段;
私人JTextField斜坡线;
私有JTextField-yintLine;
BigDecimal xCoefficient,yCoefficient,b,slope1,slope2,yIntercept1,yIntercept2,xCoord,yCoord;//声明。显然不适用于
/**
*启动应用程序。
*@param args
*@wbp.parser.entryPoint
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
SuntayProjGUI window=新的SuntayProjGUI();
window.frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*@wbp.parser.entryPoint
*/
公共SuntayProjGUI(){
初始化();
}
公共无效初始化(){
frame=新的JFrame();
frame.getContentPane().setBackground(颜色为.PINK);
frame.getContentPane().setLayout(null);
Ax=新的JTextField();
Ax.倒退(46、142、116、22);
frame.getContentPane().add(Ax);
Ax.setColumns(10);
By=新的JTextField();
通过.挫折(46、191、116、22);
frame.getContentPane().add(按);
按.setColumns(10);
C=新的JTextField();
C.挫折(46、191、116、22);
frame.getContentPane().add(C);
C.设置柱(10);
JLabel lblpleasechoosehat=新JLabel(“请选择此计算器将接收的输入”);
lblpleasechosewhat.setBounds(12,44,302,16);
frame.getContentPane().add(lblpleasechosewhat);
JComboBox comboBox=新的JComboBox();
组合框.立根(22,76,147,22);
comboBox.addItem(“带Y截距的线”);
comboBox.addItem(“带点的线”);
frame.getContentPane().add(组合框);
//计算
xCoefficient=新的BigDecimal(Ax.getText());
yCoefficient=新的BigDecimal(By.getText());
b=新的BigDecimal(C.getText());
slope1=getSlope(xCoefficient,yCoefficient);
yIntercept1=getYIntercept(yco系数,b);
JLabel lblA=新的JLabel(“A:”);
lblA.挫折(12,148,36,16);
frame.getContentPane().add(lblA);
JLabel lblB=新的JLabel(“B:”);
lblB.立根(12194,56,16);
frame.getContentPane().add(lblB);
JLabel lblC=新的JLabel(“C:”);
lblC.立根(12240,56,16);
frame.getContentPane().add(lblC);
C=新的JTextField();
C.挫折(46、237、116、22);
frame.getContentPane().add(C);
C.设置柱(10);
JLabel lblineax=新JLabel(“行:Ax+By=C”);
lblineax.立根(12,111,137,16);
frame.getContentPane().add(lblineax);
JButton btenter=新JButton(“输入”);
添加ActionListener(新ActionListener(){
已执行的公共无效操作(操作事件arg0){
frame.setVisible(假);
if(comboBox.equals(“带Y截距的直线”)){
行_with_yint lwy=新行_with_yint();
lwy.frame.setVisible(真);
}
else if(comboBox.equals(“带点的线”)){
线_与_点lwp=新线_与_点();
lwp.frame.setVisible(真);
}
}
});
b输入立根(217383,97,25);
frame.getContentPane().add(btenter);
JLabel lblslopeof=新JLabel(“线的斜率:”);
lblslopeof.立根(12291114,16);
frame.getContentPane().add(lblslopeof);
slopeLine=newjtextfield();
slopeLine.setEnabled(假);
slopeLine.setEditable(假);
倾斜线.立根(151,288,116,22);
frame.getContentPane().add(slopeLine);
倾斜线设置柱(10);
slopeLine.setText(slope1.toString());
JLabel lblyinterceptof=新JLabel(“行的Y截距:”);
lbl.立根(12332137,16);
frame.getContentPane().add(lblyInterceptof);
yintLine=newjtextfield();
yintLine.setEnabled(false);
yintLine.setEditable(false);
银线.立根(151,329,116,22);
frame.getContentPane().add(yintLine);
yintLine.setColumns(10);
yintLine.setText(yIntercept1.toString());
JButton btnCalculate=新JButton(“计算”);
BTN计算立根(21723697,25);
frame.getContentPane().add(btnCalculate);
}
公共静态BigDecimal getSlope(BigDecimal x,BigDecimal y)
{
y=y.multiply(新的BigDecimal(-1));//yung-pagsa-1 yung-pagse-ng-Ax+By=C->By=-Ax+C
y=x.divide(y,4,RoundingMode.CEILING);//eto yung Pag divide nung系数ni y sa两侧ng方程->y=-Ax/B+C/B
返回y;
}
公共静态BigDecimal getReciprocalSlope(BigDecimal x,BigDecimal y)
{
y=y.divide(x,4,RoundingMode.天花板)。multiply(新的BigDecimal(-1));//eto yung倒数。显然。balaiktarin lang。kung kanina
返回y;
}
公共静态BigDecimal getYIntercept(BigDecimal y,BigDecimal b)
{
b=b.divide(y,4,RoundingMode.CEILING);//yung Pagguha ng y截距类似的dun sa getSlope pero-ang差异,自瓦朗换位以来,divide lang。
返回b;
}
公共静态void getGEandSE(BigDecimal斜率、BigDecimal xCoord、BigDecimal yCoord、BigDecimal yIntercept、BigDecimal x、BigDecimal y)
{
大十进制平行线A,平行线B,平行线C,垂直线A,垂直线B,垂直线C;
如果(x.compareTo(BigDecimal.ZERO)<0)//itong part na’to,kapag yung除数(kasi diba si y或x yung除数,kapag斜率na normal,si y,kapag NireReciperate for Vertical,si x diba。)为负,gagawing为正,因为lagi Naang正kapag nagdidivide将两边分开
x=x.multiply(新的BigDecimal(-1));
如果(y)与(B)相比
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)
xCoefficient = new BigDecimal(Ax.getText());
yCoefficient = new BigDecimal(By.getText());
b = new BigDecimal(C.getText());
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());
    }
});
frame.setBounds(100, 100, 468, 369);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);