我正在尝试制作一个解决平行和垂直方程的计算器的GUI。它在未实现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);
}
}
}
当我尝试运行时,会出现错误:
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)
谢谢你的回答
答案 0 :(得分:1)
您尝试从初始化后应为空的文本框中获取文本。
因此,您调用new BigDecimal("")
会抛出NumberFormatException。
可能会抛出NullpointerException,因为new BigDecimal
无法创建Object。
在字段填满后你需要进行计算。
修改强>
此外,C
似乎还没有在此代码点初始化。
xCoefficient = new BigDecimal(Ax.getText());
yCoefficient = new BigDecimal(By.getText());
b = new BigDecimal(C.getText());
EDIT2:您可以移动所有应该在将值输入方法后执行的操作,并通过按钮调用此方法。
答案 1 :(得分:0)
@Nordiii已经解释了你的问题背后的原因。所以,我不是在重复它。
代码的计算部分应该是Calculate Button的actionPerformed方法。
编辑:
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);