Screen not rendering after separating into multiple classes

I tried to separate the main class (the only class) into input and screen, as well as the remaining main class. Before, I could see a colored window with a rectangle and title but now it’s just blank, even though it doesn’t say I have errors. Here are the 3 classes:

Game.java

package com.izipizi.net;

import java.awt.Color;

import javax.swing.JFrame;

public class Game extends JFrame {

private int x=200;
private int y=150;

public Game(){
	addKeyListener(new InputKey());
}

public static void main(String[] args) {
	new Screen();
	new Game();
}

public void setX(int x){
	this.x=x;
}

public int getX(){
	return this.x;
}

public void setY(int y){
	this.y=y;
}

public int getY(){
	return this.y;
}

}

//////////////////////////////////////////////////////////

InputKey.java

package com.izipizi.net;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class InputKey extends KeyAdapter {

int speed=3;
Game game;

public void keyPressed(KeyEvent e){
	
	switch(e.getKeyCode()){
	case KeyEvent.VK_UP:
		game.setY(game.getY()-speed);
		break;
	case KeyEvent.VK_DOWN:
		game.setY(game.getY()+speed);
		break;
	case KeyEvent.VK_LEFT:
		game.setX(game.getX()-speed);
		break;
	case KeyEvent.VK_RIGHT:
		game.setX(game.getX()+speed);
		break;
		
	}
	
}

}

////////////////////////////////////////////

Screen.java

package com.izipizi.net;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;

public class Screen extends JFrame {

int WIDTH=480;
int HEIGHT=360;

private Graphics dbg;
private Image dbi;
Font font=new Font(Font.SERIF, Font.BOLD,30);

Game game;

public Screen(){
	setBackground(Color.cyan);
	setSize(WIDTH,HEIGHT);
	setTitle("Simple Game");
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	setResizable(false);
	setLocationRelativeTo(null);
	setVisible(true);
}

public void paint (Graphics g){
	dbi=createImage(WIDTH,HEIGHT);
	dbg=dbi.getGraphics();
	paintComponent(dbg);
	g.drawImage(dbi, 0, 0, this);
}

public void paintComponent(Graphics g){
	g.setFont(font);
	g.setColor(Color.red);
	g.drawString("Epic Square of Epicness", 70, 70);
	g.fillRect(game.getX(), game.getY(), 18, 18);
	
	repaint();
}

}

/////////////////////////////////////////

The solution is probably something simple but i’m a newbie so please explain me.
By the way, when the screen appears i get this message in console:

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Those last two lines in your post seem to be apart of a stacktrace. Post everything that is output to the console please. I see a few other issues but I’m feeling lazy and would just like to see the stacktrace because I’m interested.

Oh, I didn’t see I could scroll up for more. Here’s the full one:

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at com.izipizi.net.Screen.paintComponent(Screen.java:42)
at com.izipizi.net.Screen.paint(Screen.java:34)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Please tell me how I can fix it and other suggestions. They would help alot. Thanks in advance!

It looks like your Game variable is null, you need to declare it. Probably passing it with the Screen constructor.

So:

public Screen(){

}

Becomes:

public Screen(Game game){
    this.game = game;
}

Then in your game class change

new Screen();
new Game();

to:


new Game();
new Screen(this);

If you look at your stack trace you’ll see the the first line says: NullPointerException. The line below says Screen.java: 42, this tells you in what class and on what line the error came from.

Thanks! I also moved ‘new Screen(this);’ to the Game constructor. Now, everything is displaying accordingly but the square doesn’t seem to move. I checked with a System.out.println and it doesn’t seem to read the key presses.

The Game variable in your InputKey class is null, you aren’t declaring it. You need to do the same as you did in the Screen class and declare your Game variable.

Try calling setFocusable(true) on your Screen JFrame, as well as requesting focus on the frame.
I’m a bit confused as to why you’ve got two classes extending JFrame, you only show one so you should only use that. I’d suggest you have a main class that owns an instance of the Game and Screen classes, so something like this:


public class Game(){
public Screen screen;
public InputKey keymanager;
public Game game;

public Game(){
    screen = new Screen(this);
    keymanager = new InputKey(this);
}

public static void main(String[] args){
    game = new Game();
}
}

I’d change the InputKey constructor to take a Game object as an argument, as to avoid a NullPointerException.

I can’t seem to be able to do it, I’ll stick to a single class :’( , but I’ll watch more tutorials, maybe I’ll stumble upon this. Do you guys have suggestions of tutorials to learn from, which are newbie friendly?

Did you try implementing the suggestions we gave?

You will probably want to figure out how to implement the multiple classes. Once your program gets large it will be very confusing to have everything in one gigantic file. Just some newbie experience that I also went through :slight_smile:

:frowning: I feel really bad for doing this, however, my PM is broken and I can’t complete the verification to send you a message lol. Could you PM me I have a few questions to ask you?