Buscar
Social
Ofertas laborales ES

Foro sobre Java SE > Error Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

Error en un programa de netbeans:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at Control.FormularioListaTablas.CboUtActionPerformed(FormularioListaTablas.java:221)
at Control.FormularioListaTablas.access$700(FormularioListaTablas.java:5)
at Control.FormularioListaTablas$8.actionPerformed(FormularioListaTablas.java:148)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1332)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
at javax.swing.DefaultComboBoxModel.addElement(DefaultComboBoxModel.java:131)
at javax.swing.JComboBox.addItem(JComboBox.java:716)
at Control.FormularioListaTablas.(FormularioListaTablas.java:18)
at Control.FormularioListaTablas$11.run(FormularioListaTablas.java:320)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Aca dejo el codigo del programa creado:

import javax.swing.table.DefaultTableModel;
public class FormularioListaTablas extends javax.swing.JFrame {
DefaultTableModel DatosTab;
String Datoss[][] = {};
String TituloTab[] = {"Nombre", "Prod", "Cant","Val Vta","TPagar"};
public FormularioListaTablas() {
initComponents();
DatosTab = new DefaultTableModel(Datoss, TituloTab);
TblVta.setModel(DatosTab);

CboPro.addItem("Cocina");
CboPro.addItem("Tecnologia");
CboPro.addItem("Hogar");
CboPro.addItem("Decoracion");
CboUt.addItem("10%");
CboUt.addItem("10%");
CboUt.addItem("10%");
CboUt.addItem("10%");
CboUt.setSelectedIndex(0);
TxtNom.requestFocus();
CboPro.setEnabled(false);
TxtCan.setEditable(false);
TxtUni.setEditable(false);
CboUt.setEnabled(false);
BtnAgre.setEnabled(false);
TxtNom.requestFocus();

}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
LblVt = new javax.swing.JLabel();
LblTo = new javax.swing.JLabel();
TxtNom = new javax.swing.JTextField();
TxtCan = new javax.swing.JTextField();
TxtUni = new javax.swing.JTextField();
CboPro = new javax.swing.JComboBox();
CboUt = new javax.swing.JComboBox();
BtnAgre = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
TblVta = new javax.swing.JTable();
jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

jPanel1.setBackground(new java.awt.Color(51, 255, 255));
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel1.setText("Venta De Productos");
jPanel2.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 10, -1, -1));

jLabel2.setText("Nombre");
jPanel2.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 40, -1, -1));

jLabel3.setText("Producto");
jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 70, -1, -1));

jLabel4.setText("Cantidad");
jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 100, -1, -1));

jLabel5.setText("Valor Unitario");
jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 130, -1, -1));

jLabel6.setText("% Utilidad");
jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 160, -1, -1));

jLabel7.setText("Valor Venta");
jPanel2.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 190, -1, -1));

jLabel8.setText("Total Pagar");
jPanel2.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 220, -1, -1));

LblVt.setText("???");
jPanel2.add(LblVt, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 190, 40, -1));

LblTo.setText("???");
jPanel2.add(LblTo, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 220, 40, -1));

TxtNom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TxtNomActionPerformed(evt);
}
});
TxtNom.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
TxtNomKeyTyped(evt);
}
});
jPanel2.add(TxtNom, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 40, 90, -1));

TxtCan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TxtCanActionPerformed(evt);
}
});
TxtCan.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
TxtCanKeyTyped(evt);
}
});
jPanel2.add(TxtCan, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 100, 40, -1));

TxtUni.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TxtUniActionPerformed(evt);
}
});
TxtUni.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
TxtUniKeyTyped(evt);
}
});
jPanel2.add(TxtUni, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 130, 40, 30));

CboPro.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CboProActionPerformed(evt);
}
});
jPanel2.add(CboPro, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 70, 90, -1));

CboUt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CboUtActionPerformed(evt);
}
});
jPanel2.add(CboUt, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 160, 90, -1));

BtnAgre.setForeground(new java.awt.Color(153, 0, 0));
BtnAgre.setText("Agregar Datos De La venta A La Tabla");
BtnAgre.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnAgreActionPerformed(evt);
}
});
jPanel2.add(BtnAgre, new org.netbeans.lib.awtextra.AbsoluteConstraints(257, 150, 350, -1));

TblVta.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(TblVta);

jPanel2.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 40, 350, 90));

jButton3.setForeground(new java.awt.Color(153, 0, 0));
jButton3.setText("Regreso Al Menu");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jPanel2.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 180, 350, -1));

jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 630, 250));

getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 670, 280));

pack();
}// </editor-fold>

private void BtnAgreActionPerformed(java.awt.event.ActionEvent evt) {

String Nom = TxtNom.getText();
String Pro = CboPro.getSelectedItem().toString();
String Can = TxtCan.getText();
String Tota=LblTo.getText();
String Vt=LblVt.getText();

String LLenarDatos[] = {Nom, Pro, Can,Vt,Tota};

DatosTab.addRow(LLenarDatos);
}

private void TxtNomActionPerformed(java.awt.event.ActionEvent evt) {
CboPro.setEnabled(true);
CboPro.requestFocus();
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
new FormularioMenu().setVisible(true);
}

private void TxtUniActionPerformed(java.awt.event.ActionEvent evt) {
CboUt.setEnabled(true);
CboUt.requestFocus();
}

private void CboUtActionPerformed(java.awt.event.ActionEvent evt) {
int Unita = Integer.parseInt(TxtUni.getText());
int Can = Integer.parseInt(TxtCan.getText());
int Tot;
int VV;
if (CboUt.getSelectedIndex() == 0) {

VV = Unita + (Unita * 10 / 100);
Tot = VV * Can;
LblVt.setText(String.valueOf(VV));
LblTo.setText(String.valueOf(Tot));
BtnAgre.setEnabled(true);
} else if (CboUt.getSelectedIndex() == 1) {
VV = Unita + (Unita * 20 / 100);
Tot = VV * Can;
LblVt.setText(String.valueOf(VV));
LblTo.setText(String.valueOf(Tot));
BtnAgre.setEnabled(true);
} else if (CboUt.getSelectedIndex() == 2) {
VV = Unita + (Unita * 30 / 100);
Tot = VV * Can;
LblVt.setText(String.valueOf(VV));
LblTo.setText(String.valueOf(Tot));
BtnAgre.setEnabled(true);
} else {
VV = Unita + (Unita * 40 / 100);
Tot = VV * Can;
LblVt.setText(String.valueOf(VV));
LblTo.setText(String.valueOf(Tot));
BtnAgre.setEnabled(true);
}
}

private void CboProActionPerformed(java.awt.event.ActionEvent evt) {

TxtCan.setEditable(true);
TxtCan.requestFocus();
}

private void TxtCanActionPerformed(java.awt.event.ActionEvent evt) {

TxtUni.setEditable(true);
TxtUni.requestFocus();

}

private void TxtNomKeyTyped(java.awt.event.KeyEvent evt) {
char zz = evt.getKeyChar();
if((zz < 'a' || zz > 'z') && (zz<'A' || zz>'Z'))
{
evt.consume();
}
}

private void TxtCanKeyTyped(java.awt.event.KeyEvent evt) {
char Num = evt.getKeyChar();
if (Num < '0' || Num > '9') {

evt.consume();
}
}

private void TxtUniKeyTyped(java.awt.event.KeyEvent evt) {
char Num = evt.getKeyChar();
if (Num < '0' || Num > '9') {

evt.consume();
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FormularioListaTablas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FormularioListaTablas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FormularioListaTablas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FormularioListaTablas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FormularioListaTablas().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton BtnAgre;
private javax.swing.JComboBox CboPro;
private javax.swing.JComboBox CboUt;
private javax.swing.JLabel LblTo;
private javax.swing.JLabel LblVt;
private javax.swing.JTable TblVta;
private javax.swing.JTextField TxtCan;
private javax.swing.JTextField TxtNom;
private javax.swing.JTextField TxtUni;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
}

octubre 23, 2015 | Unregistered CommenterGold

Estas intentando convertir una cadena vacía a un tipo de dato entero.
Fijate en las siguientes líneas:

int Unita = Integer.parseInt(TxtUni.getText());
int Can = Integer.parseInt(TxtCan.getText());

octubre 23, 2015 | Unregistered CommenterChavitoDel8

¿Alguien me puede dar una solución para el este mismo problema?
No sé como hacer para poder convertir una cadena vacía a un tipo de dato entero.

septiembre 21, 2019 | Unregistered CommenterMe

TENGO EL MISMO PROBLEMA EN MI CODIGO...INVESTIGUE EN OTRAS PARTES Y ES ERROR AL INTENTAR PARSEAR LINEAS, pero sinceramente no entiendo porque

package metodo_burbuja;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Burbuja extends JFrame implements ActionListener {
JLabel lblelementos,lblproceso;
JTextField txtelementos,txtproceso;
JButton btnaceptar,btnguardar,btnmostrar;
JTextArea arearespuesta;
JPanel contenedor_sur;
int arreglo[];
int nElementos=0;
int aux=0,a=0;
public Burbuja(){
//crear componentes
lblelementos=new JLabel("Ingresa Numero de Procesos a Realizar");
lblelementos.setForeground(new java.awt.Color(0,0,0));//color de fuente
lblelementos.setFont(new Font("Bahnschrift SemiBold",Font.BOLD,16));//formato de fuente

lblproceso=new JLabel("Digita el "+ "proceso");
lblproceso.setForeground(new java.awt.Color(0,0,0));//color de fuente
lblproceso.setFont(new Font("Bahnschrift SemiBold",Font.BOLD,16));//formato de fuente

txtelementos=new JTextField(5);
txtelementos.setBackground(new java.awt.Color(225,225,225));//color de fondo
txtelementos.setForeground(new java.awt.Color(48,172,194));//color de fuente
txtelementos.setFont(new Font("Bahnschrift SemiBold",Font.BOLD,16));//formato de fuente

txtproceso=new JTextField(5);
txtproceso.setBackground(new java.awt.Color(225,225,225));//color de fondo
txtproceso.setForeground(new java.awt.Color(48,172,194));//color de fuente
txtproceso.setFont(new Font("Bahnschrift SemiBold",Font.BOLD,16));//formato de fuente

btnaceptar= new JButton("Aceptar");
btnaceptar.addActionListener(this);
btnaceptar.setBackground(new java.awt.Color(4,29,39));//color de fondo
btnaceptar.setForeground(new java.awt.Color(109,218,218));//color de fuente
btnaceptar.setFont(new Font("Bahnschrift SemiBold",Font.BOLD,14));//formato de fuente

btnguardar= new JButton("Guardar");
btnguardar.addActionListener(this);
btnguardar.setBackground(new java.awt.Color(4,29,39));//color de fondo
btnguardar.setForeground(new java.awt.Color(109,218,218));//color de fuente
btnguardar.setFont(new Font("Bahnschrift SemiBold",Font.BOLD,14));//formato de fuente

btnmostrar= new JButton("Mostrar");
btnmostrar.addActionListener(this);
btnmostrar.setBackground(new java.awt.Color(4,29,39));//color de fondo
btnmostrar.setForeground(new java.awt.Color(109,218,218));//color de fuente
btnmostrar.setFont(new Font("Bahnschrift SemiBold",Font.BOLD,14));//formato de fuente

arearespuesta=new JTextArea(5,40);
arearespuesta.setBackground(new java.awt.Color(225,225,225));//color de fondo
arearespuesta.setForeground(new java.awt.Color(48,172,194));//color de fuente
arearespuesta.setFont(new Font("Bahnschrift SemiBold",Font.BOLD,16));//formato de fuente

contenedor_sur = new JPanel();
contenedor_sur.setLayout(new GridLayout(1,1));

//Contenedor
Container contenedor=getContentPane();
contenedor.setLayout(new FlowLayout());
contenedor.setBackground(new java.awt.Color(34,177,181));

//Elementos del contenedor
contenedor.add(lblelementos);
contenedor.add(txtelementos);
contenedor.add(btnaceptar);
contenedor.add(lblproceso);
contenedor.add(txtproceso);
contenedor.add(btnguardar);
contenedor.add(btnmostrar);
contenedor_sur.add(arearespuesta);

//Propiedades del Constructor
add(contenedor_sur,BorderLayout.SOUTH);
setSize(900,200);
setVisible(true);
setTitle("Metodo Burbuja");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent evt)
{

arreglo = new int[nElementos];

if(evt.getSource()==btnaceptar)
{
nElementos=Integer.parseInt(txtelementos.getText());
}
if(evt.getSource()==btnguardar)
{
for(int i=0;i<nElementos;i++)
{
lblproceso.setText((i+1)+"Digite un proceso: ");
arreglo[i] = Integer.parseInt(txtproceso.getText());
JOptionPane.showMessageDialog(null,"Guardado");
txtproceso.setText("");
}

for(int i=0;i<(nElementos-1);i++)
{
for(int j=0;j<(nElementos-1);j++)
{
if(arreglo[j]>arreglo[j+1]){
aux= arreglo[j];
arreglo[j]= arreglo[j+1];
arreglo[j+1] = aux;

}

}
}
arearespuesta.setText( "\nArregloordenado en forma creciente");
for(int i=0;i<nElementos;i++)
{
arearespuesta.setText(arreglo[i]+" - ");
}
arearespuesta.setText("\nArreglo de Mayor a Menor");
for(int i=(nElementos-1);i>=0;i--)
{
arearespuesta.setText(arreglo[i]+" - ");
}
arearespuesta.setText("");

System.out.println("\nArregloordenado en forma creciente");
for(int i=0;i<nElementos;i++)
{
System.out.print(arreglo[i]+" - ");
}
System.out.println("\nArreglo de Mayor a Menor");
for(int i=(nElementos-1);i>=0;i--)
{
System.out.print(arreglo[i]+" - ");
}
System.out.println("");

}
}

public static void main(String[] args)
{
new Burbuja();
}

}

marzo 24, 2021 | Unregistered CommenterEstrellaTery

Guenas.

Creo que dada la pregunta es mejor no complicar demasiado la respuesta.
Aunque es feote, mete el parsing dentro de un try-catch y si salta asiga un 0 al entero.

Un saludo,
Paposo

abril 3, 2021 | Unregistered CommenterPaposo