• 0

[Java] Confuzion


Question

import java.awt.*;
import java.applet.*;


public class main extends Applet{
	private alt testAlt;
	private static final Point altPoint = new Point(20,30);
	public void init(){
  alt testAlt = new alt(altPoint);
	}
	public void paint(Graphics graphics){
  testAlt.draw(g);
	}
}

import java.awt.*;
public class alt {
	private Point testPoint;
	public alt(Point aPoint){
  Point testPoint = new Point(aPoint);
	}
	public void draw(Graphics g){
  g.drawRect(testPoint.x,testPoint.y,20,20);
	}
}

The following error is produced

java.lang.Error: Unresolved compilation problem:

g cannot be resolved

at main.paint(main.java:23)

at sun.awt.RepaintArea.paint(Unknown Source)

at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)

I think this means that for some reason the classes cannot see the methods inside of each other, however when i move the constructor into the main paint method everything works fine, however my assignment requests that i put the constructor in this init(); method.

If anyone knows how to accomplish this please help me!

Thanks

-Asmo

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

I do not quite understand what you mean, are you saying that i should declare an instance variable at the start of the first class such as private Graphics g;

because if so i tried this and it did not work, further clarification is requested please

-Asmo

Link to comment
Share on other sites

  • 0

You are using a variable that doesn't exists!!!

As if you would do:

int i = 5

int j = 7;

int z = a + b; // a and b are not defined!!

You're probably trying to do:

testAlt.draw(graphics);

Link to comment
Share on other sites

  • 0

Ok, this i understand thank you Mouton, I have since corrected my code so that i am now calling testAlt.draw(graphics); from within the main paint method, However it now says:

java.lang.NullPointerException

at main.paint(main.java:12)

at sun.awt.RepaintArea.paint(Unknown Source)

at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)

This leads me to beleive that for some reason the main Class (main.class) cannot see the draw(graphics) method in the alt.class. This is contrary to what i have been taught in my lectures because i was under the impression that as long as a method is public it can be seen by any class that calls it. Maybe there is some conflict between the paint(Graphics graphics) method in the main.class and the draw(Graphics graphics) in the alt.class?

Here is my updated Code:

import java.awt.*;
import java.applet.*;


public class main extends Applet{
private alt testAlt;
private static final Point altPoint = new Point(20,30);
public void init(){
 alt testAlt = new alt(altPoint);
}
public void paint(Graphics graphics){
 testAlt.draw(graphics);
}
}



import java.awt.*;
public class alt {
private Point testPoint;
public alt(Point aPoint){
 Point testPoint = new Point(aPoint);
}
public void draw(Graphics graphics){
 graphics.drawRect(testPoint.x,testPoint.y,20,20);
}
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.