?
Solved

Problem with Apllet (Deprecation)

Posted on 1997-07-06
3
Medium Priority
?
259 Views
Last Modified: 2012-05-04
I am messing around with an applet I got from Java Boutique
and am getting some warnings involved with deprecated methods. If Iignore thesewarnings and use the class anyways I get a NullPointer error.

I am using jdk113.
The method is something like Dimension Size()
 
Here is the applet:

//* <C> 1996 Growler Software, Inc. *//
import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.util.*;
import java.net.*;

public class Firework extends Applet implements Runnable
{
public int AnimationSpeed,
           RocketStyleVariability,
           MaxRocketNumber,
           MaxRocketExplosionEnergy,
           MaxRocketPatchNumber,
           MaxRocketPatchLength,
           Gravity;
public String RocketSoundtrack;

private int mx=20,my=20;
private Thread thread=null;

private Rocket rocket[];

public void init()
   {
   int i;
   String p;

   p=getParameter("AnimationSpeed");   AnimationSpeed=(p==null)?100:Integer.valueOf(p).intValue();

   RocketSoundtrack=getParameter("RocketSoundtrack");
   p=getParameter("RocketStyleVariability");
   RocketStyleVariability=(p==null)?20:Integer.valueOf(p).intValue();
   p=getParameter("MaxRocketNumber");
   MaxRocketNumber=(p==null)?5:Integer.valueOf(p).intValue();
   p=getParameter("MaxRocketExplosionEnergy");
   MaxRocketExplosionEnergy=(p==null)?500:Integer.valueOf(p).intValue();
   p=getParameter("MaxRocketPatchNumber");
   MaxRocketPatchNumber=(p==null)?50:Integer.valueOf(p).intValue();
   p=getParameter("MaxRocketPatchLength");
   MaxRocketPatchLength=(p==null)?100:Integer.valueOf(p).intValue();
   p=getParameter("Gravity");
   Gravity=(p==null)?20:Integer.valueOf(p).intValue();
###############################
Here are the calls giving me theproblem
   mx=size().width-1;
   my=size().height-1;

   rocket=new Rocket[MaxRocketNumber];
   for(i=0;i<MaxRocketNumber;i++)rocket[i]=new Rocket(mx,my,Gravity);
   }
public void start()
   {
   if(thread==null)
      {
      thread=new Thread(this);
      thread.start();
      }
   }
public void stop()
   {
   if(thread!=null)
      {
      thread.stop();
      thread=null;
      }
   }
public void run()
   {
   int i,
       e=(int)(Math.random()*MaxRocketExplosionEnergy*3/4)+
         MaxRocketExplosionEnergy/4+1,
       p=(int)(Math.random()*MaxRocketPatchNumber*3/4)+
         MaxRocketPatchNumber/4+1,
       l=(int)(Math.random()*MaxRocketPatchLength*3/4)+
         MaxRocketPatchLength/4+1;
   long s=(long)(Math.random()*10000);
   boolean sleep;
   Graphics g=getGraphics();
   URL u=null;

   while(true)
      {
      try {
          thread.sleep(100/AnimationSpeed);
          }
      catch(InterruptedException x){}

      sleep=true;
      for(i=0;i<MaxRocketNumber;i++)
         sleep=sleep&&rocket[i].sleep;
      if(sleep&&Math.random()*100<RocketStyleVariability)
         {
         e=(int)(Math.random()*MaxRocketExplosionEnergy*3/4)+
           MaxRocketExplosionEnergy/4+1;
         p=(int)(Math.random()*MaxRocketPatchNumber*3/4)+
           MaxRocketPatchNumber/4+1;
         l=(int)(Math.random()*MaxRocketPatchLength*3/4)+
           MaxRocketPatchLength/4+1;
         s=(long)(Math.random()*10000);
         }

      for(i=0;i<MaxRocketNumber;i++)
         {
         if(rocket[i].sleep&&Math.random()*MaxRocketNumber*l<1)
            {
            try {
                u=new URL(getDocumentBase(),RocketSoundtrack);
                }
            catch(MalformedURLException x){}
            play(u);

            rocket[i].init(e,p,l,s);
            rocket[i].start();
            }
         rocket[i].show(g);
         }
      }
   }
public void paint(Graphics g)
   {
   g.setColor(Color.black);
   g.fillRect(0,0,mx+1,my+1);
   }
}

