chief114
Bovino maduro
- Desde
- 4 Ago 2008
- Mensajes
- 455
- Tema Autor
- #1
Este es el programa:
import java.io.*;
class Vector_1 {
int i=0;
int n;
int xVector[];
int Vector[]=new int [n];
double cuadrado;
void setVector(int Vector)throws java.io.IOException{
Vector=Vector;
}
void setcuadrado()throws java.io.IOException{
cuadrado=xVector*xVector;
}
double getcuadrado()throws java.io.IOException{
return cuadrado;
}
}
class EjecutaVector_1{
public static void main(String args [])throws java.io.IOException{
System.out.println("Programa 1 de vectores_metodos.");
System.out.println("Para salir teclee 999999");
System.out.println("______________________________________________");
while(true){
int n;
Vector_1 mivector_1=new Vector_1();
n=Lectura_datos("Cuantos elementos empleara: ");
int Vector[]=new int[n];
Vector=Cargar_Vector(Vector);
mivector_1.setcuadrado();
System.out.println ("Cuadrado= "+mivector_1.getcuadrado());
System.out.println("______________________________________________");
}
}
public static int Lectura_datos(String msg)throws java.io.IOException{
String cadena;
int dato;
InputStreamReader entrada= new InputStreamReader(System.in);
BufferedReader recibe= new BufferedReader(entrada);
while(true){
System.out.print(msg);
cadena=recibe.readLine();
try{
dato=Integer.parseInt(cadena);
if(dato<0){
System.out.println ("Introduce numeros positivos");
continue;
}
if (dato==0){
System.exit(0);
}
break;
}catch (NumberFormatException e){
System.out.println ("Dato invalido");
}
}
return dato;
}
public static int[] Cargar_Vector(int xVector[]) throws java.io.IOException {
for(int i=0; i<xVector.length; i++){
xVector=Lectura_datos("Introduce Dato en Posición Vector["+(i+1)+"]: ");
}
return xVector;
}
}
me aparece el siguiente error:
Exception in thread "main" java.lang.NullPointerException
at Vector_1.setcuadrado(Vector_1.java:12)
Alguien que me ayude o oriente se lo agradecere bastante.
at EjecutaVector_1.main(Vector_1.java:30)
import java.io.*;
class Vector_1 {
int i=0;
int n;
int xVector[];
int Vector[]=new int [n];
double cuadrado;
void setVector(int Vector)throws java.io.IOException{
Vector=Vector;
}
void setcuadrado()throws java.io.IOException{
cuadrado=xVector*xVector;
}
double getcuadrado()throws java.io.IOException{
return cuadrado;
}
}
class EjecutaVector_1{
public static void main(String args [])throws java.io.IOException{
System.out.println("Programa 1 de vectores_metodos.");
System.out.println("Para salir teclee 999999");
System.out.println("______________________________________________");
while(true){
int n;
Vector_1 mivector_1=new Vector_1();
n=Lectura_datos("Cuantos elementos empleara: ");
int Vector[]=new int[n];
Vector=Cargar_Vector(Vector);
mivector_1.setcuadrado();
System.out.println ("Cuadrado= "+mivector_1.getcuadrado());
System.out.println("______________________________________________");
}
}
public static int Lectura_datos(String msg)throws java.io.IOException{
String cadena;
int dato;
InputStreamReader entrada= new InputStreamReader(System.in);
BufferedReader recibe= new BufferedReader(entrada);
while(true){
System.out.print(msg);
cadena=recibe.readLine();
try{
dato=Integer.parseInt(cadena);
if(dato<0){
System.out.println ("Introduce numeros positivos");
continue;
}
if (dato==0){
System.exit(0);
}
break;
}catch (NumberFormatException e){
System.out.println ("Dato invalido");
}
}
return dato;
}
public static int[] Cargar_Vector(int xVector[]) throws java.io.IOException {
for(int i=0; i<xVector.length; i++){
xVector=Lectura_datos("Introduce Dato en Posición Vector["+(i+1)+"]: ");
}
return xVector;
}
}
me aparece el siguiente error:
Exception in thread "main" java.lang.NullPointerException
at Vector_1.setcuadrado(Vector_1.java:12)
Alguien que me ayude o oriente se lo agradecere bastante.
at EjecutaVector_1.main(Vector_1.java:30)