无法执行Java程序

时间:2019-03-17 05:18:49

标签: java floating-point joptionpane string-conversion

当我尝试编译程序时,它编译成功。但是当我执行程序时,这是我在命令提示符下收到的输出

  

线程“主”中的异常java.lang.NullPointerException           在java.base / jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)           在java.base / jdk.internal.math.FloatingDecimal.parseFloat(FloatingDecimal.java:122)           在java.base / java.lang.Float.parseFloat(Float.java:455)           在grade.main(grade.java:9)

 import javax.swing.JOptionPane;

    class grade{ 
             public static void main(String[] args){


         String engmark=null;
         String mathmark=null;
         String sstmark=null;
         String scimark=null;
         String compmark=null;
         float sc=Float.parseFloat(scimark);
         float en=Float.parseFloat(engmark);
         float ss=Float.parseFloat(sstmark);
         float mt=Float.parseFloat(mathmark);
         float co=Float.parseFloat(compmark);

         float totmark= sc+en+ss+co+mt;


         String response;


          response= JOptionPane.showInputDialog("Enter the maximum marks of each subject : ");

                 if (response==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (response.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }

                          else

                        engmark= JOptionPane.showInputDialog(null,"Enter the marks obtained in english :");

                                        if (engmark==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (engmark.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }
                                else           
                                 mathmark= JOptionPane.showInputDialog(null,"Enter the marks obtained in mathematics :");
                                        if (mathmark==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (mathmark.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }

                                else                                    
                                scimark= JOptionPane.showInputDialog(null,"Enter the marks obtained in science :");
                                        if (scimark==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (scimark.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }

                                else                                    
                                sstmark= JOptionPane.showInputDialog(null,"Enter the marks obtained in S.st. :");
                                        if (sstmark==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (sstmark.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }

                                else                                    
                                compmark= JOptionPane.showInputDialog(null,"Enter the marks obtained in computer :");
                                        if (compmark==null){JOptionPane.showMessageDialog(null,"You  clicked on the cancel button.");
                                         System.exit(0);
                                         }               
                     else
                     if (compmark.equals("")){JOptionPane.showMessageDialog(null,"You must make an entry in the input box");
                                      System.exit(0);
                                           }



                     else

                     JOptionPane.showMessageDialog(null,"Your total marks is :" + totmark);                          

}

}

1 个答案:

答案 0 :(得分:0)

您的代码在float sc=Float.parseFloat(scimark);处炸毁,原因是scimark为空。将其更改为其他内容,并将其余部分更改为没有null作为parseFloat的参数。

相关问题