0

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3046211
  • 466
  • 2
  • 13
  • you can use `try-catch-finally` block for exception handling to avoid application being stopped – Straw Hat Apr 03 '14 at 07:19
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Apr 04 '14 at 05:02

4 Answers4

0

Because TextField t2, t3 and t4; is not initialized. resolve by

t2=new TextField(20);
t3=new TextField(20);
t4=new TextField(20);

like t1

newuser
  • 8,338
  • 2
  • 25
  • 33
0

most likely reason that applet is failing to start??

The documentation of the Applet says to add a component to the Container, that should be initialised.

Here, in the code, t2, t3, t4 are not initialised, which throw NullPointerException

When does null pointer exception occur???

That's a very big topic and simply in one line,

When you try to access a object which is not initialised, then it throws NPE(see the example)

Object o = null;
// o is not initialised.
o.wait();

what should i change in my code so that code runs smoothly without any error

Initialise all the components which you are trying to add to Applet.

t2=new TextField(20);
t3=new TextField(20);
t4=new TextField(20);
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
0

You are Declare TextField TextField t2;TextField t3;TextField t4; but not initialize in init() initialize a t2,t3,t4. You are adding add(t2);add(t3);add(t4); //Here The Exception

         t2=new TextField(20);
         t3=new TextField(20);
         t3=new TextField(20);
Benjamin
  • 2,257
  • 1
  • 15
  • 24
0

but if you are not initializing Text fields means with out giving it any space how could it can store the data?

so try this.

t2=new TextField(size);
t3=new TextField(size);
t3=new TextField(size);

it will help.

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68