-1

私は並列と垂直方程式を解く計算機のGUIを作ろうとしています。 GUIが実装されていないときに機能しますが、GUIを実装するときにnullpointerexceptionやnumberformatexceptionなどのエラーが発生します。これを解決する際に私を助けてください。NullpointerexceptionとNumberFormatException java GUI

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) 

+2

あなたのスタックトレースポイントを設定します。 –

+0

精度がそれほど重要でない場合は、BigDoubleの代わりにDoubleデータ型を使用してください。問題を解決し、テキストフィールドからdouble値を渡すのではなく、次のようにしてください。 - new Double(c.getText())Double.parseDoubleメソッドを使用します。 –

答えて

1

に答えてくれてありがとうあなたはにしようとしているテキストボックスからの拡張。初期化後に空白にする必要があります。 したがって、new BigDecimal("")を呼び出すと、NumberFormatExceptionがスローされます。 new BigDecimalがオブジェクトの作成に失敗したため、おそらくNullpointerExceptionがスローされます。 フィールドが塗りつぶされた後に計算を行う必要があります。

EDIT: はまた、それがCは、コードのこの時点で初期化されていないように見えます。

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

EDIT2:あなたが唯一の方法に値を入力した後に行うべきであるすべてのものを移動し、ボタンを使って、このメソッドを呼び出すことができます。

+0

どうすればいいですか –

+0

このエラーは既に解決されています。どのように私はjava.math.BigDecimalを解決するのですか? ? –

+0

どうすればいいですか? EDIT2:値をメソッドに入力した後に行う必要があるすべてを移動し、このメソッドをボタンで呼び出すことができます。 –

0

@ Nordiiiはあなたの問題の背後にある理由を既に説明しました。だから私はそれを繰り返さない。

コードの計算部分は、計算ボタンのactionPerformedメソッドである必要があります。

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()); 
    } 
}); 

もだから私は、デバッガを使用して、この行を確認することをお勧めします79行目に、デフォルトのフレームサイズ

frame.setBounds(100, 100, 468, 369); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
+0

GUI設計を初めてお使いの場合は、 IDEからのあらかじめ定義されたスニペットを使用するのではなく、自分自身をコーディングする方がよいでしょう。 – saify