I am running a simple applet in my machine.Note that when executing applet "Null pointer exception" error occurs when the applet is trying to run. The below code is shown
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.math.*;
<applet code="s09_04" width=300 height=50>
</applet>
public class s09_04 extends Applet
{
CardLayout c1;
Panel p;
Label l1;
Label l2;
Label l3;
Label l4;
TextField t1;
TextField t2;
TextField t3;
TextField t4;
public void start()
{
}
public void init()
{
c1 = new CardLayout();
l1 = new Label("Enter Name :");
l2 = new Label("Enter Place :");
l3 = new Label("Address :");
l4 = new Label("Pin :670571 ");
t1 = new TextField(20);
p = new Panel();
p.setLayout(c1);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(l4);
add(t4);
}
public void paint(Graphics g)
{
}
}
The command used are
javac s09_04.java
and
appletviewer s09_04.java.
Terminal output:
java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1037)
at java.awt.Container.add(Container.java:373)
at s09_04.init(s09_04.java:32)
at java.lang.Thread.run(Thread.java:701)
My question is what is most likely reason that applet is failing to start??When does null pointer exception occur???What is the reason for causing null pointer exception??And what should i change in my code so that code runs smoothly without any error.Any suggestions/changes in code would be appreciated.note that i am running this code in linux .thanks...