Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java BoundingBox program

Tags:

java

eclipse

I'm beginner with programming in Java. I would make an application to count max Point, min Point, width and height. I write in eclipse.

  1. I become this error:

    Exception in thread "main" java.lang.NullPointerException at BoundingBox.height(BoundingBox.java:50) at App.main(App.java:39)`

  2. and my printed points looks like

    Point@659e0bfd Point@2a139a55 Point@15db9742

How should the method toString looks like in this code?

Point

public class Point {
    private double x;
    private double y;

    public Point(double xnew, double ynew) {
        x = xnew;
        y = ynew;
    }

    public double getX() {
        return x;
    }

    public void setX(double xnew) {
        x = xnew;
    }

    public double getY() {
        return y;
    }

    public void setY(double ynew) {
        y = ynew;
    }
}

BoundingBox

import java.util.List;

public class BoundingBox {

    private static Point minPoint;
    private static Point maxPoint;

    public BoundingBox(List<Point> manyPoints) {
        double minX = manyPoints.get(0).getX();
        double maxX = manyPoints.get(0).getX();
        double minY = manyPoints.get(0).getY();
        double maxY = manyPoints.get(0).getY();

        for (int i = 0; i < manyPoints.size(); i++) {
            Point test = manyPoints.get(i);
            test.getX();

            if (test.getX() < minX) {
                minX = test.getX();

            }

            if (test.getX() > maxX) {
                maxX = test.getX();
            }

            if (test.getY() < minY) {
                minY = test.getY();

            }

            if (test.getY() > maxY) {
                maxY = test.getY();

            }

            minPoint = new Point(minX, minY);
            maxPoint = new Point(maxX, maxY);

        }
    }

    public static double width() {

        double a = (maxPoint.getX() - minPoint.getX());
        return a;
    }

    public static double height() {
        System.out.println("minPoint = " + minPoint);
        System.out.println("maxPoint = " + maxPoint);
        double b = (maxPoint.getY() - minPoint.getY());
        return b;
    }

    public Point getMaxPoint() {
        return maxPoint;
    }

    public Point getMinPoint() {
        return minPoint;
    }
}

Application

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class App {
    public static void main(String[] args) {
        Point p1 = new Point(10.681,-48.857);
        Point p2 = new Point(96.980,20.724);
        Point p3 = new Point(66.647,-66.558);
        Point p4 = new Point(-2.674,-58.571);
        Point p5 = new Point(40.11,-12.342);
        Point p6 = new Point(27.782,46.809);
        Point p7 = new Point(54.759,-46.709);
        Point p8 = new Point(-33.89,-90.787);
        Point p9 = new Point(15.84,-67.553);
        Point p10 = new Point(19.481,51.331);


        List<Point> list1 = new ArrayList<Point>(); // Create ArrayList and add Points
        list1.add(p1);
        list1.add(p2);
        list1.add(p3);
        list1.add(p4);
        list1.add(p5);
        list1.add(p6);
        list1.add(p7);
        list1.add(p8);
        list1.add(p9);
        list1.add(p10);

        Iterator<Point> itr = list1.iterator( );
        while ( itr.hasNext( ) ) {
            System.out.println( itr.next( ) );
        }

        double c = BoundingBox.height();
        double d = BoundingBox.width();

        System.out.println(" The Points: " + list1 );
        System.out.println(" BoundingBox have " + c + "height and" + d + "width." );
        System.out.println(" The max Point is " );
        System.out.println(" The min Point is " );
    }
}
like image 649
inerini9 Avatar asked Nov 26 '25 16:11

inerini9


1 Answers

Your variables:

private static Point minPoint;
private static Point maxPoint;

are initialized in BoundingBox constructor, but in your App you don't invoke it, so they are null. Just do the following:

new BoundingBox(list1);
double c = BoundingBox.height();
double d = BoundingBox.width();

and NullPointerException will disappear.

like image 54
Michal Kordas Avatar answered Nov 29 '25 05:11

Michal Kordas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!