Nested class vars dont initialize
I have this code:
Code :
package zombies;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author
*/
class ZOMBIE extends Applet implements Runnable, MouseListener {
//var declaration
private Thread th;//thread
private Image dbImage;//image for double buffering
private Graphics dbg;//grpahics for double buffering
private int radius;//player circle radius
private int x_pos;//player x position
private int y_pos;//player y position
private int mX;//mouse click x postion
private int mY;//mouse click y position
private int Radius;//zombie circle radius
private int num5;//player melee strength
private int num6;//zombie strength
private boolean infected;//player infected boolean
//var declaration
//class declaration
class zombie{
boolean chase = false;//zombie chase boolean
int X_pos = (int)Math.random()*500;//zombie x position
int Y_pos = (int)Math.random()*500;//zombie y position
int sX_pos = X_pos;//zombie start x position
int sY_pos = Y_pos;//zombie start y position
int Zx;//zombie destination x position
int Zy;//zombie destination y position
int num1 = 0;//timer var number one
int num2 = 0;//timer var number 2
double num3;//random offset x position
double num4;//random offset y position
}
//class declaration
//object declaration
zombie z;
//object declaration
//functions
public void init(){
//initialize vars
infected = false;
num5 = 0;
num6 = 0;
radius = 10;
z.X_pos = 100;
z.Y_pos = 100;
z.Zx = z.X_pos;
z.Zy = z.Y_pos;
z.sX_pos = z.X_pos;
z.sY_pos = z.Y_pos;
x_pos = 250;
y_pos = 250;
mX = 250;
mY = 250;
Radius = 10;
//repeated vars
//repeated vars
//initialize vars
//set the stage
this.setSize(500, 500);
setBackground(Color.LIGHT_GRAY);
addMouseListener(this);
//set the stage
}
public void start(){
//start thread
th = new Thread(this);
th.start();
}
public void run(){
//running code
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
while(true){
//paint graphics
repaint();
//paint graphics
//game engine
//end game code
if(infected == true){
mX = x_pos;
mY = y_pos;
}
//end game code
//player movement code
if(y_pos < mY){
y_pos++;
}
if(y_pos > mY){
y_pos--;
}
if(x_pos < mX){
x_pos++;
}
if(x_pos > mX){
x_pos--;
}
//player movement code
//player detection (by zombie)
if(Math.abs(z.X_pos - x_pos)<50 && Math.abs(z.Y_pos - y_pos)<50){
z.chase=true;
}
if(Math.abs(z.X_pos - x_pos)>100 && Math.abs(z.Y_pos - y_pos)>100){
z.chase = false;
}
//player detection (by zombie)
//zombie chase code
if(z.chase==true){
z.Zx = x_pos;
z.Zy = y_pos;
z.sX_pos = z.X_pos;
z.sY_pos = z.Y_pos;
}
//zombie chase code
//zombie movement timer (timer number one)
if(z.num1>0){
z.num1--;
}
if(z.num1==0){
//zombie movement code
if(infected == false){
if(z.Y_pos < z.Zy){
z.Y_pos++;
if(z.chase==true){
z.Y_pos++;
}
}
if(z.Y_pos > z.Zy){
z.Y_pos--;
if(z.chase==true){
z.Y_pos--;
}
}
if(z.X_pos < z.Zx){
z.X_pos++;
if(z.chase==true){
z.X_pos++;
}
}
if(z.X_pos > z.Zx){
z.X_pos--;
if(z.chase==true){
z.X_pos--;
}
}
}
z.num1 = 2;
if(z.chase==false){
z.num1 += 2;
}
//zombie movement code
}
//zombie movement timer (timer number one)
//zombie idle movement code
if(z.chase == false){
//idle wandering code timer (timer number two)
if(z.num2>0){
z.num2--;
}
if(z.num2==0){
//generate random offset
z.num3 = Math.random() * 15 - Math.random() * 15;
z.num4 = Math.random() * 15 - Math.random() * 15;
//generate random offset
System.out.print(z.sX_pos + " ");
z.Zx = z.sX_pos + (int)z.num3;
z.Zy = z.sY_pos + (int)z.num4;
z.num2 = 100;
}
//idle wandering code timer (timer number two)
}
//zombie idle movement code
try{
Thread.sleep(20);
}catch(InterruptedException ex){
}
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
//game engine
}
}
public void paint(Graphics g){
//graphics code
g.setColor(Color.white);
if(Math.abs(z.X_pos - x_pos)<10 && Math.abs(z.Y_pos - y_pos)<10){
g.setColor(Color.pink);
num6++;
if(num5<num6 && infected == false){
g.setColor(Color.red);
infected = true;
}else if(num5>num6){
//z = null;
}
if(infected == true){
g.setColor(Color.darkGray);
}
}
if(infected == true){
g.setColor(Color.darkGray);
}
g.fillOval(x_pos - radius,y_pos - radius,radius,radius);
g.setColor(Color.darkGray);
if(Math.abs(z.X_pos - x_pos)<10 && Math.abs(z.Y_pos - y_pos)<10){
g.setColor(Color.red);
}
g.fillOval(z.X_pos - Radius,z.Y_pos - Radius,Radius,Radius);
//graphics code
}
public void update(Graphics g){
//double buffering
if(dbImage==null){
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
//double buffering
}
public void mouseClicked (MouseEvent e){
//nothing here
}
public void mousePressed(MouseEvent e) {
//event listener code
mX = e.getX();
mY = e.getY();
//event listener code
}
public void mouseReleased(MouseEvent e) {
//nothing
}
public void mouseEntered(MouseEvent e) {
//nothing
}
public void mouseExited(MouseEvent e) {
//nothing
}
}
When I try to run it in the Netbeans Apllet Viewer, I get this error message:
Start: applet not initialized.
Re: Nested class vars dont initialize
When I compile the program from a command line and run appletviewer from a command line, in addition to seeing Start: applet not initialized. at the bottom of the applet window, I get the following run-time error in the command window:
Code :
load: ZOMBIE.class is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a member of class ZOMBIE with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
at java.lang.Class.newInstance0(Class.java:366)
at java.lang.Class.newInstance(Class.java:325)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:798)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:727)
at sun.applet.AppletPanel.run(AppletPanel.java:380)
at java.lang.Thread.run(Thread.java:679)
Well, it's obvious how to fix that. So, after I fix it and compile and run it (from a command line) again, in addition to seeing Start: applet not initialized. at the bottom of the applet window, I get the following run-time error in the command window:
Code :
java.lang.NullPointerException
at ZOMBIE.init(ZOMBIE.java:55)
at sun.applet.AppletPanel.run(AppletPanel.java:436)
at java.lang.Thread.run(Thread.java:679)
That's not a huge head-scratcher for people who have made a certain kind of error a few times (or a few dozen times---maybe more---as I have), but I'll leave it up to you.
Bottom line: I don't know how Netbeans reports run-time errors, but it seems to me that you need to look at them to be able to debug, and you need to tell us what they are to give us a better chance of helping you see how to debug your program.
Cheers
Z
Re: Nested class vars dont initialize
Um.. thanks Zaphod! I'll try to remember to check the run-time errors. But I just started trying to program with Java, and I don't really understand what they mean. Could you explain a little bit more?
What did you mean when you said that you fixed it? Did you just make it public? And what exactly is the second problem? What do I need to do? Thanks in advance!
Re: Nested class vars dont initialize
Quote:
Originally Posted by
jserete
What did you mean when you said that you fixed it? Did you just make it public?
Yes. The applet class definition must be a public class. That's exactly what the error message was trying to tell us. (Did you try it? Did the first error message go away?)
Quote:
Originally Posted by
jserete
And what exactly is the second problem? What do I need to do?
If your error message was not exactly the thing that I posted, then paste it into your response so that we will all be singing from the same song book. Different versions of javac might have slightly different wording.
Anyhow...
If your error message is the same that I posted, then it is telling you to look (really look) at line 55 of ZOMBIE.java and see if you can figure out why there is a complaint about a NPE. (The Null Pointer Exception run-time error happens so frequently that after a while we get tired of writing it out every stinkin' time, so we call it an NPE.)
One common cause of an NPE is if you have declared a reference variable for some kind of object but it was never initialized to refer to an actual object.
Reference variables are automatically initialized to a "null" value unless the definition also instantiates a brand new object or assigns the variable's value to refer an object that already exists. Then, if the program tries to read or write using that reference variable (the one with the "null" value) there is no object to access, and then we hear Java programmers say: "Rats! Another NPE."
If you still can't see it, then post the exact line that the error message flags and post the exact error message that your system shows. Then we can talk about it.
I mean, it's No Big Deal to fix, but understanding exactly what is happening is really, really (really) important. There will (probably) be a Next Time that you see this, and if you "get it," this time, you will be able to jump all over when you see it again. You will thank me later.
Cheers!
Z
Re: Nested class vars dont initialize
Thanks Zaphod, the applet initializes now but the NPEs just keep on coming.
I've updated my code :
Code :
package zombies;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
*
* @author
*/
public class ZOMBIE extends Applet implements Runnable, MouseListener {
//var declaration
public Thread th;//thread
public Image dbImage;//image for double buffering
public Graphics dbg;//grpahics for double buffering
public int radius;//player circle radius
public int x_pos;//player x position
public int y_pos;//player y position
public int mX;//mouse click x postion
public int mY;//mouse click y position
public int Radius;//zombie circle radius
public int num5;//player melee strength
public int num6;//zombie strength
public boolean infected;//player infected boolean
//var declaration
//object declaration
public zombie z;
//object declaration
//class declaration
public class zombie{
public boolean chase = false;//zombie chase boolean
public int X_pos = (int)Math.random()*500;//zombie x position
public int Y_pos = (int)Math.random()*500;//zombie y position
public int sX_pos = X_pos;//zombie start x position
public int sY_pos = Y_pos;//zombie start y position
public int Zx;//zombie destination x position
public int Zy;//zombie destination y position
public int num1 = 0;//timer var number one
public int num2 = 0;//timer var number 2
public double num3;//random offset x position
public double num4;//random offset y position
}
//class declaration
//functions
public void init(){
//initialize vars
infected = false;
num5 = 0;
num6 = 0;
radius = 10;
x_pos = 250;
y_pos = 250;
mX = 250;
mY = 250;
Radius = 10;
//repeated vars
//repeated vars
//initialize vars
//set the stage
this.setSize(500, 500);
setBackground(Color.LIGHT_GRAY);
addMouseListener(this);
//set the stage
}
public void start(){
//start thread
th = new Thread(this);
th.start();
}
public void run(){
//running code
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
while(true){
//paint graphics
repaint();
//paint graphics
//game engine
//end game code
if(infected == true){
mX = x_pos;
mY = y_pos;
}
//end game code
//player movement code
if(y_pos < mY){
y_pos++;
}
if(y_pos > mY){
y_pos--;
}
if(x_pos < mX){
x_pos++;
}
if(x_pos > mX){
x_pos--;
}
//player movement code
//player detection (by zombie)
if(Math.abs(z.X_pos - x_pos)<50 && Math.abs(z.Y_pos - y_pos)<50){
z.chase=true;
}
if(Math.abs(z.X_pos - x_pos)>100 && Math.abs(z.Y_pos - y_pos)>100){
z.chase = false;
}
//player detection (by zombie)
//zombie chase code
if(z.chase==true){
z.Zx = x_pos;
z.Zy = y_pos;
z.sX_pos = z.X_pos;
z.sY_pos = z.Y_pos;
}
//zombie chase code
//zombie movement timer (timer number one)
if(z.num1>0){
z.num1--;
}
if(z.num1==0){
//zombie movement code
if(infected == false){
if(z.Y_pos < z.Zy){
z.Y_pos++;
if(z.chase==true){
z.Y_pos++;
}
}
if(z.Y_pos > z.Zy){
z.Y_pos--;
if(z.chase==true){
z.Y_pos--;
}
}
if(z.X_pos < z.Zx){
z.X_pos++;
if(z.chase==true){
z.X_pos++;
}
}
if(z.X_pos > z.Zx){
z.X_pos--;
if(z.chase==true){
z.X_pos--;
}
}
}
z.num1 = 2;
if(z.chase==false){
z.num1 += 2;
}
//zombie movement code
}
//zombie movement timer (timer number one)
//zombie idle movement code
if(z.chase == false){
//idle wandering code timer (timer number two)
if(z.num2>0){
z.num2--;
}
if(z.num2==0){
//generate random offset
z.num3 = Math.random() * 15 - Math.random() * 15;
z.num4 = Math.random() * 15 - Math.random() * 15;
//generate random offset
System.out.print(z.sX_pos + " ");
z.Zx = z.sX_pos + (int)z.num3;
z.Zy = z.sY_pos + (int)z.num4;
z.num2 = 100;
}
//idle wandering code timer (timer number two)
}
//zombie idle movement code
try{
Thread.sleep(20);
}catch(InterruptedException ex){
}
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
//game engine
}
}
public void paint(Graphics g){
//graphics code
g.setColor(Color.white);
if(Math.abs(z.X_pos - x_pos)<10 && Math.abs(z.Y_pos - y_pos)<10){
g.setColor(Color.pink);
num6++;
if(num5<num6 && infected == false){
g.setColor(Color.red);
infected = true;
}else if(num5>num6){
//z = null;
}
if(infected == true){
g.setColor(Color.darkGray);
}
}
if(infected == true){
g.setColor(Color.darkGray);
}
g.fillOval(x_pos - radius,y_pos - radius,radius,radius);
g.setColor(Color.darkGray);
if(Math.abs(z.X_pos - x_pos)<10 && Math.abs(z.Y_pos - y_pos)<10){
g.setColor(Color.red);
}
g.fillOval(z.X_pos - Radius,z.Y_pos - Radius,Radius,Radius);
//graphics code
}
public void update(Graphics g){
//double buffering
if(dbImage==null){
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
//double buffering
}
public void mouseClicked (MouseEvent e){
//nothing here
}
public void mousePressed(MouseEvent e) {
//event listener code
mX = e.getX();
mY = e.getY();
//event listener code
}
public void mouseReleased(MouseEvent e) {
//nothing
}
public void mouseEntered(MouseEvent e) {
//nothing
}
public void mouseExited(MouseEvent e) {
//nothing
}
}
and now I've got this error:
Code :
run-applet:
Exception in thread "Thread-4" java.lang.NullPointerException
at zombies.ZOMBIE.run(ZOMBIE.java:108)
at java.lang.Thread.run(Thread.java:662)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at zombies.ZOMBIE.paint(ZOMBIE.java:200)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:308)
at java.awt.Component.dispatchEventImpl(Component.java:4736)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:668)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:627)
at java.awt.EventQueue$2.run(EventQueue.java:625)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:641)
at java.awt.EventQueue$3.run(EventQueue.java:639)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:638)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at zombies.ZOMBIE.paint(ZOMBIE.java:200)
at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
at sun.awt.RepaintArea.paint(RepaintArea.java:224)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:308)
at java.awt.Component.dispatchEventImpl(Component.java:4736)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:668)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:627)
at java.awt.EventQueue$2.run(EventQueue.java:625)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:641)
at java.awt.EventQueue$3.run(EventQueue.java:639)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:638)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 12 seconds)
Please tell me if I need to put in/take out anything.
Thanks in advance!
Re: Nested class vars dont initialize
Quote:
Originally Posted by
jserete
...error
Code :
un-applet:
Exception in thread "Thread-4" java.lang.NullPointerException
at zombies.ZOMBIE.run(ZOMBIE.java:108)
at java.lang.Thread.run(Thread.java:662)
OK. I'll try again. Your NPE was generated by something on line 108 of your source file.
Now: Find line 108 in that file. Write it down on pencil and paper. Really. Pencil and paper.
Look at what you wrote.
Then...
Look at my previous response and answer the following questions:
What object appears in the expression of that line that you just wrote on your paper?
Where is that object declared and where is it initialized? Look back toward the beginning of your file.
Is it just possible that you have declared a reference variable that has the name of the object in the expression in the line that you wrote down but you didn't actually invoke the constructor with a "new..." statement? That is what I suggested is the cause of quite a few NPE problems. At least that's my experience.
I mean, I hate to repeat myself but
Quote:
Originally Posted by Zaphod_b
One common cause of an NPE is if you have declared a reference variable for some kind of object but it was never initialized to refer to an actual object.
Reference variables are automatically initialized to a "null" value unless the definition also instantiates a brand new object or...
If you still don't "get it" then post your line 108 and post the answers to the questions that I asked.
(Note that when I pasted your code into my editor and compiled and executed the applet I got an NPE message on line 104, so I'm not sure why yours said 108 unless a few lines got lost in translation. It's hard to be precise when the line counts don't agree.)
Cheers!
Z