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: Dont understand the nullpointerexception

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dont understand the nullpointerexception

    package src;
     
    import javax.swing.JFrame;
    import java.awt.Graphics;
     
    public class GameApplet extends JFrame {
     
    	public static GameApplet client;
     
    	public GameApplet() {
    		setTitle("GameApplet - Alpha 1.0.0");
    		setSize(1000, 1000);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setResizable(false);
    		setVisible(true);
    	}
     
    	public static void createTiles(int X, int Y, int Height, int Width) {
    		client.paint(null, X, Y, Height, Width);
    	}
     
    	public void paint(Graphics g, int X, int Y, int Height,  int Width) {
               g.drawRect(X, Y, Height, Width);
        }
    	public static void main(String[] args) {
    		createTiles(100, 100, 300, 300);
    	}
    }
     
    I understand the nullpointerexception means I have a null value but don't understand where


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    586
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Dont understand the nullpointerexception

    public static void createTiles(int X, int Y, int Height, int Width) {
    	client.paint(null, X, Y, Height, Width);
    }
    client is null. You've never instanciated it, but I don't see why you need that anyway.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dont understand the nullpointerexception

    Quote Originally Posted by PhHein View Post
    public static void createTiles(int X, int Y, int Height, int Width) {
    	client.paint(null, X, Y, Height, Width);
    }
    client is null. You've never instanciated it, but I don't see why you need that anyway.
    I got rid of that to make this:
    public static GameApplet client;
     
    	public GameApplet() {
    		setTitle("GameApplet - Alpha 1.0.0");
    		setSize(1000, 1000);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setResizable(false);
    		setVisible(true);
    	}
     
    	public static void createTiles(int X, int Y, int Height, int Width) {
    		paint(null, X, Y, Height, Width);
    	}
     
    	public static void paint(Graphics g, int X, int Y, int Height,  int Width) {
               g.drawRect(X, Y, Height, Width);
        }
    	public static void main(String[] args) {
    		createTiles(100, 100, 300, 300);
    	}
    }

    Yet the same error still occurs.

    Points of interest are lines:
    at src.GameApplet.paint(GameApplet.java:23)
    at src.GameApplet.createTiles(GameApplet.java:19)
    at src.GameApplet.main(GameApplet.java:26)

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    586
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Dont understand the nullpointerexception

    You call paint with null as the first parameter. This cannot work.

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Dont understand the nullpointerexception

    Quote Originally Posted by PhHein View Post
    You call paint with null as the first parameter. This cannot work.
    Hmm, it worked when I wrote it differently yesterday. What parameter for graphics should it be? Only just started graphic methods/stuff.

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    586
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Dont understand the nullpointerexception

    I suggest you go back to the books for a while. There's so much wrong in your code. I have no idea whether you're trying to create an applet or a Swing application, all the stuff is static, violations of the coding standards etc.

Similar Threads

  1. Replies: 1
    Last Post: February 13th, 2013, 08:07 AM
  2. My code has error when its run....I dont understand what's wrong of it.
    By jacky@~ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 11th, 2011, 07:48 AM
  3. Dont quite understand how Scanner.nextLine() works
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 14
    Last Post: September 21st, 2011, 05:27 PM
  4. I dont understand why this happens....
    By ashenwolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 09:31 PM
  5. Dont understand what to write.. :/
    By johnwalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 08:31 PM