Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Need help loading images  (Read 2119 times)
0 Members and 1 Guest are viewing this topic.
Offline Painguy

Senior Newbie





« Posted 2011-05-12 21:01:29 »

hey guys so im starting my first java game & I ran into a problem. What I'm trying to do do is load the sprites from the player class then run them in the main. Im gonna have 2 more classes for enemies doing the same thing. here is my code so u can see. Here's the error.

Exception in thread "main" java.lang.NullPointerException
        at Game.run(Game.java:41)
        at Game.main(Game.java:32)

Main class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
import java.awt.*;
import javax.swing.ImageIcon;

public class Game {
   public Renderer s;
   public PlayerShip player;
   private Image bg;
   private static final DisplayMode modes1[] = {
      new DisplayMode(800,600,32,0),
      new DisplayMode(800,600,24,0),
      new DisplayMode(800,600,16,0),
      new DisplayMode(640,480,32,0),
      new DisplayMode(640,480,24,0),
      new DisplayMode(640,480,16,0),

   };;
   public Ani f;
   Image sf1;
   Image sf2;
   Image sf3;
   public static void main(String argv[]) {
      DisplayMode dm = new DisplayMode(300, 240, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
      Game g = new Game();
      g.run(dm);
   }

   public void run(DisplayMode dm2){
      s = new Renderer();
      try{
         DisplayMode dm = s.find1stcompatmode(modes1);
         s.setFullScreen(dm);
         bg = new ImageIcon("C:\\Documents and Settings\\n17289\\My Documents\\JCreator LE\\MyProjects\\CompSci Game\\Sprites\\Hard Vacuum\\Misc\\Test2.bmp").getImage();
         player.loadplayer();
         movieLoop();

      }finally{
         s.restoreScreen();
      }
   }
   public void movieLoop(){
      long startTime = System.currentTimeMillis();
      long cumTime = startTime;
      while(cumTime - startTime<2000){
         long timePassed = System.currentTimeMillis() - cumTime;
         cumTime += timePassed;
         //player.f.update(timePassed);
         Graphics2D g = s.getGraphics();
         draw(g);
         g.dispose();
         s.update();
         try{
            Thread.sleep(10);
         }catch(Exception ex){}
         }
      }
   public void draw(Graphics g){
      g.drawImage(bg,0,0, null);
      //g.drawImage(f.getImage(), 540,405, null);
   }

}

Player clas
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
import java.awt.*;
import javax.swing.ImageIcon;
public class PlayerShip{
   public Ani f;
   Image sf1;
   Image sf2;
   Image sf3;

   public PlayerShip() {
   }

   public void loadplayer(){
      sf1 = new ImageIcon("C:\\Documents and Settings\\n17289\\My Documents\\JCreator LE\\MyProjects\\CompSci Game\\Sprites\\ayership\\stable fight\\f1playerstable.png").getImage();
      sf2 = new ImageIcon("C:\\Documents and Settings\\n17289\\My Documents\\JCreator LE\\MyProjects\\CompSci Game\\Sprites\\ayership\\stable fight\\f2playerstable.png").getImage();
      sf3 = new ImageIcon("C:\\Documents and Settings\\n17289\\My Documents\\JCreator LE\\MyProjects\\CompSci Game\\Sprites\\ayership\\stable fight\\f3playerstable.png").getImage();
      f = new Ani();
      f.addScene(sf1, 250);
      f.addScene(sf2, 250);
      f.addScene(sf3, 250);
   }
}

Screen class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
import java.awt.*;
import javax.swing.JFrame;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

public class Renderer{
   
   private GraphicsDevice vc;
   
   public Renderer(){
      GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
      vc = e.getDefaultScreenDevice();
   }
   
   public DisplayMode[] getCompatibleDisplayModes(){
      return vc.getDisplayModes();
   }
   
