Error Exception Null

0

I have this section of code where I need to add those variables to my database, but once I send the data, it tells me the error null

Here I make my connections

package lavanderia.Modelo;
import java.sql.Date;
import java.util.ArrayList;
/**
 *
 * @author Lilium Gal
 */
public class dbRegistro extends ManejadorDB implements dbPersistencia {

java.sql.Date fecha;

@Override
public void insertarRegistroLavado(Object objeto) throws Exception {

    Registro re = new Registro();
    re = (Registro) objeto;

    fecha = new java.sql.Date(re.getFecha_actual().getYear(), 
            re.getFecha_actual().getMonth(), re.getFecha_actual().getDay());

    this.conectar();
    String consulta = "insert into " +"ciclo_lavado(ciclo,id_programa,id_equipo,tipo,fecha) values(?,?,?,?,?)";

    this.sqlConsulta = this.conexion.prepareStatement(consulta);

    this.sqlConsulta.setInt(1, 1);
    this.sqlConsulta.setInt(2, re.getId_programa());
    this.sqlConsulta.setInt(3, re.getId_equipo());
    this.sqlConsulta.setString(4, "Lavado");
    this.sqlConsulta.setDate(5, fecha);

    this.sqlConsulta.executeUpdate();

    this.desconectar();
}

@Override
public void insertarRegistroSecado(Object objeto) throws Exception {

     Registro re = new Registro();
    re = (Registro) objeto;

 fecha = new java.sql.Date(re.getFecha_actual().getYear(), 
            re.getFecha_actual().getMonth(), re.getFecha_actual().getDay());

    this.conectar();
    String consulta = "insert into " +"ciclo_secado(id_programa,id_equipo,tipo,fecha) values(?,?,?,?)";

    this.sqlConsulta = this.conexion.prepareStatement(consulta);


    this.sqlConsulta.setInt(1, re.getId_programa());
    this.sqlConsulta.setInt(2, re.getId_equipo());
    this.sqlConsulta.setString(3, "Secado");
    this.sqlConsulta.setDate(4, fecha);

    this.sqlConsulta.executeUpdate();

    this.desconectar();

}

@Override
public void activarEquipo(Object objeto) throws Exception {

    Registro re = new Registro();
    re = (Registro) objeto;

    this.conectar();
    String consulta = "update equipos set Status = ? where nombre = ?";
    this.sqlConsulta = this.conexion.prepareStatement(consulta);
    this.sqlConsulta.setString(1, "A");
    this.sqlConsulta.setString(2, re.getEquipo());

    this.sqlConsulta.executeUpdate();
    this.desconectar();

}

@Override
public void mantenimiento(Object objeto) throws Exception {
    Registro re = new Registro();
    re = (Registro) objeto;

    this.conectar();
    String consulta = "update equipos set Status = ? where nombre = ? ";
    this.sqlConsulta = this.conexion.prepareStatement(consulta);
    this.sqlConsulta.setString(1, "M");
    this.sqlConsulta.setString(2, re.getEquipo());

    this.sqlConsulta.executeUpdate();

    String consul = "insert into mantenimiento(id_equipo,fecha,motivo) values(?,?,?)";

    this.sqlConsulta = this.conexion.prepareStatement(consul);
    this.sqlConsulta.setInt(1, re.getId_equipo());
    this.sqlConsulta.setDate(2, (Date) re.getFecha_actual());
    this.sqlConsulta.setString(3, re.getMotivo());

    this.sqlConsulta.executeUpdate();
    this.desconectar();


}

@Override
public void actualizarInsumos(Object objeto) throws Exception {
  Registro re = new Registro();
  re = (Registro) objeto;

  this.conectar();
  String consulta = "update insumos set existencia = ? where id_insumo = ?";
  this.sqlConsulta = this.conexion.prepareStatement(consulta);
  this.sqlConsulta.setInt(1,re.getExistencia());
  this.sqlConsulta.setInt(2, re.getId_insumo());
  this.sqlConsulta.executeUpdate();
  this.desconectar();

}

@Override
public ArrayList consultaEquipos( ) throws Exception {

    ArrayList lista = new ArrayList();
    Registro re;
    String consulta = "select * from equipos";
    this.conectar();

    this.sqlConsulta = this.conexion.prepareStatement(consulta);
    this.registros = this.sqlConsulta.executeQuery();

    while(this.registros.next()){
        re = new Registro();

        re.setId_equipo(this.registros.getInt("id_equipo"));
        re.setEquipo(this.registros.getString("nombre"));
        re.setTipo(this.registros.getString("tipo"));
        re.setHrs_trabajadas(this.registros.getInt("hrs_trabajadas"));
        re.setStatus(this.registros.getString("Status"));

        lista.add(re);
    }
    this.desconectar();
    return lista;
}

@Override
public ArrayList consultaLavado() throws Exception {
     ArrayList lista = new ArrayList();
    Registro re;
    String consulta = "select ciclo_lavado.fecha,ciclo_lavado.ciclo,ciclo_lavado.id_programa,programas_lavado.duracion,prendas.nombre,prendas.ubicacion,registro_prendas.cantidad,programas_lavado.ablandador,programas_lavado.alcalino,programas_lavado.blanqueador,programas_lavado.detergente,programas_lavado.neutralizante,programas_lavado.suavizante,equipos.nombre from prendas inner join registro_prendas on prendas.id_prenda = registro_prendas.id_prenda inner join ciclo_lavado on registro_prendas.id_ciclo = ciclo_lavado.id_ciclo inner join equipos on ciclo_lavado.id_equipo = equipos.id_equipo inner join programas_lavado on ciclo_lavado.id_programa = programas_lavado.id_programa";
    this.conectar();

    this.sqlConsulta = this.conexion.prepareStatement(consulta);
    this.registros = this.sqlConsulta.executeQuery();

    while(this.registros.next()){
        re = new Registro();

        re.setFecha_actual(this.registros.getDate("ciclo_lavado.fecha"));
        re.setId_programa(this.registros.getInt("ciclo_lavado.id_programa"));
        re.setDuracion(this.registros.getInt("programas_lavado.duracion"));
        re.setPrenda(this.registros.getString("prendas.nombre"));
        re.setUbicacion(this.registros.getString("prendas.ubicacion"));
        re.setCantidad_prendas(this.registros.getInt("registro_prendas.cantidad"));
        re.setTipo("Quimico");
        re.setAblandador(this.registros.getInt("programas_lavado.ablandador"));
        re.setAlcalino(this.registros.getInt("programas_lavado.alcalino"));
        re.setBlanqueador(this.registros.getInt("programas_lavado.blanqueador"));
        re.setDetergente(this.registros.getInt("programas_lavado.detergente"));
        re.setNeutralizante(this.registros.getInt("programas_lavado.neutralizante"));
        re.setSuavizante(this.registros.getInt("programas_lavado.suavizante"));
        re.setEquipo(this.registros.getString("equipos.nombre"));

        lista.add(re);
    }
    this.desconectar();
    return lista;
}


@Override
public ArrayList consultaSecado () throws Exception{
ArrayList lista = new ArrayList();
    Registro re;
    String consulta = "select ciclo_secado.fecha,ciclo_secado.ciclo,ciclo_secado.id_programa,programas_secado.duracion,prendas.nombre,prendas.ubicacion,registro_prendas.cantidad,programas_secado.gas,equipos.nombre from prendas inner join registro_prendas on prendas.id_prenda = registro_prendas.id_prenda inner join ciclo_secado on registro_prendas.id_ciclo = ciclo_secado.id_ciclo inner join equipos on ciclo_secado.id_equipo = equipos.id_equipo inner join programas_secado on ciclo_secado.id_programa = programas_secado.id_programa";
    this.conectar();

    this.sqlConsulta = this.conexion.prepareStatement(consulta);
    this.registros = this.sqlConsulta.executeQuery();

    while(this.registros.next()){
        re = new Registro();

        re.setFecha_actual(this.registros.getDate("ciclo_secado.fecha"));
        re.setId_programa(this.registros.getInt("ciclo_secado.id_programa"));
        re.setDuracion(this.registros.getInt("programas_secado.duracion"));
        re.setPrenda(this.registros.getString("prendas.nombre"));
        re.setUbicacion(this.registros.getString("prendas.ubicacion"));
        re.setCantidad_prendas(this.registros.getInt("registro_prendas.cantidad"));
        re.setGas(this.registros.getInt("programas_secado.gas"));
        re.setEquipo(this.registros.getString("equipos.nombre"));

        lista.add(re);
    }
    this.desconectar();
    return lista;

}

@Override
public void insertarPrendas(Object objeto) throws Exception{
    Registro re = new Registro();
    re = (Registro) objeto;

    String consulta = "insert into " + "registro_prendas(id_ciclo,tipo,id_prenda,cantidad) values (?,?,?,?)";

    this.sqlConsulta = this.conexion.prepareStatement(consulta);

    this.sqlConsulta.setInt(1, re.getId_ciclo());
    this.sqlConsulta.setString(2, re.getTipo());
    this.sqlConsulta.setInt(2, re.getId_prenda());
    this.sqlConsulta.setInt(3, re.getCantidad_prendas());

    this.sqlConsulta.executeUpdate();
    this.desconectar();

}

@Override
public Object datosSecado(int id) throws Exception{
    Registro re = new Registro();
    this.conectar();

    String consulta = "select duracion,gas from programas_secado where id_programa = ?";
    this.sqlConsulta = this.conexion.prepareStatement(consulta);

    this.sqlConsulta.setInt(1, id);

    this.registros = this.sqlConsulta.executeQuery();

    if(this.registros.next()){
        re.setDuracion(this.registros.getInt("duracion"));
        re.setGas(this.registros.getInt("gas"));

    }

    this.desconectar();
    return re;
}

@Override
public Object datosLavado(int id) throws Exception{
    Registro re = new Registro();
    this.conectar();

    String consulta = "select gas from programas_secado where id_programa = ?";
    this.sqlConsulta = this.conexion.prepareStatement(consulta);

    this.sqlConsulta.setInt(1, id);

    this.registros = this.sqlConsulta.executeQuery();

    if(this.registros.next()){
        re.setGas(this.registros.getInt("gas"));

    }

    this.desconectar();
    return re;
}

@Override
public ArrayList consultaInsumos( ) throws Exception {
    ArrayList lista = new ArrayList();
    Registro re;
    String consulta = "select * from insumos";
    this.conectar();

    this.sqlConsulta = this.conexion.prepareStatement(consulta);
    this.registros = this.sqlConsulta.executeQuery();

    while(this.registros.next()){
        re = new Registro();

        re.setId_insumo(this.registros.getInt("id_insumo"));
        re.setInsumo(this.registros.getString("nombre"));
        re.setTipo(this.registros.getString("tipo"));
        re.setMotivo(this.registros.getString("medida"));
        re.setExistencia(this.registros.getInt("existencia"));

        lista.add(re);
    }
    this.desconectar();
    return lista;

}

@Override
 public ArrayList consultaPrendas() throws Exception{
     ArrayList lista = new ArrayList();
    Registro re;
    String consulta = "select * from prendas";
    this.conectar();

    this.sqlConsulta = this.conexion.prepareStatement(consulta);
    this.registros = this.sqlConsulta.executeQuery();

    while(this.registros.next()){
        re = new Registro();

        re.setId_prenda(this.registros.getInt("id_prenda"));
        re.setPrenda(this.registros.getString("nombre"));
        re.setUbicacion(this.registros.getString("ubicacion"));

        lista.add(re);
    }
    this.desconectar();
    return lista;
 }

 @Override
public ArrayList consultaMantenimientos() throws Exception{
     ArrayList lista = new ArrayList();
    Registro re;
    String consulta = "select equipos.nombre,mantenimiento.fecha,mantenimiento.motivo from equipos inner join mantenimiento on equipos.id_equipo = mantenimiento.id_equipo ";
    this.conectar();

    this.sqlConsulta = this.conexion.prepareStatement(consulta);
    this.registros = this.sqlConsulta.executeQuery();

    while(this.registros.next()){
        re = new Registro();

        re.setEquipo(this.registros.getString("equipos.nombre"));
        re.setFecha_actual(this.registros.getDate("mantenimiento.fecha"));
        re.setMotivo(this.registros.getString("motivo"));

        lista.add(re);
    }
    this.desconectar();
    return lista;
}

}

This is my builder

package lavanderia.Controlador;

import lavanderia.Modelo.*;
import lavanderia.Vista.*;
import lavanderia.Controlador.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author DiegoTrujillo
 */
public class ControladorPrendas implements ActionListener{

private RegistroPrendas vprendas;
private Registro reg;
String tipo;

public ControladorPrendas(RegistroPrendas vprendas,Registro reg,String tipo) {
this.vprendas=vprendas;
this.reg=reg;
this.tipo=tipo;
this.vprendas.btnAgregar.addActionListener(this);
this.vprendas.btnCancelar.addActionListener(this);
this.vprendas.btnLimpiar.addActionListener(this);

}
/**
 * @param args the command line arguments
 */


@Override
public void actionPerformed(ActionEvent e) {

if(e.getSource()==this.vprendas.btnAgregar){
            Registro aux = new Registro();
            dbRegistro db = new dbRegistro();
            try{
                aux.setId_prenda(this.vprendas.jComboBox1.getSelectedIndex()+1);
                aux.setCantidad_prendas(Integer.parseInt(this.vprendas.jTextField1.getText()));
            if(tipo == "Lavado"){
                aux.setId_ciclo(this.idLavado());
                aux.setTipo(tipo);
            }
            else if(tipo=="Secado"){
                aux.setId_ciclo(this.idSecado());
                aux.setTipo(tipo);
            }
                System.out.println(tipo);
                System.out.println(aux.getId_ciclo());
                System.out.println(aux.getId_prenda());
                System.out.println(aux.getCantidad_prendas());
                db.insertarPrendas(aux);
            }catch(Exception e1){
                e1.printStackTrace();
                JOptionPane.showMessageDialog(vprendas, "Surgio un Error " + e1.getMessage());
            }

        }

if(e.getSource()==this.vprendas.btnCancelar){
           Limpiar(); 
            vprendas.setVisible(false); 
        }
if(e.getSource()==this.vprendas.btnLimpiar){
            Limpiar(); 


}
}


public void iniciarVista(){
    this.vprendas.setTitle(" ");
    this.vprendas.setVisible(true);
}

public void Limpiar (){

    this.vprendas.jComboBox1.setSelectedIndex(0);
    this.vprendas.jTextField1.setText("");


}

public int idLavado(){
   int idciclo=0;
   try{

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/lavanderia","root","");
Statement Sent = con.createStatement();
ResultSet rs =Sent.executeQuery("select id_ciclo from ciclo_lavado where id_ciclo = (select max(id_ciclo) from ciclo_lavado)");

while (rs.next ()) {    idciclo = rs.getInt ("id_ciclo"); }

}catch(Exception e){
    JOptionPane.showMessageDialog(null, e.getMessage());
}


return idciclo;

}

public int idSecado(){
   int idciclo=0;
   try{

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/lavanderia","root","");
Statement Sent = con.createStatement();
ResultSet rs = Sent.executeQuery("select id_ciclo from ciclo_secado where id_ciclo = (select max(id_ciclo) from ciclo_secado)");

while (rs.next ()) {     idciclo = rs.getInt ("id_ciclo");    }

}catch(Exception e){
    JOptionPane.showMessageDialog(null, e.getMessage());
}


return idciclo;

} }

    
asked by Lilium Gal 20.04.2017 в 07:48
source

1 answer

3

I think your problem is that in the class insertarPrendas(object) you do not open the connection, while in other methods like datosSecado(int) you do it, look:

public Object datosSecado(int id) throws Exception{
    Registro re = new Registro();
    this.conectar();

Now look at the problematic method:

public void insertarPrendas(Object objeto) throws Exception{
    Registro re = new Registro();
    re = (Registro) objeto;

You need to connect to the BD with this.conectar .

I hope it helps you.

    
answered by 20.04.2017 / 08:49
source