Erro Eclipse Java

11 respostas
javaprogramação
M

Bom, sou iniciante na programação e vinha fazendo um código em meu tempo livre. Mas ao concluir, percebi que o mesmo rodava mas não inicializava um botão configurado! Vou explicar melhor:

Capturar

Tudo está dentro dos conformes, roda sem erro. Mas ao clicar no botão de Faturamento Médio Anual dá o erro que postei abaixo.

11 Respostas

M

Ao clicar no “botão Faturamento Médio Anual” eu recebo este seguinte erro:

Exception in thread AWT-EventQueue-0 java.lang.NumberFormatException: empty String

at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)

at sun.misc.FloatingDecimal.parseDouble(Unknown Source)

at java.lang.Double.parseDouble(Unknown Source)

at PRINCIIPPAAAL$6.actionPerformed(PRINCIIPPAAAL.java:197)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEventImpl(Unknown Source)

at java.awt.EventQueue.access$500(Unknown Source)

at java.awt.EventQueue$3.run(Unknown Source)

at java.awt.EventQueue$3.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.awt.EventQueue$4.run(Unknown Source)

at java.awt.EventQueue$4.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)
R

Estás tentando converter uma String vazia para double.

M

Vazia? Pelo que observei atribui valores a todas elas. Vou colocar o código aqui:

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JCheckBox;

import javax.swing.JRadioButton;

import javax.swing.JTextField;

import javax.swing.JTextArea;

import javax.swing.JTextPane;

import javax.swing.JSeparator;

import javax.crypto.SealedObject;

import javax.swing.JButton;

import javax.swing.JToggleButton;

import java.beans.PropertyChangeListener;

import java.util.Set;

import java.util.regex.MatchResult;

import java.beans.PropertyChangeEvent;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.Color;

import java.awt.SystemColor;

import javax.swing.JPasswordField;

import javax.swing.JLabel;

import javax.swing.SwingConstants;

import javax.swing.border.TitledBorder;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.TitlePaneLayout;

import javax.swing.JEditorPane;

import javax.swing.JTable;

public class ProjetoP2 {

private JFrame frame;
private JTextField text01;
private JTextField text2;
private JTextField text3;
private JTextField text4;
private JTextField text5;
private JTextField textm;
private JTextField textc;
private JTextField textt;
private JTextField textp;
private JTextField textHh;

/**
 * Launch the application.
 */
public static void main(String[] args) {
	EventQueue.invokeLater(new Runnable() {
		public void run() {
			try {
				ProjetoP2 window = new ProjetoP2();
				window.frame.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	});
}

/**
 * Create the application.
 */
public ProjetoP2() {
	initialize();
	text01.setVisible(false);
	text2.setVisible(false);
	text3.setVisible(false);
	text4.setVisible(false);
	text5.setVisible(false);
	
	
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
	frame = new JFrame();
	frame.getContentPane().addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			
		}
	});
	frame.setResizable(false);
	frame.setBounds(100, 100, 370, 430);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.getContentPane().setLayout(null);
	