   public DisplayMode find1stcompatmode(DisplayMode modes[]){
      DisplayMode goodModes[] = vc.getDisplayModes();
      for(int x=0; x<modes.length; x++){
         for(int y =0; y<goodModes.length;y++){
            if(displayModeMatch(modes[x], goodModes[y])){
               return modes[x];
            }
         }
      }
      return null;
   }
   public DisplayMode getCurrentDisplayMode(){
      return vc.getDisplayMode();
   }
   public boolean displayModeMatch(DisplayMode m1, DisplayMode m2){  
      if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
         return false;
      }
      if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
         return false;
      }
      if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
         return false;
      }
      return true;
   }
   
   public void setFullScreen(DisplayMode dm){
      JFrame f = new JFrame();
      f.setUndecorated(true);
      f.setIgnoreRepaint(true);
      f.setResizable(false);
      vc.setFullScreenWindow(f);
      if(dm != null && vc.isDisplayChangeSupported()){
         try{
            vc.setDisplayMode(dm);
         }catch(Exception ex){}
      }
      f.createBufferStrategy(2);
   }
   
   public Graphics2D getGraphics(){
      Window w = vc.getFullScreenWindow();
      if(w != null){
         BufferStrategy s = w.getBufferStrategy();
         return(Graphics2D)s.getDrawGraphics();
      }else{
         return null;
      }
   }
   
   public void update(){
      Window w = vc.getFullScreenWindow();
      if(w != null){
         BufferStrategy s = w.getBufferStrategy();
         if(!s.contentsLost()){
            s.show();
         }
      }
   }
   public Window getFullScreenWindow(){
      return vc.getFullScreenWindow();
   }
   
   public int getWidth(){
      Window w = vc.getFullScreenWindow();
      if(w != null){
         return w.getWidth();
      }else{
         return 0;
      }
   }
   public int getHeight(){
      Window w = vc.getFullScreenWindow();
      if(w != null){
         return w.getHeight();
      }else{
         return 0;
      }
   }
   public void restoreScreen(){
      Window w = vc.getFullScreenWindow();
      if(w != null){
         w.dispose();
      }
      vc.setFullScreenWindow(null);
   }
   
   public BufferedImage createCompatibleImage(int w, int h, int t){
      Window win = vc.getFullScreenWindow();
      if(win != null){
         GraphicsConfiguration gc = win.getGraphicsConfiguration();
         return gc.createCompatibleImage(w,h,t);
      }
      return null;
   }
}

Animation class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
import java.awt.Image;
import java.util.ArrayList;

public class Ani {
   
   private ArrayList<OneScene> scenes;
   private int sceneIndex;
   private long movieTime;
   private long totalTime;
   
   public Ani(){
      scenes = new ArrayList<OneScene>();
      totalTime = 0;
      start();
   }
   
   public synchronized void addScene(Image i, long t){
      totalTime+= t;
      scenes.add(new OneScene(i, totalTime));
   }
   public synchronized void start(){
      movieTime = 0;
      sceneIndex = 0;
   }
   
   public synchronized void update(long timePassed){
      if(scenes.size()>1){
         movieTime += timePassed;
         if (movieTime >= totalTime){
            movieTime = 0;
            sceneIndex = 0;
         }
         while(movieTime >  getScene(sceneIndex).endTime){
            sceneIndex++;
         }
      }
   }
   public synchronized Image getImage(){
      if(scenes.size()==0){
         return null;
      }else{
         return getScene(sceneIndex).pic;
      }
   }
   protected OneScene getScene(int x){
      return (OneScene)scenes.get(x);
   }
}
   class OneScene{
      Image pic;
      long endTime;
      public OneScene(Image pic, long endTime){
         this.pic = pic;
         this.endTime = endTime;
      }
}

thx in advance guys. Im probably gonna be back with more questions
Offline captainsoft
« Reply #1 - Posted 2011-05-12 21:41:49 »

I dont think Java can load the Windows-specific .bmp files. Try to convert it into a gif or jpeg if loading it via ImageIcon.




Offline cylab

JGO Ninja


Medals: 52



« Reply #2 - Posted 2011-05-12 22:14:19 »

