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 联系我们: