inheritance and constructors

Error Message Null Pointer Exception Error

Page 1 of 1

8 Replies - 1028 Views - Last Post: 27 August 2009 - 12:09 AM Rate Topic: -----

#1 farukh36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-March 09

inheritance and constructors

Post icon  Posted 25 August 2009 - 11:52 PM

Hi I am currently having problems with inheritance and constructors. I am getting a Null Pointer Exception Error.

I am attaching my coding.

Point.java first


public class Point{
	int x;
	int y;
	
	public Point(int k, int j)
	{
		x=k;
		y=j;
		System.out.println("Constructing a Point ");
	
	}
	
}



Next GeometricShape.java


public class GeometricShape
{
	
	Point center;

	
	public  GeometricShape(int w, int v)
	{
		System.out.println("Constructing a GeometricShape");
	//point p = new Point();
		w = center.x;
		v = center.y;
	}
	public void displayCenter()
	{
		System.out.println("X and Y coordinates of center are" + center.x + center.y);
		
	}
}




Next Rectangle.java

public class Rectangle extends GeometricShape
{
	int width;
	int height;
	public int a;
	public int b;
	public int c;
	public int d;
	
	
	
	public Rectangle(int w, int v) {
		super(w, v);
		// TODO Auto-generated constructor stub
	}



	public Rectangle(int  x, int y,int z, int w) {
		super(x, y);
		x =w;
		int v = 0;
		y= v;
		z = width;
		w = height;
		System.out.println("Constructing a Rectangle");
	}
	
}




Next Square.java

public class Square extends Rectangle
{
	String color;
	private String col;
	public Square(int x, int y, int z, int w, String col)  {
		super(x, y, z, w);
		
		//int c;
		//int d;
		//int b;
		// TODO Auto-generated constructor stub
		Rectangle r = new Rectangle(a, b, c, d);
		r.a = x;
		r.b = y;
		r.c =width;
		r.d = height;
		
	}
	public Square(int w, int v, String color) {
		super(w, v);
		this.color = color;
	}
	//public Square() {
		// TODO Auto-generated constructor stub
		//super(x,y,z,w);
	//}
	//public Square() {
		// TODO Auto-generated constructor stub
	//	Square s = new Square();
	//	s.center = (x,y);
	//}

	
	//public void  showColor()
	//{
	//	
	//}
	public void showColor(String string) {
		// TODO Auto-generated method stub
		System.out.println("Color of the square is : " + col);
		
	}
}



Finally Main Program InheritProgram.java

public class InheritProgram {
	public static void main (String[] args)
	{
		Square s = new Square(0, 0, 0, 0, null);
		s.center.x = 10;
		s.center.y = 20;
		s.width = 15;
		s.height = 15;
		s.color = "blue";
		s.displayCenter();
		s.showColor("blue");
	}
}



I am receiving a null pointer exception error in numerous file. I would appreciate any guidance on this matter.



Precise error message is as follows:-


Constructing a GeometricShape
Exception in thread "main" java.lang.NullPointerException
at GeometricShape.<init>(GeometricShape.java:11)
at Rectangle.<init>(Rectangle.java:20)
at Square.<init>(Square.java:6)
at InheritProgram.main(InheritProgram.java:4)


Thanks


Farukh

This post has been edited by farukh36: 26 August 2009 - 12:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: inheritance and constructors

#2 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: inheritance and constructors

Posted 25 August 2009 - 11:59 PM

Please post the exact errors you are getting.
Was This Post Helpful? 1
  • +
  • -

#3 prankster  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 67
  • Joined: 31-July 09

Re: inheritance and constructors

Posted 26 August 2009 - 12:09 AM

Soemthing I noticed right away was this line:

w = center.x;



I think what you are trying to do is have your point center's x value equal w, but instead you are assigning the value of center.x (NULL) to w. then later on when you call this line:

System.out.println("X and Y coordinates of center are" + center.x + center.y);



then center.x and center.y are still not assigned a value. instead what you should have is:

center.x = w;
center.y = v;


Was This Post Helpful? 1
  • +
  • -

#4 arsh_aarsh  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 36
  • Joined: 13-October 08

Re: inheritance and constructors

Posted 26 August 2009 - 12:12 AM

Quote

Try this!!

You need to initialize the object of the class Point



class Point{
	int x;
	int y;
	
	public Point(int k, int j)
	{
		x=k;
		y=j;
		System.out.println("Constructing a Point ");
	
	}
	
}

class GeometricShape
{
	
	Point center;

	
	public  GeometricShape(int w, int v)
	{
		System.out.println("Constructing a GeometricShape");
		center = new Point(w,v);		  //you missed this

	}
	public void displayCenter()
	{
		System.out.println("X and Y coordinates of center are" + center.x + center.y);
		
	}
}

class Rectangle extends GeometricShape
{
	int width;
	int height;
	public int a;
	public int b;
	public int c;
	public int d;
	
	
	
	public Rectangle(int w, int v) {
		super(w, v);
		// TODO Auto-generated constructor stub
	}



	public Rectangle(int  x, int y,int z, int w) {
		super(x, y);
		x =w;
		int v = 0;
		y= v;
		z = width;
		w = height;
		System.out.println("Constructing a Rectangle");
	}
	
}

