Find the cause for Exception

February 4, 2011 at 04:52:11
Specs: Windows XP, 1.99 gb
Hi, Iam working on awt. Can somebody help me out with a solution for the problem. I,m using inner class to create another frame within the body of a class extendimg frame. I'm pasting the code and the error message please try to help me out. Thanks in advance.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Frameclass$Classx.<init>(Frameclass.java:103)
at Frameclass.actionPerformed(Frameclass.java:81)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


java code file:


import java.awt.Button;
import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class Frameclass extends Frame implements ActionListener
{
Label l1,l2,l3,l4,l5,l6,l7,l8;
TextField t1,t2,t3,t4,t5;
TextField t6;
Button b;
Button b1;
int n;
Frameclass()
{
setTitle("User Info");
setSize(320,400);
FlowLayout fl=new FlowLayout();
setLayout(fl);
l1=new Label();
l1.setText("xlsx-file name:");
l1.setAlignment(1);
t1=new TextField(40);
l2=new Label("row starting range");
l2.setAlignment(1);
l3=new Label("",1);
t2=new TextField(40);
l4=new Label("row end");
t3=new TextField(40);
l5=new Label("column starting range");
l5.setAlignment(1);
l6=new Label("");
t4=new TextField(40);
l7=new Label("column end");
t5=new TextField(40);
l8=new Label("enter number of columns");
t6=new TextField(40);
b=new Button("submit");
b1=new Button("close");
add(l1);
add(t1);
add(l2);
add(l3);
add(t2);
add(l4);
add(t3);
add(l5);
add(l6);
add(t4);
add(l7);
add(t5);
add(l8);
add(t6);
add(b);
add(b1);
b.addActionListener(this);
b1.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);


}
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub

if(ae.getSource()==b)
{
n=Integer.parseInt(t6.getText());
Classx o = new Classx();
}
if(ae.getSource()==b1)
{
System.exit(0);
}

}
class Classx implements ActionListener
{ Label l[]=new Label[n];
Label l1[]=new Label[n];
TextField tf[]=new TextField[n];
Choice c[]=new Choice[n];
Button but=new Button("submit");
Classx()
{
setTitle("Header Information");
setSize(320,400);
FlowLayout fl=new FlowLayout();
setLayout(fl);
for(int i=0;i<n;i++)
{
l[i].setText("enter the "+(i+1)+" column name");
l1[i].setName("select the data type");
c[i].add("number");
c[i].add("varchar");
c[i].add("date");
c[i].add("long");
c[i].add("clob");
c[i].add("blob");
add(l[i],0);
add(tf[i]);
add(l1[i],0);
add(c[i]);
}
add(b);
b.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b)
{
for(int i=0;i<n;i++)
{
System.out.println(tf[i].getText());
System.out.println(c[i].getSelectedItem());
}
}
}

}
public static void main(String args[])throws Exception
{
Frameclass f=new Frameclass();
}

}


See More: Find the cause for Exception

Report •


#1
February 4, 2011 at 07:11:48
Your immediate problem is that in Classx you declare some arrays of objects (e.g. Label l[]) and then you use the objects in these arrays. But you have never created the objects in the arrays, just the arrays themselves. Thus, the first time you try to use one of these objects, in the line:

l[i].setText("enter the "+(i+1)+" column name");

you get a null exception, because l[i] is a null pointer. You'll need to create the objects in these arrays as well as the arrays themselves.

I suspect that once you have done that you may still find the program doesn't do what you require, but it's a start.

You really need to run under a debugger to easily find these errors. May I suggest that you look at IntelJ IDEA (free download available - Google it) which provides a good IDE for developing and debugging Java programs.


Report •
Related Solutions


Ask Question