Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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  
  [Solved] Can't fix this error  (Read 318 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-04-15 15:07:18 »

Hello everyone,
this my real first OOP game, i used to do all my programs in one class, i followed some tutorials and then tried to remake a pong game that i already made but everything was in one class, but i don't want to do that anymore, things becomes really complicated when i want to fix or re-do something with it, i always get lost in the code,
so,
 here is all the codes for the new pong and the error is :
Quote
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at Screen.paintComponent(Screen.java:28)


Frame.Java

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  
import java.awt.Dimension;

import javax.swing.JFrame;


public class Frame extends JFrame  {

   public static Dimension size = new Dimension(800,600);
   public static String title ="Pong2.0";
   public Frame(){
     
      this.setSize(size);
      this.setTitle(title);
      this.setLocationRelativeTo(null);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
      init();
     
   }
   
   public void init(){
     
      Screen screen = new Screen();
     
      this.add(screen);
     
     
     
      this.setVisible(true);
     
   }
   
   
   
   
   public static void main(String args[]){
      Frame frame = new Frame();
   }
   
}


Screen.java
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  
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;


public class Screen extends JPanel implements Runnable {
   public static Thread thread = new Thread();
   
   public static Ball ball;

   
   public Screen(){
      thread.start();
   }

   public void define(){
     
      ball = new Ball(0, 0, 50, 50);
   }
   
   public void paintComponent(Graphics g){
     
     
      g.setColor(new Color(0,0,0));
      g.fillRect(0, 0, Frame.size.width, Frame.size.height);
     
      g.setColor(new Color(0,50,0));
      ball.draw(g);
     
   }
   
   
   @Override
   public void run() {
      while(true){
         repaint();
         
         
         try {
            Thread.sleep(1);
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
           e.printStackTrace();
         }
      }
   }
   
}


Ball.java
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
import java.awt.*;


public class Ball extends Rectangle {
   
   
   public Ball(int x,int y,int width,int height){
      this.setBounds(x, y, width, height);
     
     
   }

   public void draw (Graphics g){
     
      g.drawRect( x, y, width,height);
     
     
   }
}


thank you very much for your help

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline princec

JGO Kernel


Medals: 339
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2013-04-15 15:14:48 »

That's some crazy OO style you have there.

You are constructing Screen, which starts the thread immediately. This will then reference frame, which has likely not yet actually been assigned.

Cas Smiley

Offline StrideColossus
« Reply #2 - Posted 2013-04-15 15:16:27 »

The Ball class member in your Screen class is never created which causes the NPE I think.  You have a define() method to create it but it never gets called as far as I can tell.

EDIT: Although I would have then expected the error to report line 29 in that case, if cas in the previous post is correct it would be line 27, odd Clueless
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline alaslipknot
« Reply #3 - Posted 2013-04-15 15:23:08 »

That's some crazy OO style you have there.

You are constructing Screen, which starts the thread immediately. This will then reference frame, which has likely not yet actually been assigned.

Cas Smiley
+1 Roll Eyes
as i said it's my first time  Undecided
advice, articles, suggestions are very welcome  Grin

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline alaslipknot
« Reply #4 - Posted 2013-04-15 15:23:54 »

The Ball class member in your Screen class is never created which causes the NPE I think.  You have a define() method to create it but it never gets called as far as I can tell.


i don't understand  Huh

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline StrideColossus
« Reply #5 - Posted 2013-04-15 15:30:26 »

i don't understand  Huh

I've highlighted the offending lines, assuming I correctly identified the problem (I didn't try to compile/run the code).

public class Screen extends JPanel implements Runnable {
   public static Thread thread = new Thread();
  
  public static Ball ball;     <-- this is the class member that never gets created
  
   public Screen(){
      thread.start();
   }


This method doesn't seem to get called anywhere, so 'ball' is null.  
     public void define(){
      ball = new Ball(0, 0, 50, 50);
   }
 
   public void paintComponent(Graphics g){
      
      
      g.setColor(new Color(0,0,0));
      g.fillRect(0, 0, Frame.size.width, Frame.size.height);
      
      g.setColor(new Color(0,50,0));
     ball.draw(g); <-- which causes the null pointer exception (NPE) here (I think)
      
   }
  
   @Override
   public void run() {
      while(true){
         repaint();
        
        
         try {
            Thread.sleep(1);
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
   }
}

What are you using to develop the code?  Do you have a debugger so you can step through, that's the best way of tracking down bugs?

Hope this helps.

- stride

EDIT: Why can't I make stuff bold within a code block ffs Wink
Offline alaslipknot
« Reply #6 - Posted 2013-04-15 15:37:23 »

Quote

This method doesn't seem to get called anywhere, so 'ball' is null.  
     public void define(){
      ball = new Ball(0, 0, 50, 50);
   }
 

Biiiiiiig thanx for you ^_^ !!!
and even bigger oops for me  Tongue

thank you very much

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
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.

Riven (5 views)
2014-07-23 21:16:32

Riven (6 views)
2014-07-23 21:07:15

Riven (6 views)
2014-07-23 20:56:16

ctomni231 (40 views)
2014-07-18 06:55:21

Zero Volt (36 views)
2014-07-17 23:47:54

danieldean (30 views)
2014-07-17 23:41:23

MustardPeter (32 views)
2014-07-16 23:30:00

Cero (47 views)
2014-07-16 00:42:17

Riven (48 views)
2014-07-14 18:02:53

OpenGLShaders (38 views)
2014-07-14 16:23:47
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!