I have a problem with this code and I do not know what is causing it.

// The "ArrowKeys" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ArrowKeys2 extends Applet implements KeyListener, Runnable
{
    int x = 50, dx, y = 50, dy, a = 0, score, total, locx, locy;
    double rand, location;
    char m;
    boolean get = false;
    private Image dbImage;
    private Graphics dbg;
    Thread thread;
    Image img, tile, background, target;
    Graphics backg;
    // Place instance variables here

    public void init ()
    {
        rand = Math.random ();
        location = Math.random () * 1000;
        locx = (int) Math.round (location);
        location = Math.random () * 1000;
        locy = (int) Math.round (location);
        img = getImage (getDocumentBase (), "bug1right.gif");
        if (rand > .63)
        {
            target = getImage (getDocumentBase (), "flblue.gif");
            score = 5;
        }
        else if (rand > .33)
        {
            target = getImage (getDocumentBase (), "flred.gif");
            score = 2;
        }
        else
        {
            target = getImage (getDocumentBase (), "flyellow.gif");
            score = 1;
        }

        //background = getImage (getDocumentBase (), "hedgelarge.gif");
        addKeyListener (this);
        /*
                tile = createImage (1280, 690);
                backg = tile.getGraphics ();
                backg.setColor (Color.white);
        */
        // Place the body of the initialization method here
    } // init method


    public void start ()
    {
        thread = new Thread (this);
        thread.start ();
    }


    public void stop ()
    {
        thread.stop ();
    }


    public void run ()
    {
        Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);
        while (true)
        {

            // Delay for 20 milliseconds
            try
            {
                Thread.sleep (20);
            }
            catch (InterruptedException e)
            {
            }

            if (dx == -2)
            {
                // If you hit an edge, reverse the direction
                img = getImage (getDocumentBase (), "bug1left.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (x <= locx + 50 && x >= locx)
                {
                get = true;
                }
            }
            else if (dx == 2)
            {
                img = getImage (getDocumentBase (), "bug1right.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (x >= locx && x <= locx + 50)
                {
                get = true;
                }
            }
            else if (dy == -2)
            {
                img = getImage (getDocumentBase (), "bug1up.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (y >= locy + 50 && y >= locy)
               {
                get = true;
                }
            }
            else if (dy == 2)
            {
                img = getImage (getDocumentBase (), "bug1down.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (y <= locy + 50 && y >= locy)
                {
                get = true;
                }

            }
            if ((x + dx < 0) || (x + dx > getSize ().width))
            {
                dx = 0;
            }
            if ((y + dy < 0) || (y + dy > getSize ().height))
            {
                dy = 0;
            }
            //calculate the new position
            x += dx;
            y += dy;

            if (get = true)
            {
                get = false;
                total += score;
                rand = Math.random ();
                location = Math.random () * 1000;
                locx = (int) Math.round (location);
                location = Math.random () * 1000;
                locy = (int) Math.round (location);

                if (rand > .63)
                {
                    target = getImage (getDocumentBase (), "flblue.gif");
                    score = 5;
                }
                else if (rand > .33)
                {
                    target = getImage (getDocumentBase (), "flred.gif");
                    score = 2;
                }
                else
                {
                    target = getImage (getDocumentBase (), "flyellow.gif");
                    score = 1;
                }
            }
            // reset thread priority
            Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);

        }

    } // while (true)





    public void paint (Graphics g)
    {

        //backg.drawImage(background, 0, 0, this);
        g.drawImage (target, locx, locy, 50, 50, this);
        g.drawImage (img, x, y, 50, 50, this);
        g.drawString ("Score: " + total, 10, 10);

        // backg.drawImage( background, 0, 0, this );
        // Place the body of the drawing method here
    } // paint method




    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();
        if (key == KeyEvent.VK_RIGHT)
        {
            //backg.drawImage (background, 0, 0, 1280, 960, this);
            dx = 2;
            dy = 0;
            x = x + dx;

        }
        else if (key == KeyEvent.VK_LEFT)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dx = -2;
            dy = 0;
            x = x + dx;

        }
        else if (key == KeyEvent.VK_UP)
        {

            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = -2;
            dx = 0;
            y = y + dy;

        }
        else if (key == KeyEvent.VK_DOWN)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = 2;
            dx = 0;
            y = y + dy;

        }

    }


    public void keyReleased (KeyEvent e)
    {

    }


    public void keyTyped (KeyEvent e)
    {

    }


    public void update (Graphics g)
    {
        // initialize doublebuffers
        if (dbImage == null)
        {
            dbImage = createImage (this.getSize ().width, this.getSize ().height);
            dbg = dbImage.getGraphics ();
        }

        // save background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw foreground on background
        dbg.setColor (getForeground ());
        paint (dbg);

        // Now indicate ready drawn picture Offscreen on the right screen
        g.drawImage (dbImage, 0, 0, this);
    }
} // ArrowKeys class

I added in the collision between the x and locx and the y and locy.

Thanks for any help.

which line is the nullPointerException?

I have a problem with this code and I do not know what is causing it.

This is a joke, yes? Do you understand what a "problem description" is?

If it's an NPE you could give us just the tinyest little clue to work on - like which line?

Can you state what part is your problem. I couldn't run your code unfortunately.

This is the problem that I get

java.lang.NullPointerException
        at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2744)
        at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2705)
        at ArrowKeys2.paint(ArrowKeys2.java:181)
        at sun.awt.RepaintArea.paint(RepaintArea.java:180)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
        at java.awt.Component.dispatchEventImpl(Component.java:3586)
        at java.awt.Container.dispatchEventImpl(Container.java:1437)
        at java.awt.Component.dispatchEvent(Component.java:3367)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:190)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:144)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)

and here is the shortened script (just deleted a few lines)

// The "ArrowKeys" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ArrowKeys2 extends Applet implements KeyListener, Runnable
{
    int x = 50, dx, y = 50, dy, a = 0, score, total, locx, locy;
    double rand, location;
    char m;
    boolean get = false;
    private Image dbImage;
    private Graphics dbg;
    Thread thread;
    Image img, tile, background, target;
    Graphics backg;
    // Place instance variables here

    public void init ()
    {
        rand = Math.random ();
        location = Math.random () * 1000;
        locx = (int) Math.round (location);
        location = Math.random () * 1000;
        locy = (int) Math.round (location);
        img = getImage (getDocumentBase (), "bug1right.gif");
        if (rand > .63)
        {
            target = getImage (getDocumentBase (), "flblue.gif");
            score = 5;
        }
        else if (rand > .33)
        {
            target = getImage (getDocumentBase (), "flred.gif");
            score = 2;
        }
        else
        {
            target = getImage (getDocumentBase (), "flyellow.gif");
            score = 1;
        }

        //background = getImage (getDocumentBase (), "hedgelarge.gif");
        addKeyListener (this);
        /*
                tile = createImage (1280, 690);
                backg = tile.getGraphics ();
                backg.setColor (Color.white);
        */
        // Place the body of the initialization method here
    } // init method


    public void start ()
    {
        thread = new Thread (this);
        thread.start ();
    }


    public void stop ()
    {
        thread.stop ();
    }


    public void run ()
    {
        Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);
        while (true)
        {

            // Delay for 20 milliseconds
            try
            {
                Thread.sleep (20);
            }
            catch (InterruptedException e)
            {
            }

            if (dx == -2)
            {
                // If you hit an edge, reverse the direction
                img = getImage (getDocumentBase (), "bug1left.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (x <= locx + 50 && x >= locx)
                {
                get = true;
                }
            }
            else if (dx == 2)
            {
                img = getImage (getDocumentBase (), "bug1right.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (x >= locx && x <= locx + 50)
                {
                get = true;
                }
            }
            else if (dy == -2)
            {
                img = getImage (getDocumentBase (), "bug1up.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (y >= locy + 50 && y >= locy)
               {
                get = true;
                }
            }
            else if (dy == 2)
            {
                img = getImage (getDocumentBase (), "bug1down.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (y <= locy + 50 && y >= locy)
                {
                get = true;
                }

            }
            if ((x + dx < 0) || (x + dx > getSize ().width))
            {
                dx = 0;
            }
            if ((y + dy < 0) || (y + dy > getSize ().height))
            {
                dy = 0;
            }
            //calculate the new position
            x += dx;
            y += dy;

            if (get = true)
            {
                
                total += score;
                rand = Math.random ();
                location = Math.random () * 1000;
                locx = (int) Math.round (location);
                location = Math.random () * 1000;
                locy = (int) Math.round (location);

                if (rand > .63)
                {
                    target = getImage (getDocumentBase (), "flblue.gif");
                    score = 5;
                }
                else if (rand > .33)
                {
                    target = getImage (getDocumentBase (), "flred.gif");
                    score = 2;
                }
                else
                {
                    target = getImage (getDocumentBase (), "flyellow.gif");
                    score = 1;
                }
                get = false;
            }
            // reset thread priority
            Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);

        }

    } // while (true)

    public void paint (Graphics g)
    {
        //backg.drawImage(background, 0, 0, this);
        g.drawImage (target, locx, locy, 50, 50, this);
        g.drawImage (img, x, y, 50, 50, this);
        g.drawString ("Score: " + total, 10, 10);

        // backg.drawImage( background, 0, 0, this );
        // Place the body of the drawing method here
    } // paint method

    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();
        if (key == KeyEvent.VK_RIGHT)
        {
            //backg.drawImage (background, 0, 0, 1280, 960, this);
            dx = 2;
            dy = 0;
            x = x + dx;

        }
        else if (key == KeyEvent.VK_LEFT)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dx = -2;
            dy = 0;
            x = x + dx;

        }
        else if (key == KeyEvent.VK_UP)
        {

            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = -2;
            dx = 0;
            y = y + dy;

        }
        else if (key == KeyEvent.VK_DOWN)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = 2;
            dx = 0;
            y = y + dy;

        }

    }

    public void keyReleased (KeyEvent e)
    {

    }


    public void keyTyped (KeyEvent e)
    {

    }

    public void update (Graphics g)
    {
        // initialize doublebuffers
        if (dbImage == null)
        {
            dbImage = createImage (this.getSize ().width, this.getSize ().height);
            dbg = dbImage.getGraphics ();
        }

        // save background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw foreground on background
        dbg.setColor (getForeground ());
        paint (dbg);

        // Now indicate ready drawn picture Offscreen on the right screen
        g.drawImage (dbImage, 0, 0, this);
    }
} // ArrowKeys class

By just deleting a few lines you have made the line number in the exception (ArrowKeys2.paint(ArrowKeys2.java:181)) useless.
Find out while line that is, look at all the variables that appear on it, then add a print statement immediately before that line printing the values of all those variables. This will show you which one is null

This often becomes a default route for the treatment of other types of errors. For example, searching for a word in a dictionary and return NULL if not found. Anyone using NULL without explicitly check will get a null pointer exception.

Ok I fixed the null pointer exception but now I a have problem with the collision detection. Can anyone help? Thanks

// The "ArrowKeys" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ArrowKeys2 extends Applet implements KeyListener, Runnable
{
    int x = 50, dx, y = 50, dy, a = 0, score, total, locx, locy;
    double rand, location;
    char m;
    boolean get = false;
    private Image dbImage;
    private Graphics dbg;
    Thread thread;
    Image img, tile, background, target;
    Graphics backg;
    // Place instance variables here

    public void init ()
    {
        rand = Math.random ();
        location = Math.random () * 1000;
        locx = (int) Math.round (location);
        location = Math.random () * 1000;
        locy = (int) Math.round (location);
        img = getImage (getDocumentBase (), "bug1right.gif");
        if (rand > .63)
        {
            target = getImage (getDocumentBase (), "flblue.gif");
            score = 5;
        }
        else if (rand > .33)
        {
            target = getImage (getDocumentBase (), "flred.gif");
            score = 2;
        }
        else
        {
            target = getImage (getDocumentBase (), "flyellow.gif");
            score = 1;
        }

        //background = getImage (getDocumentBase (), "hedgelarge.gif");
        addKeyListener (this);
        /*
                tile = createImage (1280, 690);
                backg = tile.getGraphics ();
                backg.setColor (Color.white);
        */
        // Place the body of the initialization method here
    } // init method


    public void start ()
    {
        thread = new Thread (this);
        thread.start ();
    }


    public void stop ()
    {
        thread.stop ();
    }


    public void run ()
    {
        Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);
        while (true)
        {

            // Delay for 20 milliseconds
            try
            {
                Thread.sleep (20);
            }
            catch (InterruptedException e)
            {
            }

            if (dx == -2)
            {
                // If you hit an edge, reverse the direction
                img = getImage (getDocumentBase (), "bug1left.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (x >= locx + 50 && x <= locx)
                {
                get = true;
                }            }
            else if (dx == 2)
            {
                img = getImage (getDocumentBase (), "bug1right.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (x >= locx && x <= locx + 50)
                {
                get = true;
                }            }
            else if (dy == -2)
            {
                img = getImage (getDocumentBase (), "bug1up.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (y >= locy + 50 && y <= locy)
               {
                get = true;
                }            }
            else if (dy == 2)
            {
                img = getImage (getDocumentBase (), "bug1down.gif");
                for (int i = 0 ; i < 500000 ; i++)
                {
                }
                repaint ();
                if (y <= locy + 50 && y >= locy)
                {
                get = true;
                }
            }
            if ((x + dx < 0) || (x + dx > getSize ().width))
            {
                dx = 0;
            }
            if ((y + dy < 0) || (y + dy > getSize ().height))
            {
                dy = 0;
            }
            //calculate the new position
            x += dx;
            y += dy;

            if (get = true)
            {
                get = false;
                total += score;
                rand = Math.random ();
                location = Math.random () * 1000;
                locx = (int) Math.round (location);
                location = Math.random () * 1000;
                locy = (int) Math.round (location);

                if (rand > .63)
                {
                    target = getImage (getDocumentBase (), "flblue.gif");
                    score = 5;
                }
                else if (rand > .33)
                {
                    target = getImage (getDocumentBase (), "flred.gif");
                    score = 2;
                }
                else
                {
                    target = getImage (getDocumentBase (), "flyellow.gif");
                    score = 1;
                }
                
            }
            // reset thread priority
            Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);

        }

    } // while (true)

    public void paint (Graphics g)
    {
        //backg.drawImage(background, 0, 0, this);
        g.drawImage (target, locx, locy, 50, 50, this);
        g.drawImage (img, x, y, 50, 50, this);
        g.drawString ("Score: " + total, 10, 10);

        // backg.drawImage( background, 0, 0, this );
        // Place the body of the drawing method here
    } // paint method

    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();
        if (key == KeyEvent.VK_RIGHT)
        {
            //backg.drawImage (background, 0, 0, 1280, 960, this);
            dx = 2;
            dy = 0;
            x = x + dx;

        }
        else if (key == KeyEvent.VK_LEFT)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dx = -2;
            dy = 0;
            x = x + dx;

        }
        else if (key == KeyEvent.VK_UP)
        {

            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = -2;
            dx = 0;
            y = y + dy;

        }
        else if (key == KeyEvent.VK_DOWN)
        {
            //backg.drawImage (img, 0, 0, 1280, 960, this);
            dy = 2;
            dx = 0;
            y = y + dy;

        }

    }

    public void keyReleased (KeyEvent e)
    {

    }


    public void keyTyped (KeyEvent e)
    {

    }

    public void update (Graphics g)
    {
        // initialize doublebuffers
        if (dbImage == null)
        {
            dbImage = createImage (this.getSize ().width, this.getSize ().height);
            dbg = dbImage.getGraphics ();
        }

        // save background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw foreground on background
        dbg.setColor (getForeground ());
        paint (dbg);

        // Now indicate ready drawn picture Offscreen on the right screen
        g.drawImage (dbImage, 0, 0, this);
    }
} // ArrowKeys class

The red is the collision detection. I would like the leading edge to colide with the part of the picture. (ex. if the bug is going right it will collide with the left side of the target.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.