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)
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