Question

In: Computer Science

I keep get this exception error Exception in thread "main" java.lang.NullPointerException    at Quadrilateral.returnCoordsAsString(Quadrilateral.java:44)    at...

I keep get this exception error Exception in thread "main" java.lang.NullPointerException
   at Quadrilateral.returnCoordsAsString(Quadrilateral.java:44)
   at Quadrilateral.toString(Quadrilateral.java:51)
   at tester1.main(tester1.java:39)

In this program I needed to make a Point class to create a coordinate square from x and y. I also needed to make a Quadrilateral class that has an instance reference variable to Point

.The Quadrilateral class then inherits itself to other classes or in this case other shapes like square, trapazoid. I thought I did it right but for some reason there seems to be a problem with the toString class.

Here is Point class

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

public Point(double point1, double point2)
{
x = point1;
y = point2;
}
public Point()
{
x = 0.00;
y = 0.00;
}
public void setx(double x)
{
this.x = x;
}
public void sety(double y)
{
this.y = y;
}

public double getx()
{
return x;
}
public double gety()
{
return y;
}


@Override
public String toString()
{
return "(" + getx() + " , " + gety() + ")";
  
}
  
}

here is the Quadrilateral class

public class Quadrilateral {
Point Corner1;
Point Corner2;
Point Corner3;
Point Corner4;
public Quadrilateral(double x1, double y1, double x2, double y2, double x3, double y3 , double x4, double y4)
{
Point Corner1 = new Point(x1, y1);
Point Corner2 = new Point(x2, y2);
Point Corner3 = new Point(x3, y3);
Point Corner4 = new Point(x4, y4);
  
}

public Point getCorner1()
{
return Corner1;
}
public Point getCorner2()
{
return Corner2;
  
}
public Point getCorner3()
{
return Corner3;
}
public Point getCorner4()
{
return Corner4;
}
public String returnCoordsAsString()
{
String name1 = getCorner1().toString() + " , " + getCorner2().toString() + " , " + getCorner3().toString() + " , " + getCorner4().toString();
return name1;
}

@Override
public String toString()
{
String name = " coordinates od Quadrilateral are : " + returnCoordsAsString();
return name;

}

  
}

and this is the diver class

public class tester1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Quadrilateral quadrilateral = new Quadrilateral(
1.1, 1.2, 6.6, 2.8, 6.2, 9.9, 2.2, 7.4 );
quadrilateral.toString();
  
}
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

Point.java

public class Point {

    private double x;

    private double y;

    public Point(double point1, double point2) {

        x = point1;

        y = point2;

    }

    public Point() {

        x = 0.00;

        y = 0.00;

    }

    public void setx(double x) {

        this.x = x;

    }

    public void sety(double y) {

        this.y = y;

    }

    public double getx() {

        return x;

    }

    public double gety() {

        return y;

    }

    @Override

    public String toString() {

        return "(" + getx() + " , " + gety() + ")";

    }

}

Quadralateral.java

public class Quadrilateral {

    Point Corner1;

    Point Corner2;

    Point Corner3;

    Point Corner4;

    public Quadrilateral(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {

        Corner1 = new Point(x1, y1);

        Corner2 = new Point(x2, y2);

        Corner3 = new Point(x3, y3);

        Corner4 = new Point(x4, y4);

    }

    public Point getCorner1() {

        return Corner1;

    }

    public Point getCorner2() {

        return Corner2;

    }

    public Point getCorner3() {

        return Corner3;

    }

    public Point getCorner4() {

        return Corner4;

    }

    public String returnCoordsAsString() {

        String name1 = getCorner1().toString() + " , " + getCorner2().toString() + " , " + getCorner3().toString()

                + " , " + getCorner4().toString();

        return name1;

    }

    @Override

    public String toString() {

        String name = " coordinates od Quadrilateral are : " + returnCoordsAsString();

        return name;

    }

}

tester1.java

public class tester1 {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

        Quadrilateral quadrilateral = new Quadrilateral(1.1, 1.2, 6.6, 2.8, 6.2, 9.9, 2.2, 7.4);

        System.out.println(quadrilateral.toString());

    }

}


