Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: null pointer exception, please help!

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy null pointer exception, please help!

    .
    Last edited by tkocher13; June 11th, 2014 at 10:21 AM. Reason: ignore


  2. #2
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: null pointer exception, please help!

    Welcome tkocher13 to the forum. Please read this topic to learn how to post code in code or highlight tags and other useful info for new members. I think your program would be quite readable easily if you follow read this way of printing your codes.

    Thank You

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    My Mood
    Angry
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy null pointer exception, please help!

    For a class project i have to create a game of my choice. I chose to do "the worlds hardest game". I don't have a whole lot of experience using applets but what I have gotten so far is the moving character and the level background. I also have the enemy objects working in a separate program but when i try to put it all together i keep getting a null pointer exception error every time i try to start the game and get the enemies to appear. HELP!! heres my code:

     
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
     
     
    public class FinalAssignment extends JApplet
    implements KeyListener, FocusListener, MouseListener
    {
    //Button start = new Button ("Start Game");
    Checkbox startGame = new Checkbox ("Start Game");
    Canvas display = new Canvas ();
    int myX = 400;
    int myY = 400;
    int PlayerX = 15; // These are beginning coordinates for the
    int PlayerY = 15; // Player's circle
    int x;
    int y;
    int squareLeft = 250;
    int SQUARE_SIZE = 250;
    int squareTop = 250;
    boolean focussed = false; // True when this applet has input focus.
    DisplayPanel canvas; // The drawing surface on which the applet draws,
    // belonging to a nested class DisplayPanel, which
    // is defined below.
     
     
    public void init ()
    {
     
    super.init ();
    canvas = new DisplayPanel (); // Create drawing surface and
    setContentPane (canvas); // install it as the applet's content pane.
     
    this.getContentPane ().add (startGame);
     
    canvas.addFocusListener (this); // Set up the applet to listen for events
    canvas.addKeyListener (this); // from the canvas.
    canvas.addMouseListener (this);
     
    Canvas display = new Canvas ();
    display.resize (WIDTH, HEIGHT);
    this.getContentPane ().add ("Center", display);
     
    } // init method
     
     
     
     
    public boolean action (Event e, Object o)
    {
     
    final int RADIUS = 7;
    final int RADIUS2 = 7;
    final int RADIUS3 = 7;
    final int RADIUS4 = 7;
    Graphics displayG;
    int x1, y1, dx, dy, x2, y2, x3, y3, x4, y4, dx2, dy2, dx3, dy3, dx4, dy4;
    displayG = display.getGraphics ();
     
    x1 = 195;
    y1 = 15;
    x2 = 195;
    y2 = 55;
    x3 = 195;
    y3 = 75;
    x4 = 195;
    y4 = 115;
    drawBall (displayG, x1, y1, RADIUS, Color.blue); //**************************************
    drawBall2 (displayG, x2, y2, RADIUS2, Color.blue);
    drawBall3 (displayG, x3, y3, RADIUS3, Color.blue);
    drawBall4 (displayG, x4, y4, RADIUS4, Color.blue);
    dx = 1;
    dy = 0;
    dx2 = -1;
    dy2 = 0;
    dx3 = 1;
    dy3 = 0;
    dx4 = -1;
    dy4 = 0;
     
    for (int i = 0 ; i < 100000 ; i++) //loops for how long it will move
    {
    drawBall (displayG, x1, y1, RADIUS, getBackground ());
    x1 += dx; //movement of the ball
    y1 += dy; //movement of the ball
    if (x1 <= RADIUS || x1 >= WIDTH - RADIUS)
    dx = -dx; //determine which direction
    if (y1 <= RADIUS || y1 >= HEIGHT - RADIUS)
    dy = -dy; //determine which direction
    drawBall (displayG, x1, y1, RADIUS, Color.blue); //draw the ball
     
    drawBall2 (displayG, x2, y2, RADIUS2, getBackground ());
    x2 += dx2; //movement of the ball
    y2 += dy2; //movement of the ball
    if (x2 <= RADIUS2 || x2 >= WIDTH - RADIUS2)
    dx2 = -dx2; //determine which direction
    if (y2 <= RADIUS2 || y2 >= HEIGHT - RADIUS2)
    dy2 = -dy2; //determine which direction
    drawBall2 (displayG, x2, y2, RADIUS2, Color.blue); //draw the ball
     
    drawBall3 (displayG, x3, y3, RADIUS3, getBackground ());
    x3 += dx3; //movement of the ball
    y3 += dy3; //movement of the ball
    if (x3 <= RADIUS2 || x3 >= WIDTH - RADIUS3)
    dx3 = -dx3; //determine which direction
    if (y3 <= RADIUS2 || y3 >= HEIGHT - RADIUS3)
    dy3 = -dy3; //determine which direction
    drawBall3 (displayG, x3, y3, RADIUS3, Color.blue); //draw the ball
     
    drawBall4 (displayG, x4, y4, RADIUS4, getBackground ());
    x4 += dx4; //movement of the ball
    y4 += dy4; //movement of the ball
    if (x4 <= RADIUS || x4 >= WIDTH - RADIUS4)
    dx4 = -dx4; //determine which direction
    if (y4 <= RADIUS2 || y4 >= HEIGHT - RADIUS3)
    dy4 = -dy4; //determine which direction
    drawBall4 (displayG, x4, y4, RADIUS4, Color.blue); //draw the ball
     
     
    ballDelay (3000000); // Wastes time to produce delay.
     
     
    }
    return false;
    } // action method
     
     
    //Procedure for delays for drawing
    public static void delay (int de)
     
    {
    try
    {
    Thread.sleep (de);
    }
     
     
    catch (Exception e)
    {
     
    }
    }
     
     
    class DisplayPanel extends JPanel
    {
    // An object belonging to this nested class is used as
    // the content pane of the applet. It displays the
    // moving square on a white background with a border
    // that changes color depending on whether this
    // component has the input focus or not.
    public void paintComponent (Graphics g)
    {
    //int radius = 7;
    Dimension d = getSize ();
    g.setColor (Color.WHITE);
    g.fillRect (0, 0, d.width, d.height);
     
     
     
    g.setColor (Color.BLACK);
    g.fillRect (10, 10, 2, 200);
    g.fillRect (10, 10, 80, 2);
    g.fillRect (90, 10, 2, 160);
    g.fillRect (10, 210, 160, 2);
    g.fillRect (90, 170, 40, 2);
    g.fillRect (130, 50, 2, 122);
    g.fillRect (130, 50, 360, 2);
    g.fillRect (490, 10, 2, 42);
    g.fillRect (170, 170, 2, 42);
    g.fillRect (170, 170, 360, 2);
    g.fillRect (530, 50, 2, 122);
    g.fillRect (530, 50, 40, 2);
    g.fillRect (570, 50, 2, 160);
    g.fillRect (570, 210, 80, 2);
    g.fillRect (650, 10, 2, 202);
    g.fillRect (490, 10, 160, 2);
     
    g.setColor (new Color (190, 255, 190));
    g.fillRect (12, 12, 78, 198);
    g.fillRect (572, 12, 78, 198);
     
     
     
     
    // Player's circle
    g.setColor (Color.RED);
    g.fillRect (PlayerX, PlayerY, 20, 20);
     
     
    } // End PaintComponent Method
    } // End Nested Class DisplayPanel
     
     
    public void drawBall (Graphics g, int x1, int y1, int RADIUS, Color clr)
    {
    g.setColor (clr); //clears where the ball has moved ************************************************** ***
    g.fillOval (x1 - RADIUS + 130, y1 - RADIUS + 50, 2 * RADIUS, 2 * RADIUS); //determines the boundaries of the ball
    }
     
     
    public void drawBall2 (Graphics g, int x2, int y2, int RADIUS2, Color clr)
    {
    g.setColor (clr); //clears where the ball has moved
    g.fillOval (x2 - RADIUS2 + 130, y2 - RADIUS2 + 40, 2 * RADIUS2, 2 * RADIUS2); //determines the boundaries of the ball
    } // drawBall method
     
     
    public void drawBall3 (Graphics g, int x3, int y3, int RADIUS3, Color clr)
    {
    g.setColor (clr); //clears where the ball has moved
    g.fillOval (x3 - RADIUS3 + 130, y3 - RADIUS3 + 50, 2 * RADIUS3, 2 * RADIUS3); //determines the boundaries of the ball
     
    } // drawBall method
     
     
    public void drawBall4 (Graphics g, int x4, int y4, int RADIUS4, Color clr)
    {
    g.setColor (clr); //clears where the ball has moved
    g.fillOval (x4 - RADIUS4 + 130, y4 - RADIUS4 + 40, 2 * RADIUS4, 2 * RADIUS4); //determines the boundaries of the ball
    } // drawBall method
     
     
    public void ballDelay (int howLong)
    {
    // This wastes time.
    for (int i = 1 ; i <= howLong ; i++)
    {
    double garbage = Math.PI * Math.PI;
    }
    }
     
     
    public void focusGained (FocusEvent evt)
    {
    // The applet now has the input focus.
    focussed = true;
    }
     
     
    public void focusLost (FocusEvent evt)
    {
    // The applet has now lost the input focus.
    focussed = false;
    }
     
     
    public void keyPressed (KeyEvent evt)
    {
    // Called when the user has pressed a key, which can be
    // a special key such as an arrow key. If the key pressed
    // was one of the arrow keys, move the square (but make sure
    // that it doesn't move off the edge, allowing for a
    // 3-pixel border all around the applet).
     
    int key = evt.getKeyCode (); // keyboard code for the key that was pressed
     
    if (key == KeyEvent.VK_LEFT)
    {
    squareLeft -= 5;
    canvas.repaint ();
    PlayerX -= 5;
    }
     
     
    else if (key == KeyEvent.VK_RIGHT)
    {
    PlayerX += 5;
    delay (5);
    canvas.repaint ();
    squareLeft += 5;
    canvas.repaint ();
    }
     
     
    else if (key == KeyEvent.VK_UP)
    {
    PlayerY -= 5;
    squareTop -= 5;
    canvas.repaint ();
    }
     
     
    else if (key == KeyEvent.VK_DOWN)
    {
    PlayerY += 5;
    squareTop += 5;
    canvas.repaint ();
    }
    } // end keyPressed()
     
     
    public void keyTyped (KeyEvent evt)
    {
    }
     
     
    public void keyReleased (KeyEvent evt)
    {
    // empty method, required by the KeyListener Interface
    }
     
     
    public void mousePressed (MouseEvent evt)
    {
    // Request that the input focus be given to the
    // canvas when the user clicks on the applet.
    canvas.requestFocus ();
    }
     
     
    public void mouseEntered (MouseEvent evt)
    {
    } // Required by the
     
     
    public void mouseExited (MouseEvent evt)
    {
    } // MouseListener
     
     
    public void mouseReleased (MouseEvent evt)
    {
    } // interface.
     
     
    public void mouseClicked (MouseEvent evt)
    {
    }
     
     
     
     
     
    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: null pointer exception, please help!

    What line throws the error? What variable is null on that line?

    Use a debugger, or at least add some print statements, to answer those questions. When you have that figured out, you can work backwards to figure out *why* it's null, then you can work on fixing it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: null pointer exception, please help!

    Come on! When I told gave you the advice I didn't tell you to make a new thread. Do you even know you could have EDITED your thread? Yes you can edit your thread by simply clicking on edit like you have done previously and copy paste your entire problem with code highlighting. Ok, since you have done the non-needful, all I can say is don't do it next time. READ the rules of this forum carefully or your threads are subject to editing by moderators or movement or deletion if any rule is violated.

    Thank you.

    --- Update ---

    See, the thread has been merged.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: null pointer exception, please help!

    Please edit the code and format it properly. Logically nested statements(within {}s) should be indented.
    Too many statements in the posted code incorrectly start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. null pointer exception
    By ayswaryaes in forum AWT / Java Swing
    Replies: 1
    Last Post: February 4th, 2014, 02:50 AM
  2. Need Help with Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 23rd, 2012, 03:20 PM
  3. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 08:32 PM
  4. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 11:07 AM
  5. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM