Autor Tema: Exception in thread "main" java.lang.NullPointerException  (Leído 2809 veces)

josephb401

  • Visitante
Exception in thread "main" java.lang.NullPointerException
« en: 25 de Septiembre 2015, 04:43 »
Hola a todos estoy teniendo un problema con este programa quisiera saber si alguien me puede ayudar a resolver este problema? El programa tiene 2 clases y la tercera tiene el main. Muchas gracias.

Código: [Seleccionar]
import java.util.Scanner; // needed for Scanner class
import java.text.DecimalFormat; // needed for DecimalFormat class

public class CarpetTester
{
public static void main(String[] arg)
{

double length;
double width;
double price;

Scanner keyboard = new Scanner(System.in);

DecimalFormat df = new DecimalFormat("$#,##0.00");

System.out.print("Please enter the length of the room: ");
length = keyboard.nextDouble();

System.out.print("Please enter the width of the room: ");
width = keyboard.nextDouble();

System.out.print("Please enter the price per squart foot: ");
price = keyboard.nextDouble();

RoomDimensio dimension = new RoomDimensio(length, width);

RoomCarpet carpet = new RoomCarpet(dimension, price);

System.out.println("The total cost of the carpet is: " + df.format(carpet.getTotalCost() ) );


}


}

Código: [Seleccionar]
public class RoomDimensio
{

private double length;
private double width;


public RoomDimensio(double len, double wid)
{
len = length;
wid = width;

}

public double getArea()
{

double area = (length * width);

return area;

}

public String toString()
{
return "Length: " + length + "width: " + width +" Area: " + getArea();

}

}


Código: [Seleccionar]
public class RoomCarpet
{

private RoomDimensio dimension;
private double carpetCost;


public RoomCarpet(RoomDimensio dim, double cost)
{

dim = dimension;
cost = carpetCost;

}

public double getTotalCost()
{

double finalCost = (carpetCost * dimension.getArea());

return finalCost;

}

public String toString()
{

return "Total cost: " + getTotalCost();

}

}

« Última modificación: 28 de Septiembre 2015, 08:22 por César Krall »

DRANXZ88

  • Avanzado
  • ****
  • Mensajes: 356
    • Ver Perfil
Re:Exception in thread "main" java.lang.NullPointerException
« Respuesta #1 en: 26 de Septiembre 2015, 03:50 »
Hola hice alguna modificaciones en tu código si por ahí te sirve te voy a enviar para comparar 
con lo que tenes
clase principal main
Código: [Seleccionar]
package carpettester;

import java.text.DecimalFormat;
import java.util.Scanner;

public class CarpetTester {

    public static void main(String[] args) {
        double length;
        double width;
        double price;
        double area;
        double total;

        Scanner keyboard = new Scanner(System.in);

        DecimalFormat df = new DecimalFormat("$#,##0.00");

        System.out.print("Please enter the length of the room: ");
        length = keyboard.nextDouble();

        System.out.print("Please enter the width of the room: ");
        width = keyboard.nextDouble();

        System.out.print("Please enter the price per squart foot: ");
        price = keyboard.nextDouble();

        RoomDimensio dimension = new RoomDimensio();
        area = dimension.RoomArea(length, width);

        RoomCarpet carpet = new RoomCarpet();
        total = carpet.getTotalCost(area, price);

        System.out.println("The total cost of the carpet is: " + df.format(total));
    }

}
clase para obtener la dimension
Código: [Seleccionar]
package carpettester;

public class RoomDimensio {

    public RoomDimensio() {
    }

    public double RoomArea(double len, double wid) {

        double area = (len * wid);
        return area;

    }

}
clase para obtener el total
Código: [Seleccionar]
package carpettester;

public class RoomCarpet {

    public RoomCarpet() {
    }

    public double getTotalCost(double area, double price) {

        double finalCost = (price * area);

        return finalCost;

    }

}

 

Sobre la educación, sólo puedo decir que es el tema más importante en el que nosotros, como pueblo, debemos involucrarnos.

Abraham Lincoln (1808-1865) Presidente estadounidense.

aprenderaprogramar.com: Desde 2006 comprometidos con la didáctica y divulgación de la programación

Preguntas y respuestas

¿Cómo establecer o cambiar la imagen asociada (avatar) de usuario?
  1. Inicia sesión con tu nombre de usuario y contraseña.
  2. Pulsa en perfil --> perfil del foro
  3. Elige la imagen personalizada que quieras usar. Puedes escogerla de una galería de imágenes o subirla desde tu ordenador.
  4. En la parte final de la página pulsa el botón "cambiar perfil".