Results 1 to 4 of 4
  1. #1
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default Problem with null pointer exception?

    Here is the code:

    Java Code:
    package javaapplication1;
    
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener.*;
    import java.awt.event.*;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class CallDummy {
        static Dummy DI = new Dummy();
        static Graphics g = DI.getGraphics();
        public static void main (String args[]){
            DI.Init();
            DI.run();
        }
    }
    class Dummy extends JPanel implements KeyListener,Runnable{
       Image BackGround;
       Image Char;
       Dimension size;
       int nX=0, nY = 0, nKey;
       Thread tGame = null;
       CallDummy poo = new CallDummy();
       JFrame frame = new JFrame("Game");
        public Dummy() {
            size = new Dimension();
            BackGround = new ImageIcon(this.getClass().getResource("123.png")).getImage();
            size.width = BackGround.getWidth(null);
            size.height = BackGround.getHeight(null);
            setPreferredSize(size);
            Char = new ImageIcon(this.getClass().getResource("thing.png")).getImage();
        }
        public void keyPressed(java.awt.event.KeyEvent e) {
            nKey=e.getKeyCode();
            if(nKey==KeyEvent.VK_RIGHT){
                nX+=2;
            }
            if(nKey==KeyEvent.VK_LEFT){
                if(nX!=0){
                    nX-=2;
                }
            }
            if(nKey==KeyEvent.VK_UP){
                if(nY!=0){
                    nY-=2;
                }
            }
            if(nKey==KeyEvent.VK_DOWN){
                nY+=2;
            }
        }
    
        public void keyReleased(java.awt.event.KeyEvent e) {
            nKey=e.getKeyCode();
        }
        public void keyTyped(java.awt.event.KeyEvent e){}
        public void run(){
             try{
                while(true){
                    nX+=20;
                    Paint();
                    tGame.sleep(1000);
                }
             }
             catch(InterruptedException e){}
        }
        public void Paint() {
            //Graphics2D g2d = (Graphics2D) g;
            Graphics g = poo.g;
            g.drawImage(BackGround, 0, 0, null);
            g.drawImage(Char, nX, nY, null);
        }
        public void Init(){
            addKeyListener(this);
            frame.add(new Dummy());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            if(tGame == null){
                tGame = new Thread(this);
                tGame.start();
            }
        }
    }
    Here is the error:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
            at javaapplication1.Dummy.Paint(CallDummy.java:75)
            at javaapplication1.Dummy.run(CallDummy.java:66)
            at javaapplication1.CallDummy.main(CallDummy.java:19)
    Exception in thread "Thread-2" java.lang.NullPointerException
            at javaapplication1.Dummy.Paint(CallDummy.java:75)
            at javaapplication1.Dummy.run(CallDummy.java:66)
            at java.lang.Thread.run(Thread.java:619)

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Ok the code is actually a bit of mess if you don't mind me being forward.

    Quite a few basics are missing.

    1.) You don't start a thread by calling the run method. You call the start method. You'll need to read the threading tutorial.
    2.) You haven't correctly overridden the paint method. The method should start as
    Java Code:
    public void paint(Graphics g) { ...
    Notice that paint has a small p and the method takes a Graphics object. This mistake is what threw you off because you then tried some very strange things to get at that graphics object in your method. The attempts failed because you ended up at a null graphics object anyway and that's what caused the N.P.E. Read the painting article.

  3. #3
    dunafrothint is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    First off, the thread isn't my code. Second off, I'm not retarded. I understand that paint requires a lower case p and it imports Graphics g, but what I'm trying to do is find out how to initialize graphics g, because I need it to create a buffered image of my collection of images that I'm importing. How about we try this again with the code that I have there, cuz I can't use a non-static paint method and also, your mom told me to tell you you're a douchebag and that you never were able to take advantage of her quite like I did.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Oh man, get lost if you don't want any advice.

    IgnoreList++
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  3. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  4. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 AM
  5. Integration Problem-Null pointer Exception
    By uhdam in forum AWT / Swing
    Replies: 0
    Last Post: 04-07-2009, 11:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •