0

GUIを作成し、生成コードをコピーして、クラスに必要なものを慎重に変更し、必要なインポートなどをすべて追加しましたが、プロジェクトをビルドすると、このエラーが発生し、理解できません。

run:
Exception in thread "main" java.lang.NullPointerException
  at question3_test.Triangle.<init>(Triangle.java:35)
  at question3_test.Triangle.main(Triangle.java:142)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

これは私がコピーしたコードです(よりよく理解するために)

package question3_test;
import java.awt.*;
import javax.swing.*;
public class Triangle extends JFrame
{
    private JButton jButton1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JPanel jPanel1;
    private JLabel result_lbl;
    private JLabel side1_lbl;
    private JTextField side1_tf;
    private JTextField side2_tf;
    private JTextField side3_tf;
    private Label title_lbl;

     private void initComponents() 
     {

     }

    public Triangle()
    {
         jPanel1.setMaximumSize(new Dimension(200, 200));    
        title_lbl.setFont(new Font("Arial", 1, 18)); // NOI18N
        title_lbl.setText("AREA OF TRIANGLE");    
        side1_lbl.setFont(new Font("Tahoma", 1, 11)); // NOI18N
        side1_lbl.setText("Side 1:");    
        jLabel2.setFont(new Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Side 2:");    
        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Side 3:");    
        side1_tf.setMinimumSize(new java.awt.Dimension(60, 20));    
        side2_tf.setMinimumSize(new java.awt.Dimension(60, 20));    
        side3_tf.setMinimumSize(new java.awt.Dimension(60, 20));
        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setText("Calculate Area");
        jButton1.setBorder(null);    
        result_lbl.setText("jLabel4");    
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(25, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(side1_lbl)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(side1_tf, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(10, 10, 10)))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(side3_tf, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(side2_tf, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(result_lbl))
                    .addComponent(title_lbl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(30, 30, 30))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(title_lbl, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(side1_lbl)
                    .addComponent(side1_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(side2_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(side3_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(result_lbl)
                .addGap(0, 21, Short.MAX_VALUE))
        );

        title_lbl.getAccessibleContext().setAccessibleName("Area of Triangle");    
 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(37, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(25, Short.MAX_VALUE))
    );

    }

    public static void main(String args [])
    {
        Triangle mytri = new Triangle();
        mytri.setVisible(true);

    }
}
4

2 に答える 2

1

まだ作成されていないオブジェクトに値の設定を開始します。最初にそれらのオブジェクトを作成する必要があります。例 :

jPanel1 = new Jpanel();
于 2012-10-10T16:30:29.977 に答える
1

この線:

jPanel1.setMaximumSize(new Dimension(200, 200));

NullPointerExceptionこのコンポーネントに値を割り当てていないため、はをスローします。

実際、子コンポーネントをインスタンス化していないか、initComponentsメソッドを呼び出していません。

private void initComponents() {
    jPanel1 = new JPanel();
    title_lbl = new JLabel();
    side1_lbl = new JLabel();
    ...
}
于 2012-10-10T16:32:09.327 に答える