0

getting exception in thread awt-eventqueue-1 java.lang.nullpointerexception ?? What could be source of error? I want to create an applet to reverse given string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

public class MyApp extends Applet implements ActionListener
{
Panel p1,p2;
TextField tf[]= new TextField[2];
public void init()
{
    setBackground(Color.cyan);

}
public void start()
{
    TextField tf[]= new TextField[2];
    tf[0] = new TextField();
    tf[0].setColumns(20);
    Label l1=new Label("String:");
    p1 = new Panel();
    p1.add(l1);
    p1.add(tf[0]);
    p1.setLayout(new FlowLayout());

    tf[1] = new TextField();
    tf[1].setColumns(20);
    Button l2=new Button("Reverse:");
    l2.addActionListener(this);
    p2 = new Panel();
    p2.add(l2);
    p2.add(tf[1]);
    p2.setLayout(new FlowLayout());
    add(p1);
    add(p2);

}
public void actionPerformed(ActionEvent ae)
{
    tf[1].setText("hiii");  
    if(ae.getActionCommand()=="Reverse:")
    {
    StringBuffer sb=new StringBuffer(tf[0].getText());
    String s= new String(sb.reverse());
    tf[1].setText("hiii");  
    }
}


}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
RAJAN_PARMAR
  • 139
  • 2
  • 9
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Dec 16 '14 at 23:21

1 Answers1

0

You are declaring the array tf in both the class and in the method start.
As a consequence, what you initialize in start aren't the class members, but variables that are local to start. The member variable tf remains null.

So, in start, change

TextField tf[]= new TextField[2];

to

tf[]= new TextField[2];