	text01 = new JTextField();
	text01.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent arg0) {
		}
	});
	text01.setBackground(SystemColor.textHighlightText);
	text01.setBounds(11, 76, 123, 20);
	frame.getContentPane().add(text01);
	text01.setColumns(10);
	
	text2 = new JTextField();
	text2.setBackground(SystemColor.textHighlightText);
	text2.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
		}
	});
	text2.setColumns(10);
	text2.setBounds(11, 107, 123, 20);
	frame.getContentPane().add(text2);
	
	text3 = new JTextField();
	text3.setColumns(10);
	text3.setBounds(11, 138, 123, 20);
	frame.getContentPane().add(text3);
	
	text4 = new JTextField();
	text4.setColumns(10);
	text4.setBounds(11, 175, 123, 20);
	frame.getContentPane().add(text4);
	
	text5 = new JTextField();
	text5.setColumns(10);
	text5.setBounds(11, 208, 123, 20);
	frame.getContentPane().add(text5);
	
	textm = new JTextField();
	textm.setEnabled(false);
	textm.setBounds(202, 239, 130, 20);
	frame.getContentPane().add(textm);
	textm.setColumns(10);
	
	textc = new JTextField();
	textc.setColumns(10);
	textc.setBounds(11, 271, 107, 20);
	frame.getContentPane().add(textc);
	
	textt = new JTextField();
	textt.setColumns(10);
	textt.setBounds(11, 302, 107, 20);
	frame.getContentPane().add(textt);
	
	textp = new JTextField();
	textp.setEnabled(false);
	textp.setColumns(10);
	textp.setBounds(146, 369, 155, 20);
	frame.getContentPane().add(textp);
	
	JButton botao = new JButton("Produtividade");
	botao.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			String lprodutiv = textm.getText() + textHh.getText();
			
			if ((textm.getText() != "") & (textHh.getText() != "")){
				double produto = (Double.parseDouble(textm.getText()) / Double.parseDouble(textHh.getText()));
				textp.setText(String.valueOf(produto));
			}
		}
	});
	botao.setBounds(6, 368, 130, 23);
	frame.getContentPane().add(botao);
	
	textHh = new JTextField();
	textHh.setEnabled(false);
	textHh.setColumns(10);
	textHh.setBounds(11, 337, 107, 20);
	frame.getContentPane().add(textHh);
	
	JButton btnNewButton = new JButton("Faturamento Anual M\u00E9dio");
	btnNewButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			String novovalor = text01.getText() + text2.getText() + text3.getText() + text4.getText() + text5.getText();
			
			if (text01.getText() != "0") {
				double soma = Double.parseDouble(text01.getText());
				textm.setText(String.valueOf(soma));
				
			}if ((text2.getText() != "0") & (text01.getText() != "0")){
				double soma = Double.parseDouble(text01.getText() + text2.getText());
				textm.setText(String.valueOf(soma/2));
				
			}if ((text3.getText() != "0") & (text2.getText() != "0") & (text01.getText() != "0")) {
				double soma = Double.parseDouble(text01.getText()+text2.getText()+text3.getText());
				textm.setText(String.valueOf(soma/3));
				
			}if ((text4.getText() != "0") & (text3.getText() != "0") & (text2.getText() != "0") & (text01.getText() != "0")) {
				double soma = Double.parseDouble(text01.getText()+text2.getText()+text3.getText()+text4.getText());
				textm.setText(String.valueOf(soma/4));
				
			}if ((text5.getText() != "0") & (text4.getText() != "0") & (text3.getText() != "0") & (text2.getText() != "0") & (text01.getText() != "0")) {
				double soma = Double.parseDouble(text01.getText()+text2.getText()+text3.getText()+text4.getText()+text5.getText());
				textm.setText(String.valueOf(soma/5));
			}
				
		}
	});
	btnNewButton.setBounds(8, 238, 184, 23);
	frame.getContentPane().add(btnNewButton);
	
	JButton btnContribuioHh = new JButton("Contribui\u00E7\u00E3o Hh");
	btnContribuioHh.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			
			
			String lestat = textc.getText() + textt.getText() + textHh.getText();
			
			if ((textc.getText() != "") & (textt.getText() != "")){
				double produto = (Double.parseDouble(textc.getText()) * Double.parseDouble(textt.getText()))*12;
				textHh.setText(String.valueOf(produto));
			}
		}
	});
	btnContribuioHh.setBounds(128, 333, 130, 23);
	frame.getContentPane().add(btnContribuioHh);
	
	JLabel lblNewLabel = new JLabel("Quantos anos de faturamento voc\u00EA quer calcular?");
	lblNewLabel.setBounds(10, 24, 291, 14);
	frame.getContentPane().add(lblNewLabel);
	
	JLabel fatu1 = new JLabel("Faturamento Anual 1");
	fatu1.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
		
		}
	});
	fatu1.setBounds(146, 75, 138, 14);
	frame.getContentPane().add(fatu1);
	
	JLabel fat2 = new JLabel("Faturamento Anual 2");
	fat2.setBounds(146, 110, 138, 14);
	frame.getContentPane().add(fat2);
	
	JLabel fat3 = new JLabel("Faturamento Anual 3");
	fat3.setBounds(146, 141, 138, 14);
	frame.getContentPane().add(fat3);
	
	JLabel fat4 = new JLabel("Faturamento Anual 4");
	fat4.setBounds(146, 178, 138, 14);
	frame.getContentPane().add(fat4);
	
	JLabel fat5 = new JLabel("Faturamento Anual 5");
	fat5.setBounds(146, 211, 138, 14);
	frame.getContentPane().add(fat5);
	
	JLabel lblNewLabel_2 = new JLabel("Colaboradores");
	lblNewLabel_2.setBounds(128, 274, 97, 14);
	frame.getContentPane().add(lblNewLabel_2);
	
	JLabel lblNewLabel_3 = new JLabel("h/m\u00EAs M\u00E9dioTrabalhados");
	lblNewLabel_3.setBounds(128, 305, 173, 14);
	frame.getContentPane().add(lblNewLabel_3);
	
	JRadioButton rdbt1 = new JRadioButton("1 Ano");
	rdbt1.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			if (rdbt1.isSelected()) {
				text01.setVisible(true);
				

		}   else {
				text01.setVisible(false);
		}
		}
	});
	rdbt1.setBounds(6, 45, 68, 23);
	frame.getContentPane().add(rdbt1);
	
	JRadioButton rdbt2 = new JRadioButton("2 Anos");
	rdbt2.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			
			if (rdbt2.isSelected()) {
				text2.setVisible(true);
				text01.setVisible(true);
				

		}   else {
				text2.setVisible(false);
				text01.setVisible(false);
		}
		}
	});
	rdbt2.setBounds(76, 45, 68, 23);
	frame.getContentPane().add(rdbt2);
	
	JRadioButton rdbt3 = new JRadioButton("3 Anos");
	rdbt3.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			if (rdbt3.isSelected()) {
				text3.setVisible(true);
				text2.setVisible(true);
				text01.setVisible(true);
				

		}   else {
				text3.setVisible(false);
				text2.setVisible(false);
				text01.setVisible(false);
		}
		}
	});
	rdbt3.setBounds(146, 45, 68, 23);
	frame.getContentPane().add(rdbt3);
	
	JRadioButton rdbt4 = new JRadioButton("4 Anos");
	rdbt4.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			if (rdbt4.isSelected()) {
				text4.setVisible(true);
				text3.setVisible(true);
				text2.setVisible(true);
				text01.setVisible(true);
				

		}   else {
				text4.setVisible(false);
				text3.setVisible(false);
				text2.setVisible(false);
				text01.setVisible(false);
		}
		}
	});
	rdbt4.setBounds(216, 45, 68, 23);
	frame.getContentPane().add(rdbt4);
	
	JRadioButton rdbt5 = new JRadioButton("5 Anos");
	rdbt5.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			if (rdbt5.isSelected()) {
				text5.setVisible(true);
				text4.setVisible(true);
				text3.setVisible(true);
				text2.setVisible(true);
				text01.setVisible(true);
				

		}   else {
				text5.setVisible(false);
				text4.setVisible(false);
				text3.setVisible(false);
				text2.setVisible(false);
				text01.setVisible(false);
		}
		}
	});
	rdbt5.setBounds(286, 45, 68, 23);
	frame.getContentPane().add(rdbt5);
}

}

