Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

I can't figure out why I'm getting a Null Pointer Exception

923889Mar 14 2012 — edited Mar 14 2012
I'm writing a program that calls Bingo numbers. I got that part of the program to work but when I started adding Swing I kept getting a Null Pointer Exception and I don't know how to fix it. The Exception happens on line 15 of class Panel (g = image.getGraphics();). Here is the code for my classes. I'm still not finished with the program and I can't finish it until I know that this issue is resolved.

package Graphics;

import java.awt.Graphics;

import javax.swing.JFrame;

public class DrawFrame extends JFrame{
public Panel panel;
public DrawFrame(int x, int y, String s) {
super(s);
this.setBounds(0, 0, x, y);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(getSize());
panel = this.getPanel();
this.getContentPane().add(panel);
panel.init();
this.setVisible(true);
}
public Graphics getGraphicsEnvironment(){
return panel.getGraphicsEnvironment();
}
Panel getPanel(){
return new Panel();
}
}

package Graphics;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

public class Panel extends JPanel{
Graphics g;
Image image;

public void init() {
image = this.createImage(this.getWidth(), this.getHeight());
g = image.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
Graphics getGraphicsEnvironment() {
return g;
}
public void paint(Graphics graph) {
if (graph == null)
return;

if (image == null) {
return;
}
graph.drawImage(image, 0, 0, this);
}
}

package Graphics;

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

public class Keys extends KeyAdapter{
public int keyPressed; //creates a variable keyPressed that stores an integer

public void keyPressed(KeyEvent e) { //creates a KeyEvent from a KeyListner
keyPressed = e.getKeyCode(); //gets the key from the keyboard
}
}

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

import Graphics.*;

public class Bingo {
static Ball balls[][] = new Ball[5][15]; //creates a 2D 5 by 15 array
public static void main(String[] args) {
DrawFrame frame = new DrawFrame(1500, 500, "Welcome to the automated Bingo Caller."); //creates instance of DrawFrame that is 1000 pixels wide and 500 pixels high
Graphics g = frame.getGraphicsEnvironment(); //calls the getGraphicsEnvironment method in the DrawFrame class
Keys key = new Keys(); //creates instance of the Key class
frame.addKeyListener(key); //adds a KeyListener called Key
for (int x = 0; x < 5; x++) { //fills rows
for (int y = 0; y < 15; y++) { //fills columns
balls[x][y] = new Ball(x, y+1); //fills array
}
}
frame.pack(); //adjusts the size of the frame so everything fits
g.setColor(Color.black); //sets the font color to black
g.setFont(new Font("MonoSpace", Font.PLAIN, 20)); //creates new font
for(int y=0;y<balls.length;y++){ //draws all possible balls
g.drawString(balls[y][0].s, 0, y*100); //draws numbers
for(int x=0;x<balls[y].length;x++){ //draws all possible balls
g.drawString(balls[y][x].toString(), (x+1)*100, y*100); //draws letters
}
}
do {
frame.repaint(); //repaints the balls when one is called
int x, y; //sets variables x and y as integers
boolean exit; //sets a boolean to the exit variable
do {
exit = false; //exit is set to false
x = (int)(Math.random() * 5); //picks a random number between 0 and 4 and stores it as x
y = (int)(Math.random() * 15); //picks a random number between 0 and 14 stores it as y
if (!balls[x][y].called) { //checks to see if a value is called
exit = true; //changes exit to true if it wasn't called
balls[x][y].called = true; //sets called in the Ball class to true if it wasn't called
System.out.println(balls[x][y]); //prints value
}
} while (!exit); //if exit is false, returns to top of loop
int count = 0; //sets a count for the number of balls called
for(int z=0;z<balls.length;z++){ //looks at balls
g.setColor(Color.black); //displays in black
g.drawString(balls[z][0].s, 0, z*100); //draws balls as a string
for(int a=0;a<balls[z].length;a++){ //looks at all balls
if (balls[z][a].called){ //if a ball is called
g.setColor(Color.red); //change color to red
count++; //increments count
} else {
g.setColor(Color.black); //if it isn't called stay black
}
g.drawString(balls[z][a].toString(), (a+1)*100, y*100); //draws balls as string
}
}
do {
if (key.keyPressed == KeyEvent.VK_R||count==5*15) { //if R is pressed or count = 5*15
count=5*15; //changes count to 5*15
for(int z=0;z<balls.length;z++){ //recreates rows
g.setColor(Color.black); //sets color to black
g.drawString(balls[z][0].s, 0, z*100); //redraws rows
for(int a=0;a<balls[z].length;a++){ //recreates columns
balls[z][a] = new Ball(z, a+1); //fills array
g.drawString(balls[z][a].toString(), (a+1)*100, z*100); //redraws columns
}
}

}
} while (key.keyPressed!=KeyEvent.VK_ENTER || count == 5 * 15); //determines if the key was pressed or counter is 5*15s
} while (key.keyPressed == KeyEvent.VK_ENTER);
}
}


public class Ball {
String s; //initiates s that can store data type String
int i; //initiates i that can store data as type integer
boolean called = false; //initiates called as a boolean value and sets it to false
public Ball(int x, int y) {
i = (x * 15) + y; //stores number as i to be passed to be printed
switch (x) { //based on x value chooses letter
case 0:
s = "B";
break;
case 1:
s = "I";
break;
case 2:
s = "N";
break;
case 3:
s = "G";
break;
case 4:
s = "O";
}
}
public String toString() { //overrides toString method, converts answer to String
return s + " " + i; //returns to class bingo s and i
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 11 2012
Added on Mar 14 2012
2 comments
275 views