Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (515)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  NullPointer - TextureRegions  (Read 258 times)
0 Members and 1 Guest are viewing this topic.
Offline CTucker1327
« Posted 2013-12-13 13:04:51 »

So, here we have a 96x64 image, composed of 6 32x32 sprites.



Alright, so I have this loading and drawing to the screen (just to make sure it's not a problem with loading the texture)


So, here's the line of code throwing the null-pointer

1  
      regions[0][0] = new TextureRegion(playerSheet, 0, 0, 32, 32); 


This, from what I understand is supposed to get the first 32x32 pixels starting from 0, 0 in the image, and save it to a TextureRegion.

1  
   private TextureRegion[][] regions;


However, It's throwing a null and I'm not sure why.

1  
2  
3  
4  
5  
6  
7  
8  
Exception in thread "LWJGL Application" java.lang.NullPointerException
   at com.chris.platformer.entities.Player.loadAssets(Player.java:64)
   at com.chris.platformer.entities.Player.<init>(Player.java:33)
   at com.chris.platformer.screens.GameScreen.show(GameScreen.java:25)
   at com.badlogic.gdx.Game.setScreen(Game.java:62)
   at com.chris.platformer.Game.create(Game.java:10)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)


Here's my loadAssets() method

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
   private void loadAssets()
   {  
      regions[0][0] = new TextureRegion(playerSheet, 0, 0, 32, 32);
      regions[1][0] = new TextureRegion(playerSheet, 32, 0, 32, 32);
      regions[2][0] = new TextureRegion(playerSheet, 64, 0, 32, 32);
      regions[3][0] = new TextureRegion(playerSheet, 0, 32, 32, 32);  
      regions[4][0] = new TextureRegion(playerSheet, 32, 32, 32, 32);
      regions[5][0] = new TextureRegion(playerSheet, 64, 32, 32, 32);
     
      walkLeft = new Animation(0.40f, regions[0][0], regions[1][0], regions[2][0]);
      walkRight = new Animation(0.40f, regions[3][0], regions[4][0], regions[5][0]);
     
      playerSprite = new Texture("graphics/sprites/player/Player.png");
   }



I believe that should get all 6 of the images, however it can't even get the first one.


Offline Phased
« Reply #1 - Posted 2013-12-13 13:15:56 »

You have 2 possible variables that could be causing the problem

first, either
1  
playerSheet


or

1  
regions[][]


may be null.

Check to see if your playerSheet is null, as I don't see that anywhere in your snippets you posted

you could use this

1  
2  
3  
if(playerSheet == null){
System.out.println("playerSheet is null");
}


for the array of TextureRegions

make sure you initialize the array

1  
2  
3  
4  
5  
private TextureRegion[][] regions;

public Player(){
regions = new TextureRegion[size][size];
}


edit: renamed the constructor so people don't have to read the non code snippets.
Offline StrideColossus
« Reply #2 - Posted 2013-12-13 13:28:46 »

The exception is not happening inside the TextureRegion constructor so it must be the regions array that is null.
Can you show us how you're initialising that array?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Phased
« Reply #3 - Posted 2013-12-13 13:33:13 »

if you re read it, you will know I never said TextureRegion constructor, but the TextureRegion region[][] is null, I showed him how to initialize the array, because either region[][] or playerSheet is null as they are the only variables that can produce a null pointer, like I said in my post that you only read the code snippets of.

To remove your confusion, I will edit the code and use his class name that he had the exception in.
Offline CTucker1327
« Reply #4 - Posted 2013-12-13 13:44:17 »

Phased hit the nail on the head, I happen to forget to initialize arrays frequently and it never registers to me when I recieve the error while trying to set the values
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (30 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (39 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (25 views)
2014-10-26 10:14:04

theagentd (31 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!