7 Replies - 695 Views - Last Post: 04 April 2009 - 08:41 PM Rate Topic: -----

#1 voltamars  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-March 09

Trouble with constructors and objects

Posted 03 April 2009 - 07:10 PM

Hi,

My assignment involves constructors, methods and objects to create a picture out of certain shapes. However, I'm having trouble with the overloaded constructors. There is supposed to be an object created in it that gives measurements and coordinates on a canvas.

Everything compiles, but when I create the new object in the program, I'm able to set the measurements and coordinates, yet when I choose to make the object visible, I' am given an error message, "NullPointerException:null" and it links to a part of the code that I never touched. I'm having trouble understanding exactly what is going wrong.

Here is what I have:

public Triangle(int xPos, int yPos, int height, int width)
	{   
		Triangle aTriangle = new Triangle();
	}


And when I make this object and give the parameters, when I try to make it visible on the canvas, I get an error in this part of the code which I never touched:

public void setForegroundColor(String colorString)
	{
		if(colorString.equals("red")) {
			graphic.setColor(Color.red);
		}


Thanks for the help, it is greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with constructors and objects

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Trouble with constructors and objects

Posted 03 April 2009 - 07:15 PM

int xPos, int yPos, int height, int width
In the constructor that is your param but you do nothing with them. You need to assign those values toa variable in the class.
Triangle aTriangle = new Triangle();
either call the method from the super class
(super)
or add params to that object
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Trouble with constructors and objects

Posted 03 April 2009 - 08:51 PM

// constructor of a Triangle with some params
public Triangle(int xPos, int yPos, int height, int width)
	{	// actually these params are not used

// you are here in the middle of the constructor of a Triangle

		// here you create another Triangle
		Triangle aTriangle = new Triangle();
	}  // exiting the constructor  aTriangle does not exist anymore
// but a new Triangle object has been created 


Was This Post Helpful? 0
  • +
  • -

#4 voltamars  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-March 09

Re: Trouble with constructors and objects

Posted 03 April 2009 - 08:51 PM

I add the values for the parameters when I create the object, but when I Inspect the object, everything is set to 0.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Trouble with constructors and objects

Posted 03 April 2009 - 08:56 PM

View Postvoltamars, on 3 Apr, 2009 - 07:51 PM, said:

I add the values for the parameters when I create the object, but when I Inspect the object, everything is set to 0.

Where dis you add them ? Don't see it
Was This Post Helpful? 0
  • +
  • -

#6 voltamars  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 06-March 09

Re: Trouble with constructors and objects

Posted 03 April 2009 - 09:04 PM

Im sorry, Im a little lost as to exactly what to do. Forgive me if I make any stupid errors. My specific instructions are to have a constructor that creates an object with measurements located at specific coordinates on the canvas.

I created the constructor with those parameters, and when I run the program, create the object, I assign a value to the parameters, but when I make the object visible on the canvas, nothing appears, and when I Inspect it, the parameters are set to 0.

Here is what I have:

public Triangle(int xPos, int yPos, int height, int width)
	{   
		color = "red";
		isVisible = false;
		Triangle aTriangle = new Triangle();
	}


I dont get any errors, but nothing on the canvas. Thanks again.

This post has been edited by voltamars: 03 April 2009 - 09:06 PM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Trouble with constructors and objects

Posted 03 April 2009 - 09:17 PM

View Postvoltamars, on 3 Apr, 2009 - 08:04 PM, said:

Im sorry, Im a little lost as to exactly what to do. Forgive me if I make any stupid errors. My specific instructions are to have a constructor that creates an object with measurements located at specific coordinates on the canvas.

I created the constructor with those parameters, and when I run the program, create the object, I assign a value to the parameters, but when I make the object visible on the canvas, nothing appears, and when I Inspect it, the parameters are set to 0.

Here is what I have:

public Triangle(int xPos, int yPos, int height, int width)
	{   
		color = "red";
		isVisible = false;
		Triangle aTriangle = new Triangle();
	}


I dont get any errors, but nothing on the canvas. Thanks again.

Sure you waont have anything... where do you save the x,y coordinates and the height and the width ?

you start by telling us that your constructor was doing

public Triangle(int xPos, int yPos, int height, int width)
	{   
		Triangle aTriangle = new Triangle();
	}


Now you say that is doing

public Triangle(int xPos, int yPos, int height, int width)
	{   
		color = "red";
		isVisible = false;
		Triangle aTriangle = new Triangle();
	}

May be in 10 posts will have your completly code and will be able to help you :)
Was This Post Helpful? 0
  • +
  • -

#8 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Trouble with constructors and objects

Posted 04 April 2009 - 08:41 PM

For starts what does a Triangle object need to have...looking at your params it will have:

xPos, yPos, height, and width as passed in parameters. Color will always default red and within your triangle you create a Triangle that only exists while in the constructor.

You need to do something with those parameters...store them to a variable in the Triangle.

I imagine your class looks like this:

public class Triangle {
  int xPos, yPos, height, width;

  public Triangle(int xPos, int yPos, int height, int width) {
	  // Here you need to assign the passed in params to the variables declared above.
	  this.xPos = xPos;
	  // Etc...
   }
}



I don't know what text book you are using but this is good reading for this:

Java Tutorial

also

Other Java Tutorial

This post has been edited by SpeedisaVirus: 04 April 2009 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1