Related Solutions

I keep getting this error, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for...
I keep getting this error, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0    at simpleInheritance/simpInher.Dwelling$DriverTest.main(Dwelling.java:146) Can someone help me fix it? import java.io.BufferedReader;    import java.io.FileNotFoundException;    import java.io.FileReader;    import java.util.*;    import java.io.*;    import java.io.FileWriter;    import java.io.IOException;    class Dwelling {    /*    Declaring Variables    */    String streetAddress;    String city;    String state;    String zipCode;    int bedrooms;    double bathrooms;       /*   ...
when i run the program on eclipse it gives me this error: Exception in thread "main"...
when i run the program on eclipse it gives me this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0    at SIM.main(SIM.java:12) how do I fix that ? (please fix it ) import java.util.*; import java.util.Scanner; import java.util.ArrayList; import java.io.File; import java.io.FileNotFoundException; import java.lang.Math; public class SIM{    public static void main(String[] args) throws FileNotFoundException {       int cacheSize = Integer.parseInt( args[1] ); int assoc = Integer.parseInt( args[2] ); int replacement = Integer.parseInt(...
I'm getting this error: Exception in thread "main" java.lang.NoSuchMethodError: main I tried using public static void...
I'm getting this error: Exception in thread "main" java.lang.NoSuchMethodError: main I tried using public static void main(String[] args){ but that negates all of the methods that I try to write. I'm just trying to make it so that I can enter values. Thanks. Code below: import java.util.Scanner; public class DataSet2 { private double value; private double sum; private int count; public void add(double value){    System.out.println("Enter values, enter -1 to finish");    Scanner scan = new Scanner(System.in);    value =...
Why am I getting this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds...
Why am I getting this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at HW3.main(HW3.java:6) The code: import java.io.FileWriter; import java.io.IOException; public class HW3 { public static void main(String[] args) throws IOException { // 0th argument contains the name of algorithm String algo = args[0]; // 1st argument contains the name of file // Make a new file FileWriter fw = new FileWriter(args[1]); if (algo.equals("p1")) { // 2nd argument comes in the form of...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
I keep on get a redefinition error and an undefined error. Customer List (1) CustomerList() and...
I keep on get a redefinition error and an undefined error. Customer List (1) CustomerList() and ~CustomerList() - default constructor and destructor. (2) bool addStore(Store*s) - Add an instance of store to the linked list. Return true if successful. (3) Store *removeStore(int ID) - Locate a Store in the list if it exists, remove and return it. (4) Store *getStore(int ID) - Locate a Store in the list and return a pointer to it. (5) bool updateStore(int ID, char *name,...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at CaesarCipher.encrypt(CaesarCipher.java:28) at CaesarCipher.main(CaesarCipher.java:52) public class CaesarCipher{     char[] encoder = new char[52];     char[] decoder = new char[52];      public CaesarCipher(int rotation)      {        for(int k=0 ; k < 26 ; k++)        {            encoder[k] = (char) ('A' + (k + rotation) % 26);            decoder[k] = (char) ('A' + (k - rotation + 26) % 26);        }        for(int j...
Can anyone merge these two java programs I keep getting an error can't find main classes....
Can anyone merge these two java programs I keep getting an error can't find main classes. MySorts.java public class MySorts { public static void insertSort(int[] arr) { int i, temp, j; for (i = 1; i < arr.length; i++) { temp = arr[i]; j = i - 1; while (j >= 0 && arr[j] > temp) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = temp; } } public static void selectSort(int[] arr)...
Why am I getting this error using 000webhost , but I do not get this error...
Why am I getting this error using 000webhost , but I do not get this error running XAMPP? Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd2/006/14881006/public_html/test/index.php:1) in /storage/ssd2/006/14881006/public_html/test/index.php on line 8 Code is below. ******************************************************************************************************************** <!DOCTYPE html> <?php    //Determine if the submit button has been clicked to set the cookie name and value    if (isset($_POST['name'])){            $cookie_name = $_POST['name'];            $cookie_value = $_POST['value'];            setcookie($cookie_name, $cookie_value,...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT