字符串数组中的空指针异常

时间:2013-12-23 14:19:34

标签: java arrays string jtextarea

我是新手使用对象数组,但无法弄清楚我做错了什么以及为什么我一直得到Null指针异常。 我正在尝试使用String input的数组在JTextArea上创建TextReport。我正在处理Text spillover以在下一行获取额外的文本,它应该在JTextArea上以表格格式显示。我的老板告诉我为银行项目做简单的报告。为了更简单。提前致谢。

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;

public class testit extends JApplet implements ActionListener {
   String[] colName = new String[]{"Date", "Account.No", "Description", "Deposit", "Withdraw"};
    String A[][] = {{"13/12/2013", "101", "AlphaSoft Infotek Ltd. Nashik", "3000", "0"},
    {"15/12/2013", "103", "Bank Ladger ", "5000", "0"}};
    String[][] B = new String[3][5];

    String[] K = new String[5];

    Container c;
    JTextArea outputArea;
    //JScrollPane js1;

    JButton b;

    public void init() {

        c = getContentPane();
        c.setLayout(new FlowLayout());

        outputArea = new JTextArea(20, 80);
        Border border = BorderFactory.createLineBorder(Color.RED);
        outputArea.setBorder(BorderFactory.createCompoundBorder(border,
                BorderFactory.createEmptyBorder(10, 10, 10, 10)));
        c.add(outputArea);
        //js1=new JScrollPane(outputArea);
        // add(js1);
        b = new JButton("Show Report");
        b.addActionListener(this);
        c.add(b);

    }

    public void actionPerformed(ActionEvent e) {
        outputArea.setEditable(false);
        outputArea.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        outputArea.setText(" ");
        outputArea.append("ALPHASOFT -- BANK LADGER , REPORTS.\n\n");
        //outputArea.append("Sr.No");
        for (int j = 0; j < colName.length; j++) {
            outputArea.append("\t" + colName[j]);

        }

        outputArea.append("\n");

        for (int i = 0; i < A.length; i++) {
            int colv = A[i].length;
            String D = null;
            String Z[][] = A;
           //String Y[]=new String[];
            String Y[] = new String[colv];
            //Y=A;
            for (int ZX = 0; ZX < A[i].length; ZX++) {
                Y[ZX] = A[i][ZX];
            }
            while (Y != null) {

                Z[i] = Y;

                Y = null;
                D = null;
                String bb[] = new String[colv];
                for (int nx = 0; nx < A[i].length; nx++) {
                    Y[nx] = null;

                    //if(Z[i][nx].length()>0){
                    if (Z[i][nx].length() > 20) {
                        D += Z[i][nx].substring(0, 20);
                        Y[nx] = Z[i][nx].substring(20);

                    } else {
                        D += rightpad(Z[i][nx], 20, "R");

                    }
 }

                outputArea.append(D + "\n");

            }

        }
}

    public static String rightpad(String inp, int ln, String rl) {
        String pd = "";
        for (int nx = 0; nx < ln - inp.length(); nx++) {
            pd += " ";
        }
        if (rl == "R") {
            pd = inp + pd;
        } else {

            pd = pd + inp;
        }

        return pd;
    }

}

以下错误在控制台上:

  Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
 at reportlast.testit.actionPerformed(testit.java:93)

     BUILD SUCCESSFUL (total time: 9 seconds)

1 个答案:

答案 0 :(得分:1)

您已将DY指定为null

        Y = new String[array_length]; // this was null earlier
        D = ""; // this was null earlier
        String bb[] = new String[colv];
        for (int nx = 0; nx < A[i].length; nx++) {
            Y[nx] = null;

            //if(Z[i][nx].length()>0){
            if (Z[i][nx].length() > 20) {
                D += Z[i][nx].substring(0, 20);   // 1
                Y[nx] = Z[i][nx].substring(20);   // 2

            } else {
                D += rightpad(Z[i][nx], 20, "R");  // 3

            }

这就是为什么你会在1,2或/和3

得到空指针异常的原因