Exception in thread "main" java.lang.NullPointerException
        at Game.run(Game.java:41)
        at Game.main(Game.java:32)

You get this Exception if you try to call a method or access a property on a null object.

Just look at Game.java line 41 (the real line 41 - the posted line numbers doesn't match the error!).

I would suspect that the problem is player.loadPlayer() since you call loadPlayer() on the player variable that is uninitialized, so defaults to null...

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-05-12 23:37:57 »

I dont think Java can load the Windows-specific .bmp files. Try to convert it into a gif or jpeg if loading it via ImageIcon.
Java does support bitmap images.

Exception in thread "main" java.lang.NullPointerException
        at Game.run(Game.java:41)
        at Game.main(Game.java:32)

You get this Exception if you try to call a method or access a property on a null object.

Just look at Game.java line 41 (the real line 41 - the posted line numbers doesn't match the error!).

I would suspect that the problem is player.loadPlayer() since you call loadPlayer() on the player variable that is uninitialized, so defaults to null...
You are absolutely correct because it says the call to run(DisplayMode) is called at line 32 in main(String[]), which is line 24 in his code paste. So assuming there is an 8-line difference, line 33 is where the error is being thrown, which is "player.loadPlayer()". Since "player" has not been initialized, then the NullPointerException is being thrown.

EDIT: @OP, Aha! You've been watching Bucky's (thenewboston's) Java Gaming Youtube Tutorials haven't you? I recognized that Ani and Renderer class from somewhere: Bucky's Animation and Screen class. I still use the Animation class for my 2D animations to this day Wink

Offline Painguy

Senior Newbie





« Reply #4 - Posted 2011-05-13 02:08:06 »

OH LOL thx guys. Im such an idiot. I cant believe i didnt initialize the object lol. Yeah i got that class from thenewboston Tongue I figured I might aswell get started somewhere. thx guys I RLY appreciate it Cheesy
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-05-13 04:08:16 »

Glad to help Smiley

Offline Painguy

Senior Newbie





« Reply #6 - Posted 2011-05-14 02:13:03 »

In order to keep the number of new threads down I'll just ask my question here. I created another animation which I'm trying to load. this time i tried to not make any stupid mistakes Tongue Ill post the parts i changed. this is in the Game class only. I have everything loaded & playing except the new animation.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
   public void run(DisplayMode dm2){
      s = new Renderer();
      try{
         DisplayMode dm = s.find1stcompatmode(modes1);
         s.setFullScreen(dm);
         loadmenu();
         player.loadplayer();
         movieLoop();
      }finally{
         s.restoreScreen();
      }
   }
   public void movieLoop(){
      long startTime = System.currentTimeMillis();
      long cumTime = startTime;
      while(cumTime - startTime<2000){
         long timePassed = System.currentTimeMillis() - cumTime;
         cumTime += timePassed;
         player.f.update(timePassed);
         a.update(timePassed);
         Graphics2D g = s.getGraphics();
         draw(g);
         g.dispose();
         s.update();
         try{
            Thread.sleep(10);
         }catch(Exception ex){}
         }
      }
   public void loadmenu(){
      bg = new ImageIcon("J:\\New folder (2)\\Untitled-2.jpg").getImage();
      txt1 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner.png").getImage();
      txt2 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner2.png").getImage();
      txt3 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner3.png").getImage();
      txt4 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner4.png").getImage();
      a = new Ani();
      a.addScene(txt1, 80);
      a.addScene(txt2, 80);
      a.addScene(txt3, 80);
      a.addScene(txt4, 80);
   }
   
   public void draw(Graphics2D g){
      g.drawImage(bg,0,0, null);
      g.drawImage(player.f.getImage(), 400,450, null);
      g.drawImage(a.getImage(), 400, 150, null);
   }

thx in advance.
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-05-14 02:26:07 »

We don't need to keep the number of threads down, so this could have been fine as a new thread.

Anyway, in your movieLoop() method, your while loop keeps running until the difference between cumTime and startTime equals or exceeds 2000 milliseconds, so your animation only runs for 2 seconds. Did you mean your animation is not running at all? There doesn't seem to be any errors or bugs so everything should work as is.

Also, this line "cumTime += timePassed" could be replaced with "cumTime = System.currentTimeMillis()" since they both do the exact same thing.

Offline Painguy

Senior Newbie





« Reply #8 - Posted 2011-05-14 02:32:30 »

oh i c sry about that.
yeah the animation doesn't run at all Sad
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-05-14 02:41:51 »

It really does look fine. Try putting in System.out.printlns in the loop and other methods to see if they are actually being called.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Painguy

Senior Newbie





« Reply #10 - Posted 2011-05-14 04:12:22 »

already tried that Sad
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-05-14 17:57:15 »

What did you see? Were the methods being called?

Offline Painguy

Senior Newbie





« Reply #12 - Posted 2011-05-14 20:39:20 »

Ok so apparently my images are messed up Tongue lol. Other pictures work fine. I just don't know exactly whats wrong with it. I can see it find on my picture viewer, but...when I load the image i can't see it at all.. I can't wait till I start to get the hang of this junk. Just remade the image
Offline CyanPrime
« Reply #13 - Posted 2011-05-15 00:48:22 »

>cumTime
Just what kind of program are you making, young man?
Offline ReBirth
« Reply #14 - Posted 2011-05-15 03:29:19 »

Yay someone uses JCrestorLE like me! Cool

You want an animation of the player? but you didn't update its locations Smiley
1  
2  
3  
4  
5  
public void draw(Graphics2D g){
    g.drawImage(bg,0,0, null);
    g.drawImage(player.f.getImage(), 400,450, null);
    g.drawImage(a.getImage(), 400, 150, null);
}


and why must "cumTime" name? lol

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #15 - Posted 2011-05-15 03:44:24 »

Yay someone uses JCrestorLE like me! Cool

You want an animation of the player? but you didn't update its locations Smiley
1  
2  
3  
4  
5  
public void draw(Graphics2D g){
    g.drawImage(bg,0,0, null);
    g.drawImage(player.f.getImage(), 400,450, null);
    g.drawImage(a.getImage(), 400, 150, null);
}


and why must "cumTime" name? lol
What's JCrestorLE, do you mean JCreator? And how does his code show that he uses it?
Also, please take another look at the Ani class. He is trying to animate, not move it.

>cumTime
Just what kind of program are you making, young man?
Bahahahahah that's what I thought at first but he actually means cumulative time Wink

Offline ReBirth
« Reply #16 - Posted 2011-05-15 03:50:13 »

@r4king
typo, it must be JCreator. I know from here
1  
bg = new ImageIcon("C:\\Documents and Settings\\n17289\\My Documents\\JCreator LE\\MyProjects\\CompSci Game\\Sprites\\Hard Vacuum\\Misc\\Test2.bmp").getImage();
  Pointing

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #17 - Posted 2011-05-15 04:27:02 »

Aha, I didn't notice Roll Eyes
But I still have better eyes than you  Pointing

Offline Painguy

Senior Newbie





« Reply #18 - Posted 2011-05-15 21:21:03 »

Im making a very naught program Tongue haha it means cumulative time haha. I don't blame you tho. All my friends were laughing when they saw that haha. It turns out that my Image was using something called Indexed color & I had to change it to RGB. My animations are working fine now Smiley
Offline ReBirth
« Reply #19 - Posted 2011-05-16 02:00:26 »

Aha, I didn't notice Roll Eyes
But I still have better eyes than you  Pointing
LoL what do you mean?
Im making a very naught program Tongue haha it means cumulative time haha. I don't blame you tho. All my friends were laughing when they saw that haha. It turns out that my Image was using something called Indexed color & I had to change it to RGB. My animations are working fine now Smiley
glad to hear that Smiley

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.

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

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

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

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (56 views)
2014-10-14 00:39:48

TehJavaDev (55 views)
2014-10-14 00:35:47

TehJavaDev (46 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
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!