This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

NUllPointerException

 
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.
 
(instanceof Sidekick)
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?
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic