오류

2017-12-18 15 views
0

내가 int로의 JTextField에서 INT 변환 오류하지만 확실하지 오전 어디에 주어진 오류 메시지가오류

실행 그들

package demooo; 

/* 
* GridBagLayoutDemo.java requires no other files. 
*/ 
import java.awt.*; 
import java.awt.event.*; 
import java.sql.*; 
import javax.swing.*; 
import javax.swing.text.DefaultCaret; 

public class Demooo implements ActionListener { 

    final static boolean shouldFill = true; 
    final static boolean shouldWeightX = true; 
    final static boolean RIGHT_TO_LEFT = false; 
    private final static String newline = "\n"; 
    protected JTextField textField; 
    protected JTextArea textArea; 

    public static void addComponentsToPane(Container pane) { 

     if (RIGHT_TO_LEFT) { 

pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
     } 

     /*test for team names */ 
     //String[] teamString = {}; 
     String[] reffString = {"bob", "fred", "jon", "ed", "killa"}; 
     JLabel label; 
     JLabel labelA; 
     JLabel labelH; 
     JLabel labelA2; 
     JLabel labelH2; 
     JTextField text; 
     int g = 0 ; 
     JButton next; 
     JButton button1; 
     JButton button2; 
     JLabel blank; 
     JLabel Head; 
     JComboBox comboH; 
     JComboBox comboA; 
     JComboBox comboReff; 
     String winner; 
     Connection con = null; 
     Statement st = null; 
     ResultSet rs = null; 
     /* int h = Integer.parseInt(homegoal.getText()); 
     int a = Integer.parseInt(awaygoal.getText()); 
     int h2 = Integer.parseInt(homegoal2.getText()); 
     int a2 = Integer.parseInt(awaygoal2.getText());*/ 

     pane.setLayout(new GridBagLayout()); 

     GridBagConstraints c = new GridBagConstraints(); 
     if (shouldFill) { 
      //natural height, maximum width 
      c.fill = GridBagConstraints.HORIZONTAL; 
     } 
     /*select home team*/ 

     labelH = new JLabel("Home Team"); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 0; 
     pane.add(labelH, c); 

     comboH = new JComboBox(/*teamString*/); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 1; 
     c.gridx = 0; 
     c.gridy = 1; 
     pane.add(comboH, c); 
     /* to add blank space between teams*/ 

/*cant get right yet but will try */ 
/*select away team*/ 
     labelA = new JLabel("Away Team"); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 7; 
     c.gridy = 0; 
     pane.add(labelA, c); 

     comboA = new JComboBox(/*teamString*/); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 1; 
     c.gridx = 7; 
     c.gridy = 1; 
     pane.add(comboA, c); 

     /*Team 1 home*/ 
     Head = new JLabel("Home Game Score", SwingConstants.CENTER); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20;  //make this component tall 
     c.weightx = 0.0; 
     c.gridwidth = 3; 
     c.gridx = 0; 
     c.gridy = 3; 
     pane.add(Head, c); 
     Head.setFont(new Font("Serif", Font.BOLD, 18)); 

     /*enter score for home game*/ 
     labelH2 = new JLabel("Home Team Goals"); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 4; 
     pane.add(labelH2, c); 

     JTextField homegoal = new JTextField(2); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 5; 
     pane.add(homegoal, c); 
int h = Integer.parseInt(homegoal.getText()); 
     /*ref section*/ 
     label = new JLabel("Referee"); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 4; 
     pane.add(label, c); 

     comboReff = new JComboBox(reffString); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 1; 
     c.gridx = 3; 
     c.gridy = 5; 
     pane.add(comboReff, c); 

     /*other teams goals*/ 
     label = new JLabel("Away Team Goals"); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 7; 
     c.gridy = 4; 
     pane.add(label, c); 

     JTextField awaygoal = new JTextField(2); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 7; 
     c.gridy = 5; 
     pane.add(awaygoal, c); 
     int a = Integer.parseInt(awaygoal.getText()); 


     /*team 2 is home team*/ 
     Head = new JLabel("Away Game Score", SwingConstants.CENTER); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20;  //make this component tall 
     c.weightx = 0.0; 
     c.gridwidth = 3; 
     c.gridx = 0; 
     c.gridy = 6; 
     pane.add(Head, c); 
     Head.setFont(new Font("Serif", Font.BOLD, 18)); 

     label = new JLabel("Home Team Goals"); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 8; 
     pane.add(label, c); 

     JTextField homegoal2 = new JTextField(2); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 9; 
     pane.add(homegoal2, c); 
     int h2 = Integer.parseInt(homegoal2.getText()); 
     /*reff */ 
     label = new JLabel("Referee"); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 3; 
     c.gridy = 8; 
     pane.add(label, c); 

     comboReff = new JComboBox(reffString); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 1; 
     c.gridx = 3; 
     c.gridy = 9; 
     pane.add(comboReff, c); 


     /*next teams goals*/ 
     label = new JLabel("Away Team Goals"); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 7; 
     c.gridy = 8; 
     pane.add(label, c); 

     JTextField awaygoal2 = new JTextField(2); 
     if (shouldWeightX) { 
      c.weightx = 0.5; 
     } 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 7; 
     c.gridy = 9; 
     pane.add(awaygoal2, c); 

     int a2 = Integer.parseInt(awaygoal2.getText()); 


     /*match set complete*/ 
     button1 = new JButton("The winner of this set of matches was"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20;  //make this component tall 
     c.weightx = 2; 
     c.gridwidth = 8; 
     c.gridx = 0; 
     c.gridy = 10; 
     pane.add(button1, c); 
     button1.addActionListener(new ActionListener() { 

         public void actionPerformed(int g) { 
         if (h>a){ 
         g =+1; 
         }else if (a>h){ 
         g =-1;} 
         else{ 
         g = g;} 
         } 

      @Override 
      public void actionPerformed(ActionEvent ae) { 
       throw new UnsupportedOperationException("Not supported 
yet."); //To change body of generated methods, choose Tools | Templates. 
      } 
     }); 

     if (g > 0){ 
     winner = "home"; 
     } 
     else if (g < 0){ 
     winner = "away";} 
     else { 
     winner = "Draw";} 

     label = new JLabel (winner); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20;  //make this component tall 
     c.weightx = 2; 
     c.gridwidth = 8; 
     c.gridx = 0; 
     c.gridy = 11; 
     pane.add(label, c); 

     button2 = new JButton ("Next Match"); 
     c.fill = GridBagConstraints.HORIZONTAL; 
     c.ipady = 20;  //make this component tall 
     c.weightx = 2; 
     c.gridwidth = 8; 
     c.gridx = 0; 
     c.gridy = 12; 
     pane.add(button2, c); 

     try { 
      con = DriverManager.getConnection("jdbc:derby://localhost:1527/" 
+ "jfl", "jfl", "jfl"); 
      st = con.createStatement(); 
      String s = "SELECT Teamname,ID from JFL.TEAMS"; 

      rs = st.executeQuery(s); 
      while (rs.next()) { 
       comboH.addItem(rs.getString(1) + " " + rs.getString(2)); 
       comboA.addItem(rs.getString(1) + " " + rs.getString(2)); 
      } 
     } catch (Exception e) { 
      JOptionPane.showMessageDialog(null, "ERROR"); 
     } finally { 
      try { 
       st.close(); 
       rs.close(); 
       con.close(); 
      } catch (Exception e) { 
       JOptionPane.showMessageDialog(null, "ERROR CLOSE"); 
      } 
     } 
    } 

    /** 
    * Create the GUI and show it. 
    * 
    * @param evt 
    */ 
    public void actionPerformedd(ActionEvent evt) { 

     String text = textField.getText(); 
     textArea.append(text + newline); 
     textField.selectAll(); 

     //Make sure the new text is visible, even if there 
     //was a selection in the text area. 
     textArea.setCaretPosition(textArea.getDocument().getLength()); 
    } 

    private static void createAndShowGUI() { 
     //Create and set up the window. 
     JFrame frame = new JFrame("Junior Football League"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JMenuBar menubar = new JMenuBar(); 
     frame.setJMenuBar(menubar); 

     //file menu 
     JMenu fileMenu = new JMenu("File"); 
     menubar.add(fileMenu); 

     JMenuItem quitItem = new JMenuItem("Quit"); 
     fileMenu.add(quitItem); 
     quitItem.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       //call another method to close window 
       System.exit(0); 
      } 
     }); 
     JMenu helpItem = new JMenu("Teams"); 
     menubar.add(helpItem); 

     JMenuItem subMenu = new JMenuItem("Create New Team"); 


    public static void main(String[] args) { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

} 

을 넣어 있도록 아래를 좁혀 :

예외 글 "AWT-EventQueue의-0"java.lang.NumberFormatException의 : 입력 문자열 ('')에서 java.lang.NumberFormatException.forInputString (NumberFormatException.java:65) demooo.Demooo.addComponentsToPane에서 java.lang.Integer.parseInt (Integer.java:615)에서 java.lang.Integer.parseInt (Integer.java:592)에서 (Demooo.java : demooo.Demooo.createAndShowGUI에서 127) 자바에서 demooo.Demooo $ 6.run에서 demooo.Demooo.access $ 000 (Demooo.java:12에서 (Demooo.java:918)) (Demooo.java:936) .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.sec java.awt.EventDispatchThread에서 java.awt.EventQueue.dispatchEvent (EventQueue.java:726)에서 java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:80) 에서 (기본 방법) urity.AccessController.doPrivileged .pumpOneEventForFilters (EventDispatchThread.java:201) 있는 java.awt에서 java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java:105) 에서 java.awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread.java:116) 에서 . EventDispatchThread.pumpEvents (EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:93)java.awt.EventDispatchThread.run에서 0 (EventDispatchThread.java:82) BUILD SUCCESSFUL (총 시간 : 1 초)

+2

당신은 당신이 그것을 좁혔다 고 말했지만, 당신은 여기에 전체 코드를 버린 것처럼 보입니다. 적절한 [mcve]를 만들거나 적어도 오류가 발생한 코드 만 줄이십시오.이 방법은 너무 많습니다 – UnholySheep

+2

구문 분석 할 것이 없으면 정수를 구문 분석 할 수 없습니다 ... 문자열은 오류 메시지를 기반으로 구문 분석을 시도하기 전에 비어 있습니다. – DigitalNinja

+0

당신의'Demooo.java'에서 # ** 127 ** 라인에 무엇이 있습니까? –

답변

1

넌 127

int h = Integer.parseInt(homegoal.getText()); 

다음 라인의 줄이있을 예외는 그 라인에 오류가 있음을 말해주고 변환 형식 문자열을 Int로 간주합니다.

java.lang.Integer.parseInt(Integer.java:592) at 
java.lang.Integer.parseInt(Integer.java:615) at 
demooo.Demooo.addComponentsToPane(Demooo.java:127) at 

나는 homegoal.getText()에 대한 통해 오는 어떤 값을 살펴 것입니다. null의 경우, int 변수에 0을 넣습니다.

편집 :보기에서 보면 비어있게 텍스트 필드를 만들고있는 것입니다. 그래서 오류입니다. 비어 있는지 확인하고 비어있는 경우 0을 지정하십시오.

int h = (homegoal == null || homegoal.getText().isEmpty()? 0:Integer.parseInt(homegoal.getText());