简体   繁体   中英

How do I handle null pointer exception in java applet?

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

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

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

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 add(t2);add(t3);add(t4); //Here The Exception

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM