1 2 Previous Next 18 Replies Latest reply: Mar 18, 2007 10:22 PM by 807606 RSS

    Throwing a Thread Exception And I Don't Know WHY!!!!

    807606
      Could someone read over my code, run it, and tell me why it throws a null pointer thread exception.
      import java.awt.*;
      import java.applet.*;
      import java.util.*;
      import javax.swing.*;
      
      public class CoffeeDrop extends Applet implements Runnable{
           
           ImageIcon dcc;
           Random gen = new Random();
           int locx =0;
           int locy =50;
           int dccx = 100;
           int score = 0;
           int spnum =5;
           int droplive=0;
           Event e;
           int k;
           Thread t;
           ArrayList<Drop> darray = new ArrayList<Drop>();
           public void init()
                {
                dcc = new ImageIcon("dcc.jpg");
                Runnable r = new CoffeeDrop();
                t =new Thread(r);
                t.start();
                
                }
                
           public void run(){
                spawn(1);
                System.out.println("Run");
                while(droplive < 10)
                {
                     move();
                     try
                     {
                          t.sleep(100);
                     }
                     catch(Exception e)
                     {
                          System.out.println("Exception"+e.toString());
                     }
                     
                }
                }
      
           public void paint(Graphics g) {
                g.drawRect(50,50,400,400);
           }
           
           public void spawn(int n)
           {
                for(int i =0; i<n; i++)
                {
                int locx = ((gen.nextInt(390)+50));
                darray.add(new Drop(locx,locy));
           }
           }
           
           public void move()
           {
                System.out.println("Move"+darray.size());
                Graphics g =this.getGraphics();
                for(int z =0; z<darray.size();z++)
                {
                     if((darray.get(z)).getdlocy()<500&&darray.get(z) != null)
                     {
                          g.setColor(Color.white);
                          g.fillOval((darray.get(z)).getdlocx(),(darray.get(z)).getdlocy(),10,10);
                          darray.set(z,new Drop((darray.get(z)).getdlocx(),(darray.get(z)).getdlocy()+10));
                          g.setColor(Color.black);
                          g.fillOval((darray.get(z)).getdlocx(),(darray.get(z)).getdlocy(),10,10);
                          Catch();
                     }
                
           else
           {
                darray.remove(z);
                spawn(1);
           }
           }
           }
           
           
           public void Catch()
           {
                Graphics g =this.getGraphics();
                for(int i =0; i<darray.size();i++)
                {
                if((darray.get(i)).getdlocx()<(dcc.getIconWidth()+dccx)&&(darray.get(i)).getdlocx()>dccx&&
                (darray.get(i)).getdlocy()>450&&(darray.get(i)).getdlocy()<(dcc.getIconHeight()+450))
                {
                     
                     score+=100;
                     g.setColor(Color.white);
                     g.fillRect(40,40,70,10);
                     g.setColor(Color.black);
                     g.drawString("score "+score,50,50);
                     darray.remove(i);
                     spawn(1);
                     g.setColor(Color.white);
                     g.fillRect(40,490,450,25);
                     
                }
                else if((darray.get(i)).getdlocx()>=500||(darray.get(i)).getdlocy()>=500)
                {
                     darray.remove(i);
                     spawn(1);
                     g.setColor(Color.white);
                     g.fillRect(40,490,450,25);
                     droplive++;
                }
                else
                {
                          g.setColor(Color.white);
                     g.fillRect(40,490,450,25);
                }
           }
           }
           public boolean keyDown(Event e, int key)
           {
                     Graphics g =this.getGraphics();
                     int k = key;
                     Event event = e;
                     switch(k)
                     {
                          case 1006: 
                               if(dccx>50)
                               {
                                    g.setColor(Color.white);
                                    g.fillRect(dccx,450,dcc.getIconWidth(),dcc.getIconWidth());
                                         dcc.paintIcon(this,g,dccx-=10,450); break;
                               }
                          case 1007: 
                               if(dccx<400)
                                    {
                                    g.setColor(Color.white);
                                    g.fillRect(dccx,450,dcc.getIconWidth(),dcc.getIconWidth());
                                         dcc.paintIcon(this,g,dccx+=10,450); break;
                                    }
                     }
                     return true;
           }
      }
      You also need the Drop Class
      public class Drop
      {
           int dlocx =0;
           int dlocy =0;
           public Drop(int x, int y)
           {
                dlocx = x;
                dlocy = y;
           }
           public int getdlocx()
           {
                return dlocx;
           }
           public int getdlocy()
           {
                return dlocy;
           }
           public void setdlocx(int x)
           {
                dlocx = x;
           }
           public void setdlocy(int y)
           {
                dlocy = y;
           }
           }
        1 2 Previous Next