Slick Forums

Discuss the Slick 2D Library
It is currently Sun Sep 15, 2019 6:55 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Feb 15, 2017 6:32 am 
Offline

Joined: Wed Feb 15, 2017 6:22 am
Posts: 1
Hello, I'm new to Java. I try to create an external Card class and put it in my basic GameClass. But I got an error.
Also got a messege on "private Image cardImage = new Image("img/card.png");" in the Card class "Unhandled exception type Slickexception".

Code:
package tests;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import tests.Card;



public class GameClass extends BasicGame {
   
   
   private Image card = null;
   private int cardX = 300;
   private int cardY = 350;
   private Card card2;
   

   
   
   
   

   public GameClass(String title) {
      super(title);
      
   }

   public static void main(String[] args) throws SlickException {
      
      
      
      
      AppGameContainer app = new AppGameContainer(new GameClass("GAME"));
      app.setDisplayMode(800, 600, false);
      app.start();
      
      

   }

   @Override
   public void render(GameContainer gc, Graphics g) throws SlickException {
      
      card.draw(cardX, cardY, card.getWidth()/2, card.getHeight()/2);
      
      card2.draw();
   
      
   }

   @Override
   public void init(GameContainer gc) throws SlickException {
      
      card = new Image("img/card.png");
      
      
      
      
   }

   @Override
   public void update(GameContainer gc, int delta) throws SlickException {
      
   
      
      
   }

}


Code:
package tests;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;


public class Card extends Image {
   
   
   private Image cardImage = new Image("img/card.png");
   
   public Card(Image card){
      
      cardImage = card;
      Card myCard = new Card(cardImage);
      
   }
   
   
   
   

   public static void main(String[] args) throws SlickException {
      
            
      
      System.out.println("CARD CREATED");
      

   }

}


Error

java.lang.NullPointerException
at tests.GameClass.render(GameClass.java:50)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at tests.GameClass.main(GameClass.java:39)
Wed Feb 15 16:17:23 VLAT 2017 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:691)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at tests.GameClass.main(GameClass.java:39)


Top
 Profile  
 
PostPosted: Sun Mar 05, 2017 7:51 am 
Offline
User avatar

Joined: Sat Jun 25, 2016 3:58 am
Posts: 3
You never initialized the game's card2 variable, so when the game tries to draw it in render(), it finds that the name "card2" points to nothing, hence the NullPointerException. Also, it looks like every invocation of Card's constructor creates an additional Card, which will get you stuck in an infinite loop - to fix this, just remove
Code:
Card myCard = new Card(cardImage);
from Card's constructor. As for the SlickException, Image's constructor needs the option to throw a SlickException in order to be run, and it can't do that when a new Image is created directly inside a class like that. Just write
Code:
private Image cardImage;
instead; you're overriding that value in Card's constructor anyway.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 24 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group