4 Attachment(s)
Graphics class NullPointerException Initialize Graphics Class??
I am creating a program for my final in an AP computer science class. My idea was simple: create a program that drops "meteors" and you have to dodge them with your mouse. I wrote the whole program, and then ran it and got a NullPointerException, because I did not use the Graphics class right or something. I am a good programmer, but I am not familiar at all with graphics of any kind, and the Graphics class looked easy to use, but I am not very good at it. I am trying to stay away from the JPanel/ javax.swing because I have absolutely no idea how to use them. Please help me.
(Note: Code is commented and pretty self explanatory)
Re: Graphics class NullPointerException Initialize Graphics Class??
Quote:
got a NullPointerException
Can you post the full text of the error message and the code that goes with it.
I prefer NOT to down load .doc files.
Post the code so it can be easily read.
Re: Graphics class NullPointerException Initialize Graphics Class??
Very few (if any) will download attachments - I recommend you post the code into the forum itself (be sure to use the code tags). For what its worth, I'm betting you are doing the painting incorrect based upon the following comment:
Quote:
I am trying to stay away from the JPanel/ javax.swing because I have absolutely no idea how to use them
JPanel's are made for painting - and simply override the paintComponent and use the Graphics class passed might solve your problems.
See http://download.oracle.com/javase/tu...wing/painting/
Re: Graphics class NullPointerException Initialize Graphics Class??
Code :
//Meteor.java
import java.applet.*;
import java.awt.*;
public class Meteor {
//Problem
Graphics g;
//Randomize x coordinate of meteor
private int randomNum = (int)(Math.random() * 250);
private int mRadius;
private int y = 0;
private final int X = randomNum;
public Meteor(int radius, Graphics g){
mRadius = radius;
g.setColor(Color.white);
g.fillOval(randomNum, y, 2 * radius, 2 * radius);
}
public void fall(Graphics g){
//draw over old meteor
g.setColor(Color.black);
g.fillOval(randomNum, y, 2 * mRadius, 2 * mRadius);
//draw new meteor
g.setColor(Color.white);
y+=5;
g.fillOval(randomNum, y, 2 * mRadius, 2 * mRadius);
}
//Getters
public int getX() {
return X;
}
public int getY() {
return y;
}
//Test for if meteor is out of screen
public boolean isOutOfScreen(){
//250 is a random number I chose- its the applet windows length
if(this.getY() > 250){
return true;
}
else{
return false;
}
}
}
Code :
//Player.java
import java.awt.*;
import java.applet.Applet;
import java.awt.Event;
public class Player {
//Problem
Graphics g;
private int mouseXPosition, mouseYPosition;
public Player(Graphics g){
//get your mouse position and draw a red circle around it
mouseXPosition = getMousePositionX();
mouseYPosition = getMousePositionY();
g.setColor(Color.red);
g.fillOval(mouseXPosition,mouseYPosition, 20, 20);
}
public int getMousePositionX() {
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
return x;
}
public int getMousePositionY() {
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int y = (int) b.getY();
return y;
}
//updates where the circle around your mouse is
public boolean mouseMove(Event e, int x, int y, Graphics g){
if (getMousePositionX() != mouseXPosition || getMousePositionY() != mouseYPosition){
g.setColor(Color.black);
g.fillOval(mouseXPosition,mouseYPosition, 20, 20);
mouseXPosition = getMousePositionX();
mouseYPosition = getMousePositionY();
g.setColor(Color.red);
g.fillOval(mouseXPosition,mouseYPosition, 20, 20);
return true;
}
else{
return false;
}
}
}
Code :
//World.java
import java.applet.*;
import java.awt.*;
public class World {
Graphics g;
Meteor meteor = new Meteor(10, g);
Player player = new Player(g);
boolean isCollide;
private int score = 0;
//////////////////////////////////
//Test for Collision//////////////
/////////////////////////////////
public boolean collision(){
int pX = player.getMousePositionX();
int pY = player.getMousePositionY();
int mX = meteor.getX();
int mY = meteor.getY();
if(pX > mX - 5 && pX < mX + 5){
if(pY > mY - 5 && pY < mY + 5){
isCollide = true;
}
}
else{
isCollide = false;
}
return isCollide;
}
////////////////////////
//Increment score by 10
////////////////////////
public void scoreAdd(){
if (!(collision())){
score += 10;
}
else{
score = score;
}
}
///////////////////////
public int getScore(){
return score;
}
}
Code :
//MeteorDodger.java
//Driver Class
import java.awt.*;
import java.applet.*;
import java.util.ArrayList;
public class MeteorDodger extends Applet{
Graphics g;
static Event e;
public void init(){
g.setBackground(Color.black);
}
public void paint(Graphics g){
Player player = new Player(g);
World world = null;
g.drawString("Score: " + world.getScore(), 0, 500);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Graphics g;
//attempt to innitialize???
g = g.getGraphics();
Player player = new Player(g);
World world = null;
ArrayList<Meteor> listMeteors = new ArrayList<Meteor>();
while (!(world.collision())){
listMeteors.add(new Meteor(10,g));
for(int i = 0; i < listMeteors.size(); i++){
listMeteors.get(i).fall(g);
if((listMeteors.get(i).isOutOfScreen())){
listMeteors.remove(i);
}
}
world.scoreAdd();
//Disregard. not completed yet: test for is mouse move
if((player.mouseMove(e, player.getMousePositionX(), player.getMousePositionY(), g))){
player.mouseMove(e, player.getMousePositionX(), player.getMousePositionY(), g);
}
}
}
}
Re: Graphics class NullPointerException Initialize Graphics Class??
Where is the text of the error message?
Is you program supposed to be an Applet that runs in a browser or does it run from a commmand line? I see a main method and a class that extends the Applet class.
Re: Graphics class NullPointerException Initialize Graphics Class??
And to add to Norm's questions, I also see the following:
Code :
Graphics g;
Meteor meteor = new Meteor(10, g);
Player player = new Player(g);
Object variables that are not initialized are by default null...g is never initialized. Override the paintComponent method of a JPanel - its much easier and the proper way to draw. Here is an example:
Code :
public class MyPanel extends JPanel{
@Override
public void paintComponent(Graphics g){
//do your drawing here.
}
}
Your above code will have to be refactored a bit, but not too much.
Re: Graphics class NullPointerException Initialize Graphics Class??
thank you so much copeg. ill add that. will I need to take all the drawOval calls out of the various constructors and put them in that override or can they remain there, just slightly altered? if the latter is true, will I have to incorporate some sort of JPanel code to the fillOval? what will be the new call if so?
Re: Graphics class NullPointerException Initialize Graphics Class??
Quote:
Originally Posted by
bglueck
thank you so much copeg. ill add that. will I need to take all the drawOval calls out of the various constructors and put them in that override or can they remain there, just slightly altered? if the latter is true, will I have to incorporate some sort of JPanel code to the fillOval? what will be the new call if so?
My advice would be to think "object oriented programming". Each object has its purpose, and to separate each object based upon that purpose. For example, you have defined classes (World, Meteor, etc..) and could then have classes which define how these are drawn (JPanel(s), etc..). As an pseudo-example
Code :
public class MyPanel extends JPanel{
private Meteor meteor;
private World world;
public MyPanel(Meteor m, World w){
meteor = m;
world = w;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawOval(..get meteor values....);
....etc....
}
}
You could also place draw methods in your classes, which you can pass a Graphics object to and can be called from the paintComponent...many ways to go about it.