Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NUllPointerException

 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a code for applet which draws a string at point x &y but it gives null Pointer Exception.can anyone help me? the code is-

import java.awt.*;
import java.applet.*;
public class SecondApplet extends Applet
{
String text;

int x;
int y;

public void init()
{
this.text= "error";
x=50;
y=100;
}
public void start(){
try{
text=getParameter(text);
}catch(Exception ex){}

try
{
x=Integer.parseInt(getParameter("x"));
y=Integer.parseInt(getParameter("y"));
}catch(NumberFormatException e){}

}

public void paint(Graphics g) throws NullPointerException
{
g.drawString("Welcome rajesh this is Applet",150,50);
g.drawString(this.text,x,y);
}
}
it is saved as SecondApplet.java and the html file is-

<HTML>

<HEAD>
<TITLE>USING APPLET TAG</TITLE>
</HEAD>
<BODY>
<H3>APPLET DISPLAYS AT X AND Y POSITION</H3>

<APPLET CODE="SecondApplet.class" WIDTH=500 HEIGHT=250>
<PARAM NAME="text" VALUE="APPLETS ARE FUN">
<PARAM NAME="x" VALUE="50">
<PARAM NAME="Y" VALUE="70">
NOT JAVA ENABLED
</APPLET>
</BODY>
</HTML>

when i use appletviewer i get a nullpointer exception.
also wen i use IE 6 as browser to open the html file the applet doesnt come.
I guess java is disabled but then how to enable it. I cant find any option in advanced tab of Internet Options.
Otherwise in appletviewer it is working fine except for nullpointer exception.Can anyone point out why the string text in g.drawString is giving the said exception? I have tried it without using "this" also.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

It would help to know what line the exception refers to. Any chance you wanted getParameter(text) to be getParameter("text") in quotes?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic