Why can't I use debugger?
I get this error after trying to debug with a breakpoint in the main method of "Principal" class:
ClassNotFoundException(Throwable). <init>(String, Throwable) line: 286
Source not found.
When I run my programm i get this error:
Exception in thread "main" java.lang.NullPointerException
at Ejercicio01.Indice.ordenar(Principal.java:144)
at Ejercicio01.Indice.<init>(Principal.java:122)
at Ejercicio01.Principal.main(Principal.java:64)
I run the debugger, to check why i get these error, then the debugger error, at first time I press "f5" keystroke
Code java:
import java.util.Vector;
public class Principal {
public static void main(String[] args) throws ClassNotFoundException{
Vector<Producto> v = new Vector<Producto>();
/**BREAKPOINT HERE*/
Producto p1 = new Producto();
Producto p2 = new Producto();
Producto p3 = new Producto();
Producto p4 = new Producto();
Producto p5 = new Producto();
/******************************************/
/** Se introducen los valores: */
/******************************************/
p1.setNombre("Gorro");
p1.setPrecio(10);
p1.setReferencia(1);
p2.setNombre("Bañador");
p2.setPrecio(40);
p2.setReferencia(2);
p3.setNombre("Gafas");
p3.setPrecio(20);
p3.setReferencia(3);
p4.setNombre("Aletas");
p4.setPrecio(30);
p4.setReferencia(4);
p5.setNombre("Toalla");
p5.setPrecio(10);
p5.setReferencia(5);
/******************************************/
//Se añaden los productos
v.add(p1);
v.add(p2);
v.add(p3);
v.add(p4);
v.add(p5);
//Instanciación de un Indice con criterio de ordenación "referencia"
Indice indice = new Indice(v, "referencia");
//Comprobación de los productos albergados
for(int i=0; i<0 ;i++)
System.out.println(indice.getProductos().elementAt(i).getNombre());
}
}
class Producto {
private String nombre;
private float precio;
private int referencia;
//Constructor valores vacíos.
Producto(){
this.nombre = "";
this.precio = 0;
this.referencia = 0;
}
//Constructor valores a conveniencia.
Producto(String nombre, float precio, int referencia){
this.nombre = nombre;
this.precio = precio;
this.referencia = referencia;
}
//Getters:
public String getNombre(){
return this.nombre;
}
public float getPrecio(){
return this.precio;
}
public int getReferencia(){
return this.referencia;
}
//Setters:
public void setNombre(String nombre){
this.nombre = nombre;
}
public void setPrecio(float precio){
this.precio = precio;
}
public void setReferencia(int referencia){
this.referencia = referencia;
}
}
class Indice {
private Vector<Producto> productos;
private String criterio;
Indice(Vector<Producto> productos, String criterio){
this.criterio = criterio;
//Se ordena según el criterio.
ordenar(criterio);
}
public Vector<Producto> getProductos(){
return this.productos;
}
public String getCriterio(){
return criterio;
}
public void ordenar(String criterio){
//Ordenar por precio
if(criterio.equals("precio")){
for(int i=0; i<this.productos.size(); i++){
for(int j=0; i<this.productos.size(); j++){
if(this.productos.elementAt(i).getPrecio() < this.productos.elementAt(j).getPrecio())
this.productos.set(i, this.productos.elementAt(j));
}
}
}
//Ordenar por referencia
else if(criterio.equals("referencia")){
for(int i=0; i<this.productos.size(); i++){
for(int j=0; i<this.productos.size(); j++){
if(this.productos.elementAt(i).getPrecio() < this.productos.elementAt(j).getPrecio())
this.productos.set(i, this.productos.elementAt(j));
}
}
}
else
System.err.println("No se introdujo un criterio de ordenación válido.");
}
public void insertar(Producto nuevo){
//Primero se inserta el nuevo producto
productos.addElement(nuevo);
//Ahora se vuelve a ordenar la lista
ordenar(this.criterio);
}
//Devuelve un vector con todos los productos que contengan el nombre pasado como argumento
public Vector<Producto> buscar(String nombre){
Vector<Producto> v = new Vector<Producto>();
//Se recorre todo el vector de productos en busca de los que tengan el nombre pasado como argumento.
for(int i = 0; i<productos.size() ;i++)
if(this.productos.elementAt(i).getNombre().equals(nombre))
v.add(this.productos.elementAt(i));
return v;
}
}
--- Update ---
I had 3 class in the same package in different files. But i tried making a single file, but still that error.
--- Update ---
When I place a breakpoint at the end of my main method, I am able to debugg, and it goes directly to the line that make the error.
Don't understand why placing breakpoint at the start It occurs the error that i told you before.