5 Replies Latest reply: Mar 29, 2011 9:50 PM by EJP RSS

    NullPointerException problem

    804374
      Hi,
      When I tried to run my source code, it had an error that said,
      Exception in thread "main" java.lang.NullPointerException
           at Stages.FirstStage.drawImages(FirstStage.java:42)
           at Stages.FirstStage.<init>(FirstStage.java:13)
           at clashofthenintendodemo.MainConfig$1.<init>(MainConfig.java:25)
           at clashofthenintendodemo.MainConfig.<init>(MainConfig.java:25)
           at clashofthenintendodemo.ClashofTheNintendoDemo.main(ClashofTheNintendoDemo.java:23)
      Java Result: 1
      I was trying to search for the error, but I couldn't seem to figure out the problem
      Here is my code

      Main Class
      public class ClashofTheNintendoDemo {
         public MainConfig MC = new MainConfig();
         
          
          public static void main (String[] args){
              JFrame f = new JFrame();
              f.setTitle("ClashOfTheNintendo");
              f.add(new MainConfig());
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.setSize(550,500);
              f.setVisible(true);
              f.setLocationRelativeTo(null);
          }
      
      
      }
      Class Neutral Moves
      public class NeutralMoves implements KeyListener, ActionListener {
      
          public NeutralMoves() {
              MarioFrame = IdleRight;
              x = StageStartValueX;
              y = StageStartValueY;
          }
          /////Place Values//////
          public int x;
          public int y;
          public int dx;
          public int dy;
          public int StageStartValueX = 400;
          public int StageStartValueY = 400;
          ////Move Booleans////
          public boolean RunningRight = false;
          public boolean RunningLeft = false;
          public boolean RunningStopRight = false;
          public boolean RunningStop = false;
          //////Other Booleans//////
          public boolean MarioIsIdle = true;
          ////Divider///////////
          public float TimePressed;
          ///Timers////////////
          public Timer RunTimmer;
          public Timer StopRunningTimmer;
          //NeutralMoves(LoadFrames)//
          Image MarioFrame;
          Image IdleRight = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario\\idel frames\\Right\\IdleGif.gif.gif").getImage();
          Image IdleLeft = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario\\idel frames\\Left\\IdleGif.gif").getImage();
          Image RunRight = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario\\RunningRight.gif").getImage();
          Image RunLeft = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\MarioGameSprites\\Mario\\RunningLeft.gif").getImage();
          ////////////More Dividers///////////////
      
          public void ifIdle() {
              if (MarioIsIdle = true) {
                  MarioFrame = IdleRight;
              }
          }
      
          public void RunningConfig() {
              ///Boolean Difiners
              if (RunningRight = true) {
                  RunTimmer.start();
              }
              if (RunningRight = false) {
                  RunTimmer.stop();
              }
              if (RunningLeft = true) {
                  RunTimmer.start();
              }
              if (RunningLeft = false) {
                  RunTimmer.start();
              }
              if (RunningStop = true) {
                  RunTimmer.stop();
                  StopRunningTimmer.start();
              }
              ////Divider//////////////
      
      
          }
      
          @Override
          public void keyTyped(KeyEvent e) {
              e.consume();
          }
      
          @Override
          public void keyPressed(KeyEvent e) {
              int key = e.getKeyCode();
              RunningRight = true;
              if (key == KeyEvent.VK_RIGHT) {
                  if (RunningRight) {
                      while (RunningRight) {
                          if (System.currentTimeMillis() < 1000 && System.currentTimeMillis() > 0) {
                              dx += 1;
                          }
                          if (System.currentTimeMillis() < 2000 && System.currentTimeMillis() > 1001) {
                              dx += 2;
                          }
                          if (System.currentTimeMillis() >= 3000 && System.currentTimeMillis() > 2001) {
                              dx += 3;
                          }
                          if (System.currentTimeMillis() >= 3001) {
                              RunningStop = true;
                              dx += 3;
                          }
      
      
                      }
                  }
                  e.consume();
                  MarioFrame = RunRight;
              }
              if (key == KeyEvent.VK_LEFT) {
                  RunningLeft = true;
                  if (RunningLeft = true) {
                      while (RunningLeft = true) {
                          if (System.currentTimeMillis() < 1000 && System.currentTimeMillis() > 0) {
                              dx -= -1;
      
                          }
                          if (System.currentTimeMillis() < 2000 && System.currentTimeMillis() > 1001) {
                              dx -= -2;
      
                          }
                          if (System.currentTimeMillis() >= 3000 && System.currentTimeMillis() < 2001) {
                              dx -= -3;
      
                          }
                      }
      
                  }
                  e.consume();
                  MarioFrame = RunLeft;
              }
          }
      
          @Override
          public void keyReleased(KeyEvent e) {
              int key = e.getKeyCode();
              RunningStop = true;
              RunningRight = false;
              if (key == KeyEvent.VK_RIGHT) {
                  if (RunningRight = false) {
                      RunningStop = true;
                      while (RunningStop) {
                          if (System.currentTimeMillis() < 1000 && System.currentTimeMillis() > 0) {
                              dx -= -1;
      
                          }
                          if (System.currentTimeMillis() < 2000 && System.currentTimeMillis() > 1001) {
                              dx -= -2;
      
                          }
                          if (System.currentTimeMillis() >= 3000 && System.currentTimeMillis() < 2001) {
                              dx -= -3;
      
                          }
                      }
      
                      RunTimmer.stop();
                      RunningRight = false;
                      MarioFrame = IdleRight;
                  }
                  if (key == KeyEvent.VK_LEFT) {
                      RunningLeft = false;
                      MarioFrame = IdleLeft;
                  }
      
              }
          }
      
          public void MarioCollision() {
      
              Rectangle Platforms = new Rectangle();
              if (RunningRight = true) {
              }
          }
      
          ////graphic meathods(So graphics show)///
      
      
          public void actionPerformed(ActionEvent e) {
              Movement();
          }
      
      
      
          public class getKeys extends KeyAdapter {
      
              NeutralMoves NeutralMoves = new NeutralMoves();
      
              public void keyPressed(KeyEvent e) {
                  NeutralMoves.keyPressed(e);
              }
      
              public void keyReleased(KeyEvent e) {
                  NeutralMoves.keyReleased(e);
              }
          }
      
          public void draw(Graphics2D MarioShow) {
              MarioShow.drawImage(MarioFrame, getX(), getY(), null);
      
      
          }
      
          /////Movement//////
          public void Movement() {
              x += x + dx;
              y += y + dy;
          }
      
          ///Just Return Values///
          public int getX() {
              return x;
          }
      
          public int getY() {
              return y;
          }
      
          public int getDx() {
              return dx;
          }
      
          public int getDy() {
              return dy;
          }
      }
      Class FirstStage
      public abstract class FirstStage extends JPanel{
          
          public FirstStage() {     
              this.drawImages(null);
          }
          //Imagesfor DemoStage
          public Image BackgroundDemo = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\TemplatesCOTN\\BackgroundDemoStage.png").getImage();
          public Image DemoPlatformPic = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\TemplatesCOTN\\GrayOrEvilGround.png").getImage();
          public Image Clouds = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\TemplatesCOTN\\DarkClouds.png").getImage();
          //////Rectangles//////
          public Platform PlatformCol = new Platform(); 
          public Rectangle Char = new Rectangle();
          ////////divider///////////
          
          
          /*
          public void Collision(){
                          
          }*/
          
         //Define the new Rectangle
          public class Platform {
      
              public Rectangle PlatformCol = new Rectangle();
      
              public Rectangle getBounds() {
                  return new Rectangle(DemoPlatformPic.getHeight(null), DemoPlatformPic.getWidth(null));
              }
          }
          
          ///Draw Methods////
          public void drawImages(Graphics2D g){
              g.drawImage(BackgroundDemo, 0, 0,null);
              g.drawImage(Clouds, 0, 0,null);
              g.drawImage(DemoPlatformPic, -200, 0,null);
          }
          
      }
      Class MainConfig
      public class MainConfig extends JPanel{
      
         public MainConfig(){
             load();
         }
         ////Load Characters////
         public Mario.NeutralMoves Mario = new Mario.NeutralMoves();
         ///LoadStages//////
         public Stages.FirstStage Stage1 = new Stages.FirstStage(){};
         
         public void load(){
             Mario = new Mario.NeutralMoves();
             Stage1 = new Stages.FirstStage(){};
                    
         }
         
      
      }
      My goal was, to run this to see everything works so far, so my collision detection class might be incomplete. All i wanted to see, was mario run on the X axis while I press the left and right arrow.Just to make sure I coded things right.

      Edited by: SubzeroX5 on Mar 29, 2011 4:44 PM
        • 1. Re: NullPointerException problem
          796440
          Waaayyyyy too much code.

          What is line 42 of FirstStage.java?
          • 2. Re: NullPointerException problem
            804374
            sorry if I made you read to much.
            Line 42 said
             g.drawImage(BackgroundDemo, 0, 0,null);
            Edited by: SubzeroX5 on Mar 29, 2011 4:50 PM
            • 3. Re: NullPointerException problem
              EJP
              So 'g' is null.

              ... because you called it with null, here:
              this.drawImages(null);
              So don't do that.
              • 4. Re: NullPointerException problem
                804374
                Thanks, I found an alternative and it works now. Here's the new code,
                public abstract class FirstStage extends JPanel {
                
                    public FirstStage() {
                        this.FirstStageLoad();
                
                    }
                
                    public TheFirstStage FirstStageLoad() {
                
                        return FirstStage;
                    }
                    public TheFirstStage FirstStage = new TheFirstStage();
                
                    public class TheFirstStage {
                        //Imagesfor DemoStage
                
                        public Image BackgroundDemo = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\TemplatesCOTN\\BackgroundDemoStage.png").getImage();
                        public Image DemoPlatformPic = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\TemplatesCOTN\\GrayOrEvilGround.png").getImage();
                        public Image Clouds = new ImageIcon("C:\\Documents and Settings\\ben\\My Documents\\My Pictures\\Java pics\\TemplatesCOTN\\DarkClouds.png").getImage();
                        //////Rectangles//////
                        public Platform PlatformCol = new Platform();
                        public Rectangle Char = new Rectangle();
                        ////////divider///////////
                
                        /*
                        public void Collision(){
                        
                        }*/
                        //Define the new Rectangle
                        public class Platform {
                
                            public Rectangle PlatformCol = new Rectangle();
                
                            public Rectangle getBounds() {
                                return new Rectangle(DemoPlatformPic.getHeight(null), DemoPlatformPic.getWidth(null));
                            }
                        }
                
                        ///Draw Methods////
                        public void drawImages(Graphics2D g) {
                            g.drawImage(BackgroundDemo, 0, 0, null);
                            g.drawImage(Clouds, 0, 0, null);
                            g.drawImage(DemoPlatformPic, -200, 0, null);
                        }
                    }
                }
                But, theirs another problem. Although this program works, nothing but the JFrame shows when I run it

                Edited by: SubzeroX5 on Mar 29, 2011 7:30 PM

                Edited by: SubzeroX5 on Mar 29, 2011 7:31 PM
                • 5. Re: NullPointerException problem
                  EJP
                  Thanks, I found an alternative
                  Calling a method that only returns a value and ignoring the value. What's the point of that?
                  Although this program works, nothing but the JFrame shows when I run it
                  So it doesn't work.