Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to using arrays of objects but can't figure out what I am doing wrong and why I keep getting a Null pointer exception. I am trying to create TextReport on JTextArea using array of String input.even i am working on Text spillover to get extra text on next line and it should be display as in table format on JTextArea. my boss told me to make simple report for bank project. For more simplicity. Thanks in advance.

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

}

following errors are on console:

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

     BUILD SUCCESSFUL (total time: 9 seconds)
share|improve this question
 
Check testit line 93. Also, capitalize the class name. You don't have to guess when it comes to NullPointerExceptions. They're very easy to diagnose. –  ᴋᴇʏsᴇʀ Dec 23 '13 at 14:21
 
Still i am facing this problem coz of Y=null; but its mendatory to make y as null to perform next operation, so plz help me to solve correctely.@KEYSER –  IceBreakerSandy_at_Alpha Dec 23 '13 at 14:33
add comment

1 Answer

You have assigned D and Y as 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

            }

This was the reason why you would have got a null pointer exception at 1, 2 or/and 3

share|improve this answer
 
or just D = ""; –  ᴋᴇʏsᴇʀ Dec 23 '13 at 14:24
 
yeah, that would work as well. –  Ankit Rustagi Dec 23 '13 at 14:25
 
I appreciate your reply i tried changes as you told me but getting same error.now what plz help me to get out from this thread.@AnkitRustagi. –  IceBreakerSandy_at_Alpha Dec 23 '13 at 14:29
 
Could you point me to the line where you got the exception ? –  Ankit Rustagi Dec 23 '13 at 14:30
 
@AnkitRustagi D = ""; is much better because you use existing String instead of creating new String object. –  Bosko Mijin Dec 23 '13 at 14:35
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.