Need help with nullpointerexception
Hi, I am having problem with my program. When I compile the program it says i have the null pointer exception. I think I am doing something wrong with the default constructor Polygon(). Any suggestions will be greatly appreciated. Below is the code for the program:
Code:
import java.text.NumberFormat;
import java.util.*;
public class Foothill
{
public static void main(String[] args) throws Exception
{
//declare and initialize objects
Point samplePoint1, samplePoint2;
samplePoint1 = new Point();
samplePoint2 = new Point();
//set the minimum and maximum value of the coordinates
Point.setRange(-20.0, 20.0);
//set value for point object
samplePoint1.set(-16, 25);
samplePoint2.set(6.3, 14.7);
//display the points
samplePoint1.displayPoint();
samplePoint2.displayPoint();
//reset and display the value of samplePoint2
samplePoint2.set(-3.4, -9);
samplePoint2.displayPoint();
//testing mutators for data filtering
if (samplePoint1.set(-14,21) == false)
System.out.println("Unable to set the coordinates to (-14,21)");
else
System.out.println("Coordiantes have been set to (-14, 21)");
if (samplePoint1.set(-20.001, 20.001) == false)
System.out.println("Unable to set the coordiantes to (-20.001, 20.001)");
else
System.out.println("Coordiantes have to been set to (-20.001, 20.001)");
//declare and initialize arrays
double[] xValue1 = {-2, -7, 7, -1, 9};
double[] yValue1 = {-6.5, 7.56, 8.99, 3.3, 9.99};
double[] xValue2 = {4.0, 6.0, -5.5, 2};
double[] yValue2 = {-5, 4.2, -5.6, 11};
double[] xValue3 = {20, 0, 1.56};
double[] yValue3 = {10, 5.5, -6};
//declare and initialize objects for class "Polygon"
Polygon samplePolygon1, samplePolygon2, samplePolygon3;
samplePolygon1 = new Polygon();
samplePolygon2 = new Polygon(4, xValue2, yValue2);
samplePolygon3 = new Polygon(3, xValue3, yValue3);
//set the points for object samplePolygon1
samplePolygon1.setPoints(5, xValue1, yValue1);
//display the points involved for each polygon
samplePolygon1.showPolygon();
samplePolygon2.showPolygon();
samplePolygon3.showPolygon();
//add points in the polygons
samplePolygon2.addPoint(-19.99, 15);
samplePolygon3.addPoint(3, 5);
samplePolygon1.addPoint(-20, 15);
samplePolygon1.addPoint(11, 14);
}
}
//introduce and define class "Point"
class Point
{
//instance data
private double x;
private double y;
private static double MIN_VAL;
private static double MAX_VAL;
//default constructor without parameters
public Point()
{
//initialize the minimum and maximum value of co-ordinate
MIN_VAL = -10.0;
MAX_VAL = 10.0;
//declare variable
double coOrdinate;
//calculate the co-ordinates for a point
coOrdinate = (MIN_VAL + MAX_VAL)/2;
x = coOrdinate;
y = coOrdinate;
}
//parameters taking constructor
public Point(double x, double y)
{
if (!set(x, y))
set(0,0);
}
//mutator
boolean set(double x, double y)
{
if (x < MIN_VAL || x > MAX_VAL)
return false;
else
this.x = x;
if (y < MIN_VAL || y > MAX_VAL)
return false;
else
this.y = y;
return true;
}
//accessors
double getX()
{
return x;
}
double getY()
{
return y;
}
//display the coordinates of the point
public void displayPoint()
{
System.out.println("(" + getX() + ", " + getY() + ")");
}
//allow the user to set the minimum and maximum value of the coordinates
public static boolean setRange(double newMinVal, double newMaxVal)
{
//maximum value has to be greater than the minimum value
if (newMaxVal <= newMinVal)
return false;
else
{
MIN_VAL = newMinVal;
MAX_VAL = newMaxVal;
}
return true;
}
}
//introduce and define class "Polygon"
class Polygon
{
//constant static data
public static final int MAX_POINTS = 75;
//instance data members
private int numPoints;
private Point[] points;
private double[] xArray;
private double[] yArray;
//default constructor without parameter
public Polygon()
{
numPoints = 0;
points = new Point[MAX_POINTS];
//setPoints(0, new double[]{0.0}, new double[]{0});
int k;
for (k = 0; k < points.length; k++)
{
points[k].set(0, 0);
this.points[k] = points[k];
}
}
//parameters taking constructors
public Polygon(int numpoints, double xArray[], double yArray[])
{
if (!setPoints(numpoints, xArray, yArray))
{ numPoints = 0;
int k;
setPoints(0, new double[]{0.0}, new double[]{0});
points = new Point[MAX_POINTS];
for (k = 0; k < points.length; k++)
points[k].set(0, 0);
}
}
//mutators
public boolean setPoints(int numPoints, double xValues[], double yValues[])
{
if (numPoints < 0)
return false;
if (points.length < numPoints)
return false;
if(xValues.length != yValues.length)
return false;
else
{
this.numPoints = numPoints;
//declare variable
int K;
for (K = 0; K < numPoints; K++)
{
points[K].set(xValues[K], yValues[K]);
}
}
return true;
}
//function to display all the points involved to draw the polygon
public void showPolygon()
{
//declare and initialize variables
int K;
if (numPoints == 0)
System.out.println("There is not a single point assigned to the polygon");
String allThePoints = new String("(" + points[0].getX() + ", " + points[0].getY() + ")");
for (K = 1; K < numPoints; K++ )
{
allThePoints = allThePoints + ", " + "(" + points[K].getX() + ", " + points[K].getY() + ")";
}
}
//function that let the user store additional point
boolean addPoint( double x, double y)
{
numPoints = numPoints + 1;
//only if the points array can store more value
if (numPoints > points.length)
return false;
else
{
points[numPoints - 1].set(x, y);
}
return true;
}
//function that let you store additional point
boolean addPoint(Point p)
{
numPoints = numPoints + 1;
//only if the points array can store more value
if (numPoints > points.length)
return false;
else
points[numPoints - 1] = p;
return true;
}
}
Below is the message I get when I compile the program:
[note]
Exception in thread "main" java.lang.NullPointerException
at Polygon.<init>(Foothill.java:170)
at Foothill.main(Foothill.java:49)
[/note]
Re: Need help with nullpointerexception
Quote:
Originally Posted by
andrewmills
Below is the message I get when I
compile the program:
Code:
Exception in thread "main" java.lang.NullPointerException
at Polygon.<init>(Foothill.java:170)
at Foothill.main(Foothill.java:49)
No, that's impossible as you can't get NPE from compilation. It can only be thrown when you run the program, and that's a big difference.
The key to solving a NPE, and the thing you haven't told us is knowing the line that throws it because on that line you are using a variable that is in fact null. Once you have identified the variable, you can track back in your code and find out why it has not been given a valid reference.
So on that note, please show us which lines are involved with this exception? Which lines are line 49 and 170 of your Foothill class?
Re: Need help with nullpointerexception
When you identify the line, here's a suggestion to help you better visualize the problem and its solution. Visualize a reference array, meaning an array of objects not primitives like ints, as being similar to an egg carton. If you create the array, you've only created the carton, and you've not yet filled it with eggs. You can't start using eggs in the carton and cooking with them until you've first put eggs into the carton, until you've first put objects into the array.
Re: Need help with nullpointerexception
Hi Fubarable. Thank you so much for your response. The problems I am having are in the lines below:
Line 170:
Code:
points[k].set(0, 0);
Line 49:
Code:
samplePolygon1 = new Polygon();
And, I think my problem is with the array points[]. Since, this array has a type of class Point, I am not really sure how to put the objects into it. If it was array of type String, double or int, I would know how to initialize it.
Re: Need help with nullpointerexception
You need to fill it with Point objects. Consider using a for loop and placing the Point objects into it that way, but doing so in the constructor right after creating the array and before using it.
Re: Need help with nullpointerexception
Quote:
Originally Posted by
Fubarable
You need to fill it with Point objects. Consider using a for loop and placing the Point objects into it that way, but doing so in the constructor right after creating the array and before using it.
I am not sure if this is how you put the point objects or not. But, I tried doing it anyway and it did not work. Here is how I redefined my constructor Polygon():
Code:
public Polygon()
{
numPoints = 0;
points = new Point[MAX_POINTS];
//setPoints(0, new double[]{0.0}, new double[]{0});
int k;
for (k = 0; k < points.length; k++)
{
this.points[k] = points[k];
}
}
I think I should mention that after redefining my constructor as above, when I try to run the program, I am getting error in different lines. The errors I am getting are in Lines:
Line 196:
Code:
if (points.length < numPoints)
Line 177:
Code:
if (!setPoints(numpoints, xArray, yArray))
Line 50:
Code:
samplePolygon2 = new Polygon(4, xValue2, yValue2);
Below is the complete code for the program:
Code:
import java.text.NumberFormat;
import java.util.*;
public class Foothill
{
public static void main(String[] args) throws Exception
{
//declare and initialize objects
Point samplePoint1, samplePoint2;
samplePoint1 = new Point();
samplePoint2 = new Point();
//set the minimum and maximum value of the coordinates
Point.setRange(-20.0, 20.0);
//set value for point object
samplePoint1.set(-16, 25);
samplePoint2.set(6.3, 14.7);
//display the points
samplePoint1.displayPoint();
samplePoint2.displayPoint();
//reset and display the value of samplePoint2
samplePoint2.set(-3.4, -9);
samplePoint2.displayPoint();
//testing mutators for data filtering
if (samplePoint1.set(-14,21) == false)
System.out.println("Unable to set the coordinates to (-14,21)");
else
System.out.println("Coordiantes have been set to (-14, 21)");
if (samplePoint1.set(-20.001, 20.001) == false)
System.out.println("Unable to set the coordiantes to (-20.001, 20.001)");
else
System.out.println("Coordiantes have to been set to (-20.001, 20.001)");
//declare and initialize arrays
double[] xValue1 = {-2, -7, 7, -1, 9};
double[] yValue1 = {-6.5, 7.56, 8.99, 3.3, 9.99};
double[] xValue2 = {4.0, 6.0, -5.5, 2};
double[] yValue2 = {-5, 4.2, -5.6, 11};
double[] xValue3 = {20, 0, 1.56};
double[] yValue3 = {10, 5.5, -6};
//declare and initialize objects for class "Polygon"
Polygon samplePolygon1, samplePolygon2, samplePolygon3;
samplePolygon1 = new Polygon();
samplePolygon2 = new Polygon(4, xValue2, yValue2);
samplePolygon3 = new Polygon(3, xValue3, yValue3);
//set the points for object samplePolygon1
samplePolygon1.setPoints(5, xValue1, yValue1);
//display the points involved for each polygon
samplePolygon1.showPolygon();
samplePolygon2.showPolygon();
samplePolygon3.showPolygon();
//add points in the polygons
samplePolygon2.addPoint(-19.99, 15);
samplePolygon3.addPoint(3, 5);
samplePolygon1.addPoint(-20, 15);
samplePolygon1.addPoint(11, 14);
}
}
//introduce and define class "Point"
class Point
{
//instance data
private double x;
private double y;
private static double MIN_VAL;
private static double MAX_VAL;
//default constructor without parameters
public Point()
{
//initialize the minimum and maximum value of co-ordinate
MIN_VAL = -10.0;
MAX_VAL = 10.0;
//declare variable
double coOrdinate;
//calculate the co-ordinates for a point
coOrdinate = (MIN_VAL + MAX_VAL)/2;
x = coOrdinate;
y = coOrdinate;
}
//parameters taking constructor
public Point(double x, double y)
{
if (!set(x, y))
set(0,0);
}
//mutator
boolean set(double x, double y)
{
if (x < MIN_VAL || x > MAX_VAL)
return false;
else
this.x = x;
if (y < MIN_VAL || y > MAX_VAL)
return false;
else
this.y = y;
return true;
}
//accessors
double getX()
{
return x;
}
double getY()
{
return y;
}
//display the coordinates of the point
public void displayPoint()
{
System.out.println("(" + getX() + ", " + getY() + ")");
}
//allow the user to set the minimum and maximum value of the coordinates
public static boolean setRange(double newMinVal, double newMaxVal)
{
//maximum value has to be greater than the minimum value
if (newMaxVal <= newMinVal)
return false;
else
{
MIN_VAL = newMinVal;
MAX_VAL = newMaxVal;
}
return true;
}
}
//introduce and define class "Polygon"
class Polygon
{
//constant static data
public static final int MAX_POINTS = 75;
//instance data members
private int numPoints;
private Point[] points;
private double[] xArray;
private double[] yArray;
//default constructor without parameter
public Polygon()
{
numPoints = 0;
points = new Point[MAX_POINTS];
//setPoints(0, new double[]{0.0}, new double[]{0});
int k;
for (k = 0; k < points.length; k++)
{
this.points[k] = points[k];
}
}
//parameters taking constructors
public Polygon(int numpoints, double xArray[], double yArray[])
{
if (!setPoints(numpoints, xArray, yArray))
{ numPoints = 0;
int k;
setPoints(0, new double[]{0.0}, new double[]{0});
points = new Point[MAX_POINTS];
for (k = 0; k < points.length; k++)
{
this.points[k] = points[k];
}
}
}
//mutators
public boolean setPoints(int numPoints, double xValues[], double yValues[])
{
if (numPoints < 0)
return false;
if (points.length < numPoints)
return false;
if(xValues.length != yValues.length)
return false;
else
{
this.numPoints = numPoints;
//declare variable
int K;
for (K = 0; K < numPoints; K++)
{
points[K].set(xValues[K], yValues[K]);
}
}
return true;
}
//function to display all the points involved to draw the polygon
public void showPolygon()
{
//declare and initialize variables
int K;
if (numPoints == 0)
System.out.println("There is not a single point assigned to the polygon");
String allThePoints = new String("(" + points[0].getX() + ", " + points[0].getY() + ")");
for (K = 1; K < numPoints; K++ )
{
allThePoints = allThePoints + ", " + "(" + points[K].getX() + ", " + points[K].getY() + ")";
}
}
//function that let the user store additional point
boolean addPoint( double x, double y)
{
numPoints = numPoints + 1;
//only if the points array can store more value
if (numPoints > points.length)
return false;
else
{
points[numPoints - 1].set(x, y);
}
return true;
}
//function that let you store additional point
boolean addPoint(Point p)
{
numPoints = numPoints + 1;
//only if the points array can store more value
if (numPoints > points.length)
return false;
else
points[numPoints - 1] = p;
return true;
}
}
Re: Need help with nullpointerexception
I am sorry that's not how I redefine my constructor. I copied the wrong code. This is how I actually redefined my constructor:
Code:
//default constructor without parameter
public Polygon()
{
numPoints = 0;
points = new Point[MAX_POINTS];
//setPoints(0, new double[]{0.0}, new double[]{0});
int k;
for (k = 0; k < points.length; k++)
{
this.points[k] = new Point();
}
}
Below is my complete code for the program:
Code:
import java.text.NumberFormat;
import java.util.*;
public class Foothill
{
public static void main(String[] args) throws Exception
{
//declare and initialize objects
Point samplePoint1, samplePoint2;
samplePoint1 = new Point();
samplePoint2 = new Point();
//set the minimum and maximum value of the coordinates
Point.setRange(-20.0, 20.0);
//set value for point object
samplePoint1.set(-16, 25);
samplePoint2.set(6.3, 14.7);
//display the points
samplePoint1.displayPoint();
samplePoint2.displayPoint();
//reset and display the value of samplePoint2
samplePoint2.set(-3.4, -9);
samplePoint2.displayPoint();
//testing mutators for data filtering
if (samplePoint1.set(-14,21) == false)
System.out.println("Unable to set the coordinates to (-14,21)");
else
System.out.println("Coordiantes have been set to (-14, 21)");
if (samplePoint1.set(-20.001, 20.001) == false)
System.out.println("Unable to set the coordiantes to (-20.001, 20.001)");
else
System.out.println("Coordiantes have to been set to (-20.001, 20.001)");
//declare and initialize arrays
double[] xValue1 = {-2, -7, 7, -1, 9};
double[] yValue1 = {-6.5, 7.56, 8.99, 3.3, 9.99};
double[] xValue2 = {4.0, 6.0, -5.5, 2};
double[] yValue2 = {-5, 4.2, -5.6, 11};
double[] xValue3 = {20, 0, 1.56};
double[] yValue3 = {10, 5.5, -6};
//declare and initialize objects for class "Polygon"
Polygon samplePolygon1, samplePolygon2, samplePolygon3;
samplePolygon1 = new Polygon();
samplePolygon2 = new Polygon(4, xValue2, yValue2);
samplePolygon3 = new Polygon(3, xValue3, yValue3);
//set the points for object samplePolygon1
samplePolygon1.setPoints(5, xValue1, yValue1);
//display the points involved for each polygon
samplePolygon1.showPolygon();
samplePolygon2.showPolygon();
samplePolygon3.showPolygon();
//add points in the polygons
samplePolygon2.addPoint(-19.99, 15);
samplePolygon3.addPoint(3, 5);
samplePolygon1.addPoint(-20, 15);
samplePolygon1.addPoint(11, 14);
}
}
//introduce and define class "Point"
class Point
{
//instance data
private double x;
private double y;
private static double MIN_VAL;
private static double MAX_VAL;
//default constructor without parameters
public Point()
{
//initialize the minimum and maximum value of co-ordinate
MIN_VAL = -10.0;
MAX_VAL = 10.0;
//declare variable
double coOrdinate;
//calculate the co-ordinates for a point
coOrdinate = (MIN_VAL + MAX_VAL)/2;
x = coOrdinate;
y = coOrdinate;
}
//parameters taking constructor
public Point(double x, double y)
{
if (!set(x, y))
set(0,0);
}
//mutator
boolean set(double x, double y)
{
if (x < MIN_VAL || x > MAX_VAL)
return false;
else
this.x = x;
if (y < MIN_VAL || y > MAX_VAL)
return false;
else
this.y = y;
return true;
}
//accessors
double getX()
{
return x;
}
double getY()
{
return y;
}
//display the coordinates of the point
public void displayPoint()
{
System.out.println("(" + getX() + ", " + getY() + ")");
}
//allow the user to set the minimum and maximum value of the coordinates
public static boolean setRange(double newMinVal, double newMaxVal)
{
//maximum value has to be greater than the minimum value
if (newMaxVal <= newMinVal)
return false;
else
{
MIN_VAL = newMinVal;
MAX_VAL = newMaxVal;
}
return true;
}
}
//introduce and define class "Polygon"
class Polygon
{
//constant static data
public static final int MAX_POINTS = 75;
//instance data members
private int numPoints;
private Point[] points;
private double[] xArray;
private double[] yArray;
//default constructor without parameter
public Polygon()
{
numPoints = 0;
points = new Point[MAX_POINTS];
//setPoints(0, new double[]{0.0}, new double[]{0});
int k;
for (k = 0; k < points.length; k++)
{
this.points[k] = new Point();
}
}
//parameters taking constructors
public Polygon(int numpoints, double xArray[], double yArray[])
{
if (!setPoints(numpoints, xArray, yArray))
{ numPoints = 0;
int k;
//setPoints(0, new double[]{0.0}, new double[]{0});
points = new Point[MAX_POINTS];
for (k = 0; k < points.length; k++)
{
this.points[k] = new Point();
}
}
}
//mutators
public boolean setPoints(int numPoints, double xValues[], double yValues[])
{
if (numPoints < 0)
return false;
if (points.length < numPoints)
return false;
if(xValues.length != yValues.length)
return false;
else
{
this.numPoints = numPoints;
//declare variable
int K;
for (K = 0; K < numPoints; K++)
{
points[K].set(xValues[K], yValues[K]);
}
}
return true;
}
//function to display all the points involved to draw the polygon
public void showPolygon()
{
//declare and initialize variables
int K;
if (numPoints == 0)
System.out.println("There is not a single point assigned to the polygon");
String allThePoints = new String("(" + points[0].getX() + ", " + points[0].getY() + ")");
for (K = 1; K < numPoints; K++ )
{
allThePoints = allThePoints + ", " + "(" + points[K].getX() + ", " + points[K].getY() + ")";
}
}
//function that let the user store additional point
boolean addPoint( double x, double y)
{
numPoints = numPoints + 1;
//only if the points array can store more value
if (numPoints > points.length)
return false;
else
{
points[numPoints - 1].set(x, y);
}
return true;
}
//function that let you store additional point
boolean addPoint(Point p)
{
numPoints = numPoints + 1;
//only if the points array can store more value
if (numPoints > points.length)
return false;
else
points[numPoints - 1] = p;
return true;
}
}
Following are the errors I am getting when I run the program;
Line 197:
Code:
if (points.length < numPoints)
return false; //line 197
Line 178
Code:
public Polygon(int numpoints, double xArray[], double yArray[])
{
if (!setPoints(numpoints, xArray, yArray))
{ numPoints = 0; //Line 178
line 50:
Code:
samplePolygon2 = new Polygon(4, xValue2, yValue2);
Re: Need help with nullpointerexception
Now your problem is the other way around: You need to post the actual error messages not just the lines that cause them.