R

SIM VAZIA
N importa o código se nada foi digitado no campo.

R

N é assim q se compara strings.
String é uma classe, deve usar métodos para isso, use isEmpty ou equals

M
if (text01.getText() isEmpty 0) {

double soma = Double.parseDouble(text01.getText());

textm.setText(String.valueOf(soma));

assim? Não sei como funciona o is Empty

F

isEmpty é um método boolean.
O que um método boolean retorna? true ou false || verdadeiro ou falso

Certo, o que isEmpty quer dizer? “éVazio” ou “estáVazio”

Agora, um método éVazio que retorna verdadeiro ou falso parece dificil de usar?

if (qualquerString.isEmpty()) = se a string for vazia


Dentro dos métodos da classe string, ainda temos o equals e o equalsIgnoreCase

equals pode ser traduzido como igual
IgnoreCase é para ignorar o case sensitive

como usar?

if (suaString.equalsIgnoreCase(“outra String pra comparar se é igual a suaString”)

Faltou atenção aí, hein.

F

Errado!

Você não está colocando o . pra chamar o método isEmpty na string text01.getText()

Como disse acima, o isEmpty é pra ver se é vazia, e não para comparar. Logo, não faz sentido o seu “0”.

M

Este método pode ser usar por JTextFild? Quado atribui o seguinte código:

177 - if (text5.isEmpty() {

178 - double soma = Double.parseDouble(text01.getText());

179 - textm.setText(String.valueOf(soma));

ele diz que: Syntax error, insert ") Statment to complete IfStatment
na linha 177

F

Sempre leia o erro.

É erro de SINTAXE. Você não fechou um parente, erro básico.

Cara, revise seu código antes de postar! Veja sempre os (),[] e {} que usa.

E pra finalizar: tá errado. O isEmpty é um método que verifica se STRINGS estão vazias. Um JTextField NÃO É UMA STRING, por isso você usa nomedotextfield.getText().isEmpty().

O getText() te retorna a String que inputaram no textField.

M

Entendi!
Sou novato nesse ramo de automação, verifiquei no código e nele está com os parênteses. Apenas deixei de copiar aqui, erro de “cópia”.

Vou tentando aqui, e aprendendo aos poucos.
Obrigado pela ajuda

Criado 29 de novembro de 2018
Ultima resposta 29 de nov. de 2018
Respostas 11
Participantes 3