class Rocket
{
public boolean sleep=true;

private int energy,patch,length,
            mx,my,
            gravity,
            ox,oy,
            vx[],vy[],
            x,y,
            red,blue,green,
            t;
private Random random;

public Rocket(int a,int b,int g)
   {
   mx=a;
   my=b;

   gravity=g;
   }
public void init(int e,int p,int l,long seed)
   {
   int i;

   energy=e;
   patch=p;
   length=l;

   random=new Random(seed);

   vx=new int[patch];
   vy=new int[patch];

   red=(int)(random.nextDouble()*128)+128;
   blue=(int)(random.nextDouble()*128)+128;
   green=(int)(random.nextDouble()*128)+128;

   ox=(int)(Math.random()*mx/2)+mx/4;
   oy=(int)(Math.random()*my/2)+my/4;

   for(i=0;i<patch;i++)
      {
      vx[i]=(int)(Math.random()*energy)-energy/2;
      vy[i]=(int)(Math.random()*energy*7/8)-energy/8;
      }
   }
public void start()
   {
   t=0;
   sleep=false;
   }
public void show(Graphics g)
   {
   if(!sleep)
      if(t<length)
         {
         int i,c;
         double s;
         Color color;

         c=(int)(random.nextDouble()*64)-32+red;
         if(c>=0&&c<256)red=c;
         c=(int)(random.nextDouble()*64)-32+blue;
         if(c>=0&&c<256)blue=c;
         c=(int)(random.nextDouble()*64)-32+green;
         if(c>=0&&c<256)green=c;
         color=new Color(red,blue,green);

         for(i=0;i<patch;i++)
            {
            s=(double)t/100;
            x=(int)(vx[i]*s);
            y=(int)(vy[i]*s-gravity*s*s);

            g.setColor(color);
            g.drawLine(ox+x,oy-y,ox+x,oy-y);

            if(t>=length/2);
               {
               int j;
               for(j=0;j<2;j++)
                  {
                  s=(double)((t-length/2)*2+j)/100;
                  x=(int)(vx[i]*s);
                  y=(int)(vy[i]*s-gravity*s*s);

                  g.setColor(Color.black);
                  g.drawLine(ox+x,oy-y,ox+x,oy-y);
                  }
               }
            }

         t++;
         }
      else
         {
         sleep=true;
         }
   }
}

0
Comment
Question by:chuckd070397
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Expert Comment

by:mgk
ID: 1222693
Hi, deprecation means that a class you're using in this code has been replaced between JDK 1.0 and JDK 1.1, compiling using "-deprecation" in the command line will tell you what classes are deprecated. The docs that come with the JDK usually tell you what a class/method is replaced by if you look the old one's up. Normally it's just a case of conforming to the get/set standards of JavaBeans, so it would be Dimension d=getSize();

I'm less sure about the null pointer, I'd probably just check you have all the parameters in your applet tag and they're all spelt the same (especially the cases)

Mark King

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1222694
Mark's answer is mostly (if not completley) correct.

I'll only add that the NULL pointer is probably caused by the fact that the deprecated methods are not understood by the VM you are running under, so the VM return NULL whenever they are called.
0
 

Accepted Solution

by:
scott99 earned 200 total points
ID: 1222695
This may likely be a timing problem.

Recall that Applet is a Panel which is a Container which is a Component.

The width and height variables are initially null and get assigned during the setBounds() (which replaces deprecated reshape() ). If setBounds was not called yet, the width and height values in the Dimension that you get back from size() have not been initialized and are null. When you subtract 1 from a null value, that is where you get a null pointer exception.

mx=size().width-1;
my=size().height-1;

You may have to postpone this calculation until the doLayout() method .

0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question