class Square extends Rectangle
{
	String color;
	private String col;
	public Square(int x, int y, int z, int w, String col)  {
		
	super(x, y, z, w);
		this.col = col;
		Rectangle r = new Rectangle(a, b, c, d);
		r.a = x;
		r.b = y;
		r.c =width;
		r.d = height;
		
	}
	public Square(int w, int v, String color) {
		super(w, v);
		this.color = color;
	}
	//public Square() {
		// TODO Auto-generated constructor stub
		//super(x,y,z,w);
	//}
	//public Square() {
		// TODO Auto-generated constructor stub
	//	Square s = new Square();
	//	s.center = (x,y);
	//}

	
	//public void  showColor()
	//{
	//	
	//}
	public void showColor(String string) {
		// TODO Auto-generated method stub
		System.out.println("Color of the square is : " + col);
		
	}
}

public class InheritProgram {
	public static void main (String[] args)
	{
		Square s = new Square(0, 0, 0, 0, null);
		s.center.x = 10;
		s.center.y = 20;
		s.width = 15;
		s.height = 15;
		s.color = "blue";
		s.displayCenter();
		s.showColor("blue");
	}
}



This post has been edited by arsh_aarsh: 26 August 2009 - 12:14 AM

Was This Post Helpful? 0
  • +
  • -

#5 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: inheritance and constructors

Posted 26 August 2009 - 12:16 AM

View Postprankster, on 25 Aug, 2009 - 11:09 PM, said:

Soemthing I noticed right away was this line:

w = center.x;



I think what you are trying to do is have your point center's x value equal w, but instead you are assigning the value of center.x (NULL) to w. then later on when you call this line:

System.out.println("X and Y coordinates of center are" + center.x + center.y);



then center.x and center.y are still not assigned a value. instead what you should have is:

center.x = w;
center.y = v;



You also have to initialize center or your still going to get a null pointer exception.
Was This Post Helpful? 0
  • +
  • -

#6 arsh_aarsh  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 36
  • Joined: 13-October 08

Re: inheritance and constructors

Posted 26 August 2009 - 12:19 AM

View Postsyfran, on 25 Aug, 2009 - 11:16 PM, said:

View Postprankster, on 25 Aug, 2009 - 11:09 PM, said:

Soemthing I noticed right away was this line:

w = center.x;



I think what you are trying to do is have your point center's x value equal w, but instead you are assigning the value of center.x (NULL) to w. then later on when you call this line:

System.out.println("X and Y coordinates of center are" + center.x + center.y);



then center.x and center.y are still not assigned a value. instead what you should have is:

center.x = w;
center.y = v;



You also have to initialize center or your still going to get a null pointer exception.



Thats been taken care of in the code tha i posted...
Was This Post Helpful? 1
  • +
  • -

#7 farukh36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-March 09

Re: inheritance and constructors

Posted 26 August 2009 - 12:27 AM

View Postarsh_aarsh, on 25 Aug, 2009 - 11:12 PM, said:

Quote

Try this!!

You need to initialize the object of the class Point



class Point{
	int x;
	int y;
	
	public Point(int k, int j)
	{
		x=k;
		y=j;
		System.out.println("Constructing a Point ");
	
	}
	
}

class GeometricShape
{
	
	Point center;

	
	public  GeometricShape(int w, int v)
	{
		System.out.println("Constructing a GeometricShape");
		center = new Point(w,v);		  //you missed this

	}
	public void displayCenter()
	{
		System.out.println("X and Y coordinates of center are" + center.x + center.y);
		
	}
}

class Rectangle extends GeometricShape
{
	int width;
	int height;
	public int a;
	public int b;
	public int c;
	public int d;
	
	
	
	public Rectangle(int w, int v) {
		super(w, v);
		// TODO Auto-generated constructor stub
	}



	public Rectangle(int  x, int y,int z, int w) {
		super(x, y);
		x =w;
		int v = 0;
		y= v;
		z = width;
		w = height;
		System.out.println("Constructing a Rectangle");
	}
	
}

class Square extends Rectangle
{
	String color;
	private String col;
	public Square(int x, int y, int z, int w, String col)  {
		
	super(x, y, z, w);
		this.col = col;
		Rectangle r = new Rectangle(a, b, c, d);
		r.a = x;
		r.b = y;
		r.c =width;
		r.d = height;
		
	}
	public Square(int w, int v, String color) {
		super(w, v);
		this.color = color;
	}
	//public Square() {
		// TODO Auto-generated constructor stub
		//super(x,y,z,w);
	//}
	//public Square() {
		// TODO Auto-generated constructor stub
	//	Square s = new Square();
	//	s.center = (x,y);
	//}

	
	//public void  showColor()
	//{
	//	
	//}
	public void showColor(String string) {
		// TODO Auto-generated method stub
		System.out.println("Color of the square is : " + col);
		
	}
}

public class InheritProgram {
	public static void main (String[] args)
	{
		Square s = new Square(0, 0, 0, 0, null);
		s.center.x = 10;
		s.center.y = 20;
		s.width = 15;
		s.height = 15;
		s.color = "blue";
		s.displayCenter();
		s.showColor("blue");
	}
}




Hi

It works except for the color is showing as Null in the output.

Please guide

Farukh
Was This Post Helpful? 0
  • +
  • -

#8 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: inheritance and constructors

Posted 26 August 2009 - 12:34 AM

Why does one constuctor initialize color and the other col? To get rid of the null you need to pick one and stick with it.
Was This Post Helpful? 1
  • +
  • -

#9 arsh_aarsh  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 36
  • Joined: 13-October 08

Re: inheritance and constructors

Posted 27 August 2009 - 12:09 AM

The only reason for that is that in when you are initializing the object of class Square in MAIN, you are passing the value of color as NULL.

You need to mention the color as some string.

For example,


Square s = new Square(0, 0, 0, 0,"white");




That would work the way you want... Try it!!

This post has been edited by arsh_aarsh: 27 August 2009 - 12:10 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1