ArrayList Calculator GUI的NullPointerException

[英]NullPointerException with ArrayList Calculator GUI


I'm designing a calculator console that will take in two values, create a new instance of a class AdditionProblem which will return JLabel arrays of the 2 addends of the problem, the sum, and the amounts carried from each place (it shows the work). I have a custom class that extends JPanel and an ArrayList of JPanels that represent each line of the Solved problem after the add button is clicked. The AdditionProblem class works just fine as well as the Main class. When the addition button is clicked after I created the console class to display the answer I get a null pointer exception on line 44 "row.add(new JPanel)". If you need to see the rest of my code please let me know and I will post it but to save time I will just post the Console class since the other classes have been working properly until I moved code over to this class.

我正在設計一個計算器控制台,它將接收兩個值,創建一個類AdditionProblem的新實例,該實例將返回問題的兩個附加物的JLabel數組,以及每個地方的金額(它顯示了工作)。我有一個擴展JPanel的自定義類和一個JPanel的ArrayList,它表示單擊add按鈕后解決問題的每一行。附加問題類和主類一樣工作得很好。當我創建控制台類以顯示答案后單擊add按鈕時,我在第44行中得到一個空指針異常。添加(新JPanel)”。如果您需要查看我的其余代碼,請讓我知道,我將發布它,但是為了節省時間,我將只發布控制台類,因為其他類一直正常工作,直到我將代碼轉移到這個類。

package Main;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Aritmathic.AdditionProblem;

public class Console extends JPanel {

private int width = 230, height = 260;
private ArrayList<JPanel> rows;
private ArrayList<JLabel> columns;

private void init(){

    this.setPreferredSize(new Dimension(width, height));
    this.setBackground(Color.WHITE);
    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

}

public Console(){

    init();

}

public void printAnswer(AdditionProblem problem){

    int rowIndex = 0;

    JLabel[] sumLabels = problem.getSumLabels();
    JLabel[] addend1Labels = problem.getAddend1Labels();
    JLabel[] addend2Labels = problem.getAddend2Labels();
    JLabel[] carriedLabels = problem.getCarriedLabels();

    rows.add(new JPanel());

    for(int i = 0; i < carriedLabels.length; i++){
        rows.get(rowIndex).add(carriedLabels[i]);
    }

    rowIndex++;
    rows.add(new JPanel());

    for(int i = 0; i < addend1Labels.length; i++){
        rows.get(rowIndex).add(addend1Labels[i]);
    }

    rowIndex++;
    rows.add(new JPanel());

    for(int i = 0; i < addend2Labels.length; i++){
        rows.get(rowIndex).add(addend2Labels[i]);
    }

    rowIndex++;
    rows.add(new JPanel());

    for(int i = 0; i < sumLabels.length; i++){
        rows.get(rowIndex).add(new JLabel("__"));
    }

    rowIndex++;
    rows.add(new JPanel());

    for(int i = 0; i < sumLabels.length; i++){
        rows.get(rowIndex).add(sumLabels[i]);
    }

    for(int i = 0; i < rows.size(); i++){
        this.add(rows.get(i));
    }
}

}

2 个解决方案

#1


3  

Initialize ArrayList<>() in constructor.

初始化數組列表< >()構造函數。

public Console(){
    rows = new ArrayList<JPanel>();
    columns = new ArrayList<JLabel>();
    init();
}

#2


1  

You never initialize your private ArrayList<JPanel> rows; member. Change it to private ArrayList<JPanel> rows = new ArrayList<>();. The same applies to columns, though it doesn't seem to be used anywhere in the code you posted.

您從未初始化您的私有ArrayList 行;成員。將它改為private ArrayList 行= new ArrayList<>();同樣的情況也適用於列,盡管在您發布的代碼中似乎沒有使用它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/06/03/af57d3ea98fd536afc3a7f4f9cd8501f.html



 
  © 2014-2022 ITdaan.com 联系我们: