Erro no Java!

5 respostas
C

Oi Galera, por favor, me digam qual é este erro e como solucionar, o erro está abaixo, desde já agradeço.

Exception in thread main java.lang.NumberFormatException: For input string: “”

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at Trabalho.<init>(Trabalho.java:58)

at Trabalho.main(Trabalho.java:99)

Obrigado!
Carlos Cesar[color=“blue”][/color]

5 Respostas

_

Você ou alguma ferramenta que você está usando está tentando transformar uma String em um int…

E a String correspondente não é um int…
Pelo erro, a String é vazia “”… e isso não corresponde a um int…

Olhe seu arquivo Trabalho.java na linha 58…

C
Olá, obrigado pela resposta, mas não consegui intender, vou lhe passar o codigo fonte pra vc dar uma olhada, logo abaixo, agradeço de novo!!
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import java.lang.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class Trabalho extends JFrame implements ActionListener 
&#123;

	 
    private JButton &#91;&#93; Botoes   = new JButton&#91;100&#93;;    
    JPanel panel = new JPanel&#40;&#41;; 
    private JLabel tam = new JLabel&#40;&quot;Tamanho do Vetor&quot;&#41;;//Label
    private JPanel grid = new JPanel&#40;&#41;;
    private JButton novo = new JButton&#40;&quot;Criar Vetor&quot;&#41;;//
    private JTextField Tl = new JTextField&#40;&#41;;
    private int tamvetor;
    Container cp;
    private Random rnd;

        
    public Trabalho&#40;&#41; &#123;
        setSize&#40;500,400&#41;;
        setTitle&#40;&quot;Trabalho de Pesquisa e Ordenação&quot;&#41;;
        centerJFrame&#40;this&#41;;
        setDefaultCloseOperation&#40;JFrame.EXIT_ON_CLOSE&#41;;
        setResizable&#40;false&#41;;
        
        cp = getContentPane&#40;&#41;;
        cp.setLayout&#40;null&#41;;
        
        
        
        grid.setBounds&#40;3, 53, 497, 327&#41;;
        grid.setLayout&#40;new GridLayout&#40;10, 10, 2, 2&#41;&#41;;
        
        novo.setBounds&#40;380, 2, 100, 20&#41;;
        novo.setFocusable&#40;false&#41;;
        novo.addActionListener&#40;this&#41;;
        //Label
        tam.setSize&#40;150,25&#41;;
        tam.setLocation&#40;1,2&#41;;
        //Tl - Tamanho do Vetor 
        Tl.setSize&#40;50,25&#41;;
        Tl.setLocation&#40;3,25&#41;;
        //String para inteiro
        String teste = Tl.getText&#40;&#41;;
        tamvetor = Integer.parseInt&#40;teste&#41;;
        
        cp.add&#40;novo&#41;;
        cp.add&#40;tam&#41;;
        cp.add&#40;Tl&#41;;
        CriaVetor&#40;Botoes,tamvetor&#41;;
        preencheVetor&#40;Botoes,tamvetor&#41;;
    &#125; 
    
     private void preencheVetor&#40;JButton&#91;&#93; Vetor, int tamvetor&#41; &#123;
    	String teste;
    	rnd = new Random&#40;&#41;;
    	teste = String.valueOf&#40;rnd&#41;;
        for &#40;int i=0;i&lt;tamvetor;i++&#41;&#123;
        	Vetor&#91;i&#93;.setText&#40;teste&#41;;
        &#125;
        	
        
    &#125;
     
    private void CriaVetor&#40;JButton&#91;&#93; Botoes,int tamvetor&#41;&#123;
        
        for&#40;int i=0;i&lt;tamvetor;i++&#41; &#123;
            Botoes&#91;i&#93; = new JButton&#40;&quot;&quot;&#41;;
            Botoes&#91;i&#93;.addActionListener&#40;this&#41;;
            Botoes&#91;i&#93;.setFocusable&#40;true&#41;;
            grid.add&#40;Botoes&#91;i&#93;&#41;;
          &#125;
    &#125;
        
    
    public void actionPerformed&#40;ActionEvent e&#41; &#123;
    &#125;

    private void centerJFrame&#40;JFrame frame&#41; &#123;
        Dimension paneSize      = frame.getSize&#40;&#41;;
        Dimension screenSize    = frame.getToolkit&#40;&#41;.getScreenSize&#40;&#41;;
        frame.setLocation&#40; &#40;screenSize.width - paneSize.width&#41; / 2, &#40;screenSize.height - paneSize.height&#41; / 2&#41;;
    &#125;
    
   	public static void main&#40;String&#91;&#93; args&#41; &#123;
           Trabalho app = new Trabalho&#40;&#41;;
           app.show&#40;&#41;;
    &#125;
	
&#125;
OBRIGADO CARLOS CESAR
P
//String para inteiro
String teste = Tl.getText&#40;&#41;;
tamvetor = Integer.parseInt&#40;teste&#41;;  // --&gt; O problema esta aqui!

o getText não ta pegando nada…estranho isso estar no contrutor da classe…da uma revisada total nesse codigo aih…

C

Obrigado, jah consegui arrumar, agradeço a todos!!!

C

Por favor, compartilhe o código com a comunidade :joia:

Criado 20 de fevereiro de 2007
Ultima resposta 22 de fev. de 2007
Respostas 5
